@hero-design/snowflake-guard
Version:
A hero-design bot detecting snowflake usage
136 lines (135 loc) • 5.75 kB
JavaScript
;
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 recast = __importStar(require("recast"));
const flowParser = __importStar(require("./parsers/flow"));
const tsParser = __importStar(require("./parsers/typescript"));
const constants_1 = require("./reports/mobile/constants");
const constants_2 = require("./reports/constants");
const reportCustomStyleProperties_1 = __importDefault(require("./reports/mobile/reportCustomStyleProperties"));
const reportStyledComponents_1 = __importDefault(require("./reports/mobile/reportStyledComponents"));
const parseMobileSource = (source) => {
let hasHeroDesignImport = false;
let hasStyledComponentsImport = false;
const componentList = {};
let styledAliasName = 'styled';
let styledComponentLocs = [];
let styleLocs = [];
let violatingAttributes = [];
const approvedInlineStyleCmts = [];
const approvedStyledComponentLocs = [];
const ast = recast.parse(source, {
parser: source.includes('@flow') ? flowParser : tsParser,
});
recast.visit(ast, {
visitImportDeclaration(path) {
this.traverse(path);
const importedFrom = path.value.source.value;
// Check if file imports components from '@hero-design/rn'
if (importedFrom === '@hero-design/rn') {
recast.visit(path.node, {
visitImportSpecifier(importPath) {
this.traverse(importPath);
if (constants_1.HD_MOBILE_COMPONENTS.includes(importPath.value.imported.name)) {
componentList[importPath.value.local.name] =
importPath.value.imported.name;
hasHeroDesignImport = true;
}
},
});
}
// Check if file imports from '@emotion/native'
if (importedFrom === '@emotion/native') {
recast.visit(path.node, {
visitImportDefaultSpecifier(importPath) {
this.traverse(importPath);
styledAliasName = importPath.value.local.name;
hasStyledComponentsImport = true;
},
});
}
},
visitComment(path) {
this.traverse(path);
const comment = path.value.value;
if (comment
.toLowerCase()
.includes(constants_2.APPROVED_INLINE_STYLE_COMMENT.toLowerCase())) {
approvedInlineStyleCmts.push({
loc: path.value.loc.start.line,
comment,
});
}
if (comment
.toLowerCase()
.includes(constants_2.APPROVED_STYLED_COMPONENTS_COMMENT.toLowerCase())) {
approvedStyledComponentLocs.push(path.value.loc.start.line);
}
},
});
const isNotApprovedStyledComponentSnowflakes = (loc) => !approvedStyledComponentLocs.includes(loc - 1);
if (hasHeroDesignImport) {
// Case 1: Using style and other style config objects to customise components
const customPropLocs = (0, reportCustomStyleProperties_1.default)(ast, componentList, {
styleCmts: approvedInlineStyleCmts,
});
styleLocs = [
...new Set([
...customPropLocs.style,
...customPropLocs.barStyle,
...customPropLocs.containerStyle,
...customPropLocs.textStyle,
]),
];
// Case 2: Using styled-components to customise components
if (hasStyledComponentsImport) {
styledComponentLocs = (0, reportStyledComponents_1.default)(ast, componentList, styledAliasName).filter(isNotApprovedStyledComponentSnowflakes);
}
violatingAttributes = customPropLocs.violatingAttributes;
}
return {
styleLocs,
styledComponentLocs,
approvedLocs: [
...approvedInlineStyleCmts.map((cmt) => cmt.loc),
...approvedStyledComponentLocs,
],
violatingAttributes,
};
};
exports.default = parseMobileSource;