forked from prettier/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-node-comments.js
91 lines (85 loc) · 2.2 KB
/
no-node-comments.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"use strict";
const path = require("path");
// `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";
module.exports = {
meta: {
type: "suggestion",
docs: {
url: "https://github.com/prettier/prettier/blob/main/scripts/tools/eslint-plugin-prettier-internal-rules/no-node-comments.js",
},
messages: {
[messageId]: "Do not access node.comments.",
},
},
create(context) {
const fileName = context.getFilename();
const ignored = new Map(
context.options.map((option) => {
if (typeof option === "string") {
option = { file: option };
}
const { file, functions } = option;
return [
path.join(__dirname, "../../..", file),
functions ? new Set(functions) : true,
];
})
);
// avoid report on `const {comments} = node` twice
const reported = new Set();
return {
[selector](node) {
if (reported.has(node)) {
return;
}
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;
}
}
reported.add(node);
context.report({
node,
messageId,
});
},
};
},
};