@hero-design/snowflake-guard
Version:
A hero-design bot detecting snowflake usage
324 lines (323 loc) • 12.2 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const parseMobileSource_1 = __importDefault(require("../parseMobileSource"));
describe('parseSource', () => {
it('reports correct snowflakes with typescript files', () => {
const source = fs.readFileSync('./src/__mocks__/mobileSourceSample.tsx', 'utf-8');
expect((0, parseMobileSource_1.default)(source)).toEqual({
approvedLocs: [54, 56, 72, 81, 18],
styleLocs: [71, 75, 82, 48, 50, 51, 52, 53, 64, 66, 67, 68, 69, 70],
styledComponentLocs: [14, 23, 28],
violatingAttributes: [
{
attributeName: 'padding',
attributeValue: '20',
componentName: 'Button.Icon',
inlineStyleProps: 'style',
loc: 71,
},
{
attributeName: 'padding',
attributeValue: '20',
componentName: 'Button.Icon',
inlineStyleProps: 'style',
loc: 75,
},
{
attributeName: 'backgroundColor',
attributeValue: "'red'",
componentName: 'Button.Icon',
inlineStyleProps: 'style',
loc: 75,
},
{
attributeName: 'backgroundColor',
attributeValue: "'red'",
componentName: 'Button.Icon',
inlineStyleProps: 'style',
loc: 82,
},
{
attributeName: 'padding',
attributeValue: '10',
componentName: 'Tabs',
inlineStyleProps: 'barStyle',
loc: 48,
},
{
attributeName: 'width',
attributeValue: '100',
componentName: 'Tabs',
inlineStyleProps: 'containerStyle',
loc: 48,
},
{
attributeName: 'width',
attributeValue: '100',
componentName: 'Tabs.Scroll',
inlineStyleProps: 'containerStyle',
loc: 50,
},
{
attributeName: 'color',
attributeValue: "'red'",
componentName: 'TextInput',
inlineStyleProps: 'textStyle',
loc: 51,
},
{
attributeName: 'color',
attributeValue: "'red'",
componentName: 'Search.OneLine',
inlineStyleProps: 'textStyle',
loc: 52,
},
{
attributeName: 'borderColor',
attributeValue: "'red'",
componentName: 'Search.OneLine',
inlineStyleProps: 'textStyle',
loc: 52,
},
{
attributeName: 'color',
attributeValue: "'red'",
componentName: 'Toolbar.Message',
inlineStyleProps: 'textStyle',
loc: 53,
},
{
attributeName: 'borderColor',
attributeValue: "'red'",
componentName: 'Toolbar.Message',
inlineStyleProps: 'textStyle',
loc: 53,
},
{
attributeName: 'width',
attributeValue: '200',
componentName: 'Empty',
inlineStyleProps: 'style',
loc: 64,
},
{
attributeName: 'width',
attributeValue: '200',
componentName: 'Button.Utility',
inlineStyleProps: 'style',
loc: 66,
},
{
attributeName: 'padding',
attributeValue: '30',
componentName: 'Button',
inlineStyleProps: 'style',
loc: 67,
},
{
attributeName: 'padding',
attributeValue: '30',
componentName: 'Button',
inlineStyleProps: 'style',
loc: 68,
},
{
attributeName: 'padding',
attributeValue: '30',
componentName: 'Button',
inlineStyleProps: 'style',
loc: 69,
},
{
attributeName: 'padding',
attributeValue: '30',
componentName: 'Button',
inlineStyleProps: 'style',
loc: 70,
},
],
});
});
it('reports correct snowflakes with flow files', () => {
const source = fs.readFileSync('./src/__mocks__/mobileSourceSample.js', 'utf-8');
expect((0, parseMobileSource_1.default)(source)).toEqual({
approvedLocs: [55, 57, 73, 82, 19],
styleLocs: [72, 76, 83, 49, 51, 52, 53, 54, 65, 67, 68, 69, 70, 71],
styledComponentLocs: [15, 24, 29],
violatingAttributes: [
{
attributeName: 'padding',
attributeValue: '20',
componentName: 'Button.Icon',
inlineStyleProps: 'style',
loc: 72,
},
{
attributeName: 'padding',
attributeValue: '20',
componentName: 'Button.Icon',
inlineStyleProps: 'style',
loc: 76,
},
{
attributeName: 'backgroundColor',
attributeValue: "'red'",
componentName: 'Button.Icon',
inlineStyleProps: 'style',
loc: 76,
},
{
attributeName: 'backgroundColor',
attributeValue: "'red'",
componentName: 'Button.Icon',
inlineStyleProps: 'style',
loc: 83,
},
{
attributeName: 'padding',
attributeValue: '10',
componentName: 'Tabs',
inlineStyleProps: 'barStyle',
loc: 49,
},
{
attributeName: 'width',
attributeValue: '100',
componentName: 'Tabs',
inlineStyleProps: 'containerStyle',
loc: 49,
},
{
attributeName: 'width',
attributeValue: '100',
componentName: 'Tabs.Scroll',
inlineStyleProps: 'containerStyle',
loc: 51,
},
{
attributeName: 'color',
attributeValue: "'red'",
componentName: 'TextInput',
inlineStyleProps: 'textStyle',
loc: 52,
},
{
attributeName: 'color',
attributeValue: "'red'",
componentName: 'Search.OneLine',
inlineStyleProps: 'textStyle',
loc: 53,
},
{
attributeName: 'borderColor',
attributeValue: "'red'",
componentName: 'Search.OneLine',
inlineStyleProps: 'textStyle',
loc: 53,
},
{
attributeName: 'color',
attributeValue: "'red'",
componentName: 'Toolbar.Message',
inlineStyleProps: 'textStyle',
loc: 54,
},
{
attributeName: 'borderColor',
attributeValue: "'red'",
componentName: 'Toolbar.Message',
inlineStyleProps: 'textStyle',
loc: 54,
},
{
attributeName: 'width',
attributeValue: '200',
componentName: 'Empty',
inlineStyleProps: 'style',
loc: 65,
},
{
attributeName: 'width',
attributeValue: '200',
componentName: 'Button.Utility',
inlineStyleProps: 'style',
loc: 67,
},
{
attributeName: 'padding',
attributeValue: '30',
componentName: 'Button',
inlineStyleProps: 'style',
loc: 68,
},
{
attributeName: 'padding',
attributeValue: '30',
componentName: 'Button',
inlineStyleProps: 'style',
loc: 69,
},
{
attributeName: 'padding',
attributeValue: '30',
componentName: 'Button',
inlineStyleProps: 'style',
loc: 70,
},
{
attributeName: 'padding',
attributeValue: '30',
componentName: 'Button',
inlineStyleProps: 'style',
loc: 71,
},
],
});
});
it('does not include approved snowflakes in the report', () => {
const source = fs.readFileSync('./src/__mocks__/mobileApprovalSourceSample.tsx', 'utf-8');
expect((0, parseMobileSource_1.default)(source)).toEqual({
approvedLocs: [14, 16, 7],
styleLocs: [],
styledComponentLocs: [],
violatingAttributes: [],
});
});
});