@hero-design/snowflake-guard
Version:
A hero-design bot detecting snowflake usage
139 lines (138 loc) • 5.92 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 tsParser = __importStar(require("./parsers/typescript"));
const reportCustomStyleProperties_1 = __importDefault(require("./reports/reportCustomStyleProperties"));
const reportStyledComponents_1 = __importDefault(require("./reports/reportStyledComponents"));
const constants_1 = require("./reports/constants");
const parseSource = (source) => {
let hasHeroDesignImport = false;
let hasStyledComponentsImport = false;
const componentList = {};
let styledAliasName = 'styled';
let styledComponentLocs = [];
let classNameLocs = [];
let styleLocs = [];
let sxLocs = [];
let violatingAttributes = [];
const approvedInlineStyleCmts = [];
const approvedClassnameLocs = [];
const approvedStyledComponentLocs = [];
const ast = recast.parse(source, { parser: tsParser });
recast.visit(ast, {
visitImportDeclaration(path) {
this.traverse(path);
const importedFrom = path.value.source.value;
// Check if file imports components from '@hero-design/react'
if (importedFrom === '@hero-design/react') {
recast.visit(path.node, {
visitImportSpecifier(importPath) {
this.traverse(importPath);
if (constants_1.HD_COMPONENTS.includes(importPath.value.imported.name)) {
componentList[importPath.value.local.name] =
importPath.value.imported.name;
hasHeroDesignImport = true;
}
},
});
}
// Check if file imports from 'styled-components'
if (importedFrom === 'styled-components') {
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_1.APPROVED_INLINE_STYLE_COMMENT.toLowerCase())) {
approvedInlineStyleCmts.push({
loc: path.value.loc.start.line,
comment,
});
}
if (comment.toLowerCase().includes(constants_1.APPROVED_CLASSNAME_COMMENT.toLowerCase())) {
approvedClassnameLocs.push(path.value.loc.start.line);
}
if (comment
.toLowerCase()
.includes(constants_1.APPROVED_STYLED_COMPONENTS_COMMENT.toLowerCase())) {
approvedStyledComponentLocs.push(path.value.loc.start.line);
}
},
});
const isNotApprovedStyledComponentSnowflakes = (loc) => !approvedStyledComponentLocs.includes(loc - 1);
if (hasHeroDesignImport) {
// Case 1: Using className to customise components
// Case 2: Using style object to customise components
// Case 3: Using sx object to customise components
const customPropLocs = (0, reportCustomStyleProperties_1.default)(ast, componentList, {
classNameCmts: approvedClassnameLocs,
styleCmts: approvedInlineStyleCmts,
});
classNameLocs = customPropLocs.className;
styleLocs = customPropLocs.style;
sxLocs = customPropLocs.sx;
// Case 4: Using styled-components to customise components
if (hasStyledComponentsImport) {
styledComponentLocs = (0, reportStyledComponents_1.default)(ast, componentList, styledAliasName).filter(isNotApprovedStyledComponentSnowflakes);
}
violatingAttributes = customPropLocs.violatingAttributes;
}
return {
classNameLocs,
styleLocs,
sxLocs,
styledComponentLocs,
approvedLocs: [
...approvedInlineStyleCmts.map((cmt) => cmt.loc),
...approvedClassnameLocs,
...approvedStyledComponentLocs,
],
violatingAttributes,
};
};
exports.default = parseSource;