UNPKG

tslint-etc

Version:
49 lines (48 loc) 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Rule = void 0; const tsquery_1 = require("@phenomnomnominal/tsquery"); const Lint = require("tslint"); const support_1 = require("../support"); class Rule extends Lint.Rules.TypedRule { applyWithProgram(sourceFile, program) { const failures = []; const typeChecker = program.getTypeChecker(); const statements = tsquery_1.tsquery(sourceFile, `CallExpression[expression.text="it"] ExpressionStatement, CallExpression[expression.text="it"] VariableStatement`); statements.forEach((statement) => { const index = sourceFile.text.indexOf("\n", statement.end); if (index !== -1) { const trailing = sourceFile.text.substring(statement.end, index); const match = trailing.match(/^\s*\/\/\s*(\$ExpectDeprecation|\$ExpectNoDeprecation)\s*$/); if (match) { const idendtifers = tsquery_1.tsquery(statement, "Identifier"); const found = idendtifers.some((idendtifer) => support_1.getDeprecation(idendtifer, typeChecker) !== undefined); const [, expectation] = match; const pos = statement.end + trailing.indexOf(expectation); const end = pos + expectation.length; if (expectation === "$ExpectDeprecation") { if (!found) { failures.push(new Lint.RuleFailure(sourceFile, pos, end, "No deprecation found", this.ruleName)); } } else { if (found) { failures.push(new Lint.RuleFailure(sourceFile, pos, end, "Deprecation found", this.ruleName)); } } } } }); return failures; } } exports.Rule = Rule; Rule.metadata = { description: "Asserts deprecations with $ExpectDeprecation and $ExpectNoDeprecation.", options: null, optionsDescription: "Not configurable.", requiresTypeInfo: true, ruleName: "expect-deprecation", type: "functionality", typescriptOnly: true, };