UNPKG

@hero-design/snowflake-guard

Version:

A hero-design bot detecting snowflake usage

106 lines (105 loc) 4.92 kB
"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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const recast = __importStar(require("recast")); const reportStyledComponents = (ast, componentList, styledAliasName) => { const locs = []; const localComponentList = Object.keys(componentList); recast.visit(ast, { visitVariableDeclaration(path) { this.traverse(path); const declaration = path.value .declarations[0]; if (declaration.init && declaration.init.type === 'TaggedTemplateExpression') { const { tag } = declaration.init; if (tag.type === 'CallExpression' && tag.callee.type === 'Identifier' && tag.callee.name === styledAliasName) { const arg = tag.arguments[0]; // Case 1: Custom default component, e.g. styled(Button) if (arg.type === 'Identifier' && localComponentList.includes(arg.name) && tag.loc) { locs.push(tag.loc.start.line); } // Case 2: Custom compound component, e.g. styled(Button.Icon) if (arg.type === 'MemberExpression' && arg.object.type === 'Identifier' && localComponentList.includes(arg.object.name) && tag.loc) { locs.push(tag.loc.start.line); } } } // Case 3: Custom compound component with spead operator. e.g. const { Icon } = Button, then styled(Icon) if (declaration.init && declaration.init.type === 'Identifier' && localComponentList.includes(declaration.init.name)) { const { id } = declaration; if (id.type === 'ObjectPattern') { const compoundComponentNames = id.properties .map((prop) => prop.type === 'ObjectProperty' && prop.key.type === 'Identifier' ? prop.key.name : null) .filter((name) => name !== null); recast.visit(ast, { visitVariableDeclaration(spreadPath) { this.traverse(spreadPath); const spreadDeclaration = spreadPath.value .declarations[0]; if (spreadDeclaration.init && spreadDeclaration.init.type === 'TaggedTemplateExpression') { const { tag } = spreadDeclaration.init; if (tag.type === 'CallExpression' && tag.callee.type === 'Identifier' && tag.callee.name === styledAliasName) { const arg = tag.arguments[0]; if (arg.type === 'Identifier' && compoundComponentNames.includes(arg.name) && tag.loc) { locs.push(tag.loc.start.line); } } } }, }); } } }, }); return locs; }; exports.default = reportStyledComponents;