eslint-plugin-dtslint
Version:
ESLint rules for dtslint tests
86 lines (85 loc) • 3.35 kB
JavaScript
const tsquery_1 = require("@phenomnomnominal/tsquery");
const eslint_etc_1 = require("eslint-etc");
const tslint_deprecation_1 = require("../tslint-deprecation");
const utils_1 = require("../utils");
const rule = (0, utils_1.ruleCreator)({
defaultOptions: [],
meta: {
docs: {
description: "Asserts deprecations with `$ExpectDeprecation` and `$ExpectNoDeprecation`.",
recommended: false,
},
fixable: undefined,
hasSuggestions: false,
messages: {
found: "Deprecation found.",
notFound: "Deprecation not found.",
},
schema: [],
type: "problem",
},
name: "expect-deprecation",
create: (context) => {
const { esTreeNodeToTSNodeMap } = (0, eslint_etc_1.getParserServices)(context);
const { typeChecker } = (0, eslint_etc_1.getTypeServices)(context);
let expectations = {};
function check(node) {
const { line } = node.loc.end;
if (expectations.hasOwnProperty(line)) {
const { expected, loc } = expectations[line];
const idendtifers = (0, tsquery_1.tsquery)(esTreeNodeToTSNodeMap.get(node), "Identifier");
const found = idendtifers.some((idendtifer) => (0, tslint_deprecation_1.getDeprecation)(idendtifer, typeChecker) !== undefined);
if (expected) {
if (!found) {
context.report({
loc,
messageId: "notFound",
});
}
}
else {
if (found) {
context.report({
loc,
messageId: "found",
});
}
}
}
}
return {
ExpressionStatement: check,
Program: (node) => {
const { comments } = node;
if (!comments) {
return;
}
expectations = comments.reduce((record, comment) => {
const { value } = comment;
const match = value.match(/(\s*)(\$ExpectDeprecation|\$ExpectNoDeprecation)/);
if (match) {
const [, whitespace, expectation] = match;
const commentTokenWidth = 2;
const loc = {
...comment.loc,
start: {
...comment.loc.start,
column: comment.loc.start.column +
commentTokenWidth +
whitespace.length,
},
};
record[loc.start.line] = {
expected: /^\$ExpectDeprecation/.test(expectation),
loc,
};
}
return record;
}, {});
},
VariableDeclaration: check,
};
},
});
module.exports = rule;
;