UNPKG

10.9 kBSource Map (JSON)View Raw
1{"version":3,"file":"tslint-ban-snippets.umd.js","sources":["../src/ruleIds.ts","../src/config/ConfigFactory.ts","../src/utils/GeneralRuleUtils.ts","../src/tslBanSnippetsRule.ts"],"sourcesContent":["export const BAN_SNIPPETS_RULE_ID = \"tsl-ban-snippets\";\n","import { BanSnippetsRuleConfig } from \"../model/BanSnippetsRuleConfig\";\nimport { BAN_SNIPPETS_RULE_ID } from \"../ruleIds\";\n\nconst SNIPPETS_PROP = \"snippets\";\nconst REGEX_SNIPPETS_PROP = \"regexSnippets\";\n\nexport namespace ConfigFactory {\n export function createForBanSnippetsRule(options: any): BanSnippetsRuleConfig {\n const config = createFromArguments<BanSnippetsRuleConfig>(options, BAN_SNIPPETS_RULE_ID);\n\n validate(config, \"banned\", BAN_SNIPPETS_RULE_ID);\n config.banned.forEach(b => validateSnippetsList(b, BAN_SNIPPETS_RULE_ID));\n\n return config;\n }\n\n function validate(config: any, prop: string, ruleId: string) {\n if (!hasProp(config, prop)) {\n throw new Error(`invalid config for rule ${ruleId} - ${prop} is missing`);\n }\n }\n\n function hasProp(config: any, prop: string): boolean {\n return config[prop] !== undefined;\n }\n\n function validateSnippetsList(config: any, ruleId: string) {\n // either snippets OR regexSnippets is required\n const hasSnippets = hasProp(config, SNIPPETS_PROP);\n const hasRegexSnippets = hasProp(config, REGEX_SNIPPETS_PROP);\n\n const isValid = hasSnippets || hasRegexSnippets;\n\n const hasBoth = hasSnippets && hasRegexSnippets;\n\n if (!isValid || hasBoth) {\n throw new Error(\n `invalid config for rule ${ruleId} - either ${SNIPPETS_PROP} or ${REGEX_SNIPPETS_PROP} must be set`\n );\n }\n }\n\n function createFromArguments<T>(options: any, ruleId: string): T {\n const args = options.ruleArguments;\n\n if (args.length !== 1) {\n throw new Error(\n `tslint rule is misconfigured (${ruleId}) - options.ruleArguments length is ${\n args.length\n }`\n );\n }\n\n const config: T = args[0];\n\n return config;\n }\n}\n","export namespace GeneralRuleUtils {\n export function buildFailureString(message: string, ruleId: string): string {\n // include the rule ID, to make it easier to disable\n return `${message} (${ruleId})`;\n }\n\n export function isFileInPaths(filePath: string, paths: string[]): boolean {\n return paths.some(path => filePath.indexOf(path) >= 0);\n }\n}\n","import * as Lint from \"tslint\";\nimport * as ts from \"typescript\";\n\nimport { ConfigFactory } from \"./config/ConfigFactory\";\nimport { BannedSnippet, BanSnippetsRuleConfig } from \"./model/BanSnippetsRuleConfig\";\nimport { BAN_SNIPPETS_RULE_ID } from \"./ruleIds\";\nimport { GeneralRuleUtils } from \"./utils/GeneralRuleUtils\";\n\nexport class Rule extends Lint.Rules.AbstractRule {\n apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {\n const config = ConfigFactory.createForBanSnippetsRule(this.getOptions());\n\n const walker = new StatementsWalker(sourceFile, this.getOptions(), config);\n this.applyWithWalker(walker);\n\n return walker.getFailures();\n }\n}\n\nclass StatementsWalker extends Lint.RuleWalker {\n constructor(\n sourceFile: ts.SourceFile,\n options: Lint.IOptions,\n private config: BanSnippetsRuleConfig\n ) {\n super(sourceFile, options);\n }\n\n visitReturnStatement(node: ts.ReturnStatement) {\n this.visitSomeNode(node);\n }\n\n protected visitBinaryExpression(node: ts.BinaryExpression) {\n this.visitSomeNode(node);\n\n super.visitBinaryExpression(node);\n }\n\n protected visitCallExpression(node: ts.CallExpression) {\n this.visitSomeNode(node);\n\n super.visitCallExpression(node);\n }\n\n protected visitDebuggerStatement(node: ts.Statement) {\n this.visitSomeNode(node);\n\n super.visitDebuggerStatement(node);\n }\n\n private visitSomeNode(node: ts.Node) {\n const text = node.getText();\n\n const relevantBanned = this.getRelevantBanned();\n\n relevantBanned.forEach(banned => {\n if (banned.snippets) {\n this.checkBannedSnippet(banned.snippets, text, node, banned.message);\n } else if (banned.regexSnippets) {\n this.checkRegexBannedSnippet(banned.regexSnippets, text, node, banned.message);\n } else {\n throw new Error(\"Invalid config? No snippets and no regex-snippets\");\n }\n });\n }\n\n private checkBannedSnippet(snippets: string[], text: string, node: ts.Node, message?: string) {\n const bannedCodeFound = snippets.filter(bannedSnippet => text.indexOf(bannedSnippet) >= 0);\n\n if (bannedCodeFound.length > 0) {\n this.failRule(node, bannedCodeFound, message);\n }\n }\n\n private checkRegexBannedSnippet(\n regexSnippets: string[],\n text: string,\n node: ts.Node,\n message?: string\n ) {\n const bannedCodeFound = regexSnippets.filter(regexSnippet => {\n const regex = new RegExp(regexSnippet);\n\n return regex.test(text);\n });\n\n if (bannedCodeFound.length > 0) {\n this.failRule(node, bannedCodeFound, message);\n }\n }\n\n private failRule(node: ts.Node, bannedCodeFound: string[], message?: string) {\n const failureNode = node.getFirstToken() || node;\n\n this.addFailureAtNode(\n failureNode,\n GeneralRuleUtils.buildFailureString(\n message || `Do not use banned code '${bannedCodeFound.join(\"' or '\")}'.`,\n BAN_SNIPPETS_RULE_ID\n )\n );\n }\n\n private getRelevantBanned(): BannedSnippet[] {\n const sourceFilePath = this.getSourceFile().fileName;\n\n return this.config.banned.filter(\n b =>\n (!b.includePaths ||\n GeneralRuleUtils.isFileInPaths(sourceFilePath, b.includePaths)) &&\n (!b.excludePaths || !GeneralRuleUtils.isFileInPaths(sourceFilePath, b.excludePaths))\n );\n }\n}\n"],"names":["tslib_1.__extends","Lint.Rules","Lint.RuleWalker"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAAO,IAAM,oBAAoB,GAAG,kBAAkB,CAAC;;ICGvD,IAAM,aAAa,GAAG,UAAU,CAAC;IACjC,IAAM,mBAAmB,GAAG,eAAe,CAAC;AAE5C,QAAiB,aAAa,CAmD7B;IAnDD,WAAiB,aAAa;QAC1B,SAAgB,wBAAwB,CAAC,OAAY;YACjD,IAAM,MAAM,GAAG,mBAAmB,CAAwB,OAAO,EAAE,oBAAoB,CAAC,CAAC;YAEzF,QAAQ,CAAC,MAAM,EAAE,QAAQ,EAAE,oBAAoB,CAAC,CAAC;YACjD,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,UAA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