UNPKG

@hero-design/snowflake-guard

Version:

A hero-design bot detecting snowflake usage

114 lines (113 loc) 4.51 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; }; })(); 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 reportInlineStyle_1 = __importDefault(require("../reportInlineStyle")); const constants_1 = require("../constants"); const tsParser = __importStar(require("../../../parsers/typescript")); describe('reportInlineStyle', () => { it('should return line numbers and violating attributes for inline styles that violate the ruleset', () => { const code = ` const MyComponent = () => ( <Card style={{ color: 'red', backgroundColor: 'blue' }} /> ); `; // Step 1: Parse the code to generate the AST const ast = recast.parse(code, { parser: tsParser }); const attributes = []; // Step 2: Traverse the AST to extract relevant JSXAttributes (style) recast.visit(ast, { visitJSXAttribute(path) { const { node } = path; attributes.push(node); this.traverse(path); }, }); const componentName = 'Card'; // Mock the RULESET_MAP for this test constants_1.MOBILE_RULESET_MAP.Card = ['color', 'backgroundColor']; // Step 3: Run the function with the dynamically extracted attributes const result = (0, reportInlineStyle_1.default)(ast, attributes, componentName); expect(result.locs.style).toEqual(3); expect(result.violatingAttributes).toEqual([ { attributeName: 'color', attributeValue: "'red'", componentName: 'Card', inlineStyleProps: 'style', loc: 3, }, { attributeName: 'backgroundColor', attributeValue: "'blue'", componentName: 'Card', inlineStyleProps: 'style', loc: 3, }, ]); }); it('should return empty arrays when no violations are found', () => { const code = ` const MyComponent = () => ( <Card style={{ margin: 10 }} /> ); `; // Step 1: Parse the code to generate the AST const ast = recast.parse(code, { parser: tsParser }); const attributes = []; // Step 2: Traverse the AST to extract relevant JSXAttributes (style) recast.visit(ast, { visitJSXAttribute(path) { const { node } = path; attributes.push(node); this.traverse(path); }, }); const componentName = 'Card'; // Mock the RULESET_MAP and SX_RULESET_MAP for this test constants_1.MOBILE_RULESET_MAP.Card = ['color']; const result = (0, reportInlineStyle_1.default)(ast, attributes, componentName); expect(result.locs.style).toEqual(undefined); expect(result.violatingAttributes).toEqual([]); expect(result.violatingAttributes).toEqual([]); }); });