forked from mysticatea/eslint-plugin-eslint-comments
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathget-all-directive-comments.js
154 lines (143 loc) · 5.56 KB
/
get-all-directive-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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"use strict"
const utils = require("./utils")
/**
* @typedef {object} DirectiveComment
* @property {string} kind The kind of directive comment.
* @property {string} [value] The directive value if it is `eslint-` comment.
* @property {string} description The description of the directive comment.
* @property {object} node The node of the directive comment.
* @property {import("@eslint/core").SourceRange} range The range of the directive comment.
* @property {import("@eslint/core").SourceLocation} loc The location of the directive comment.
*/
const pool = new WeakMap()
/**
* @param {import('eslint').SourceCode} sourceCode - The source code to scan.
* @returns {DirectiveComment[]} The directive comments.
*/
function getAllDirectiveCommentsFromAllComments(sourceCode) {
return sourceCode
.getAllComments()
.map((comment) => ({
comment,
directiveComment: utils.parseDirectiveComment(comment),
}))
.filter(({ directiveComment }) => Boolean(directiveComment))
.map(
({ comment, directiveComment }) =>
/** @type {DirectiveComment} */ ({
kind: directiveComment.kind,
value: directiveComment.value,
description: directiveComment.description,
node: comment,
range: comment.range,
loc: comment.loc,
})
)
}
/**
* @param {import('@eslint/core').TextSourceCode} sourceCode - The source code to scan.
* @returns {DirectiveComment[]} The directive comments.
*/
function getAllDirectiveCommentsFromInlineConfigNodes(sourceCode) {
const result = sourceCode.getDisableDirectives().directives.map(
(directive) =>
/** @type {DirectiveComment} */ ({
kind: `eslint-${directive.type}`,
value: directive.value,
description: directive.justification,
node: directive.node,
range: sourceCode.getRange(directive.node),
get loc() {
return sourceCode.getLoc(directive.node)
},
})
)
return result.concat(
sourceCode
.getInlineConfigNodes()
.map((node) => ({
node,
range: sourceCode.getRange(node),
}))
.filter(
({ range }) =>
// The node has intersection of the directive comment.
// So, we need to skip it.
!result.some(
(comment) =>
comment.range[0] <= range[1] &&
range[0] <= comment.range[1]
)
)
.map(({ node, range }) => {
const nodeText = sourceCode.text.slice(range[0], range[1])
const commentValue = extractCommentContent(nodeText)
const directiveComment = utils.parseDirectiveText(commentValue)
return {
directiveComment,
node,
range,
}
})
.filter(
({ directiveComment }) =>
directiveComment != null &&
![
"eslint-disable",
"eslint-disable-line",
"eslint-disable-next-line",
"eslint-enable",
].includes(directiveComment.kind)
)
.map(
({ directiveComment, node, range }) =>
/** @type {DirectiveComment} */ ({
kind: directiveComment.kind,
value: directiveComment.value,
description: directiveComment.description,
node,
range,
get loc() {
return sourceCode.getLoc(node)
},
})
)
)
}
function extractCommentContent(text) {
// Extract comment content from the comment text.
// The comment format was based on the language comment definition in vscode-eslint.
// See https://github.com/microsoft/vscode-eslint/blob/c0e753713ea9935667e849d91e549adbff213e7e/server/src/languageDefaults.ts#L14
return text.startsWith("/*") && text.endsWith("*/")
? text.slice(2, -2)
: text.startsWith("//")
? text.slice(2)
: text.startsWith("<!--") && text.endsWith("-->")
? text.slice(4, -3)
: text.startsWith("###") && text.endsWith("###")
? text.slice(3, -3)
: text.startsWith("#")
? text.slice(1)
: text
}
module.exports = {
/**
* Get all directive comments for the given rule context.
*
* @param {import("@eslint/core").RuleContext} context - The rule context to get.
* @returns {DirectiveComment[]} The all directive comments object for the rule context.
*/
getAllDirectiveComments(context) {
const sourceCode = context.sourceCode || context.getSourceCode()
let result = pool.get(sourceCode.ast)
if (result == null) {
result =
typeof sourceCode.getInlineConfigNodes === "function" &&
typeof sourceCode.getDisableDirectives === "function"
? getAllDirectiveCommentsFromInlineConfigNodes(sourceCode)
: getAllDirectiveCommentsFromAllComments(sourceCode)
pool.set(sourceCode.ast, result)
}
return result
},
}