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
140 lines (131 loc) · 4.93 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
"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) {
/** @type {DirectiveComment[]} */
const result = []
for (const comment of sourceCode.getAllComments()) {
const directiveComment = utils.parseDirectiveComment(comment)
if (directiveComment != null) {
result.push({
kind: directiveComment.kind,
value: directiveComment.value,
description: directiveComment.description,
node: comment,
range: comment.range,
loc: comment.loc,
})
}
}
return result
}
/**
* @param {import('@eslint/core').TextSourceCode} sourceCode - The source code to scan.
* @returns {DirectiveComment[]} The directive comments.
*/
function getAllDirectiveCommentsFromInlineConfigNodes(sourceCode) {
/** @type {DirectiveComment[]} */
const result = []
const disableDirectives = sourceCode.getDisableDirectives()
for (const directive of disableDirectives.directives) {
result.push({
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)
},
})
}
for (const node of sourceCode.getInlineConfigNodes()) {
const range = sourceCode.getRange(node)
// The node has intersection of the directive comment.
// So, we need to skip it.
if (
result.some(
(comment) =>
comment.range[0] <= range[1] && range[0] <= comment.range[1]
)
) {
continue
}
const nodeText = sourceCode.text.slice(range[0], range[1])
const commentValue = extractCommentContent(nodeText)
const directiveComment = utils.parseDirectiveText(commentValue)
if (
directiveComment != null &&
directiveComment.kind !== "eslint-disable" &&
directiveComment.kind !== "eslint-disable-line" &&
directiveComment.kind !== "eslint-disable-next-line" &&
directiveComment.kind !== "eslint-enable"
) {
result.push({
kind: directiveComment.kind,
value: directiveComment.value,
description: directiveComment.description,
node,
range,
get loc() {
return sourceCode.getLoc(node)
},
})
}
}
return result
}
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(1)
: 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) {
if (
typeof sourceCode.getInlineConfigNodes === "function" &&
typeof sourceCode.getDisableDirectives === "function"
) {
result =
getAllDirectiveCommentsFromInlineConfigNodes(sourceCode)
} else {
result = getAllDirectiveCommentsFromAllComments(sourceCode)
}
pool.set(sourceCode.ast, result)
}
return result
},
}