tslint-etc
Version:
More rules for TSLint
54 lines (53 loc) • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Rule = void 0;
const tsquery_1 = require("@phenomnomnominal/tsquery");
const Lint = require("tslint");
class Rule extends Lint.Rules.AbstractRule {
apply(sourceFile) {
const failures = [];
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*(.+)$/);
if (match) {
const [, expectation] = match;
const pos = statement.end + trailing.indexOf(expectation);
const end = pos + expectation.length;
if (/^(\$\s+)?Expect/.test(expectation)) {
failures.push(new Lint.RuleFailure(sourceFile, pos, end, Rule.FAILURE_STRING, this.ruleName));
return;
}
if (!/^\$Expect/.test(expectation)) {
return;
}
if (!/^\$Expect(Type\s*|Error\s*$|Deprecation\s*$|NoDeprecation\s*$)/.test(expectation)) {
failures.push(new Lint.RuleFailure(sourceFile, pos, end, Rule.FAILURE_STRING, this.ruleName));
return;
}
if (!/^\$ExpectType/.test(expectation)) {
return;
}
if (!/^\$ExpectType\s+[^\s]/.test(expectation)) {
failures.push(new Lint.RuleFailure(sourceFile, pos, end, Rule.FAILURE_STRING, this.ruleName));
return;
}
}
}
});
return failures;
}
}
exports.Rule = Rule;
Rule.metadata = {
description: "Disallows dtslint expectations that have typographical errors.",
options: null,
optionsDescription: "Not configurable.",
requiresTypeInfo: false,
ruleName: "no-dtslint-typo",
type: "functionality",
typescriptOnly: true,
};
Rule.FAILURE_STRING = "Typo in dtslint expectation";