Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

105 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

import path from "node:path";
2020-11-25 22:14:27 +08:00
// `node.comments`
const memberExpressionSelector = [
"MemberExpression[computed=false]",
"",
">",
"Identifier.property",
'[name="comments"]',
].join("");
// `const {comments} = node`
// `const {comments: nodeComments} = node`
const objectPatternSelector = [
"ObjectPattern",
">",
"Property.properties",
">",
"Identifier.key",
'[name="comments"]',
].join("");
const selector = `:matches(${memberExpressionSelector}, ${objectPatternSelector})`;
const messageId = "no-node-comments";
export default {
2020-11-25 22:14:27 +08:00
meta: {
type: "suggestion",
messages: {
[messageId]: "Do not access node.comments.",
},
schema: {
type: "array",
items: {
anyOf: [
{ type: "string" },
{
type: "object",
properties: {
file: { type: "string" },
functions: {
type: "array",
items: { type: "string" },
},
},
},
],
},
},
2020-11-25 22:14:27 +08:00
},
create(context) {
const { physicalFilename: fileName } = context;
2020-11-25 22:14:27 +08:00
const ignored = new Map(
context.options.map((option) => {
if (typeof option === "string") {
option = { file: option };
}
const { file, functions } = option;
return [
path.join(import.meta.dirname, "../../..", file),
2020-11-25 22:14:27 +08:00
functions ? new Set(functions) : true,
];
}),
);
2020-12-24 19:44:17 +08:00
// avoid report on `const {comments} = node` twice
const reported = new Set();
2020-11-25 22:14:27 +08:00
return {
[selector](node) {
2020-12-24 19:44:17 +08:00
if (reported.has(node)) {
return;
}
2020-11-25 22:14:27 +08:00
if (ignored.has(fileName)) {
const functionNames = ignored.get(fileName);
if (functionNames === true) {
return;
}
let isIgnored;
let currentNode = node.parent;
while (currentNode) {
if (
currentNode.type === "FunctionDeclaration" &&
currentNode.id &&
currentNode.id.type === "Identifier" &&
functionNames.has(currentNode.id.name)
) {
isIgnored = true;
break;
}
currentNode = currentNode.parent;
}
if (isIgnored) {
return;
}
}
2020-12-24 19:44:17 +08:00
reported.add(node);
2020-11-25 22:14:27 +08:00
context.report({
node,
messageId,
});
},
};
},
};