Skip to content

Commit 343f992

Browse files
authored
refactor: don't use node.value when removing unused directives (#18835)
1 parent 183b459 commit 343f992

3 files changed

Lines changed: 128 additions & 94 deletions

File tree

lib/linter/apply-disable-directives.js

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,23 @@ function groupByParentDirective(directives) {
6060
/**
6161
* Creates removal details for a set of directives within the same comment.
6262
* @param {Directive[]} directives Unused directives to be removed.
63-
* @param {Token} node The backing Comment token.
63+
* @param {{node: Token, value: string}} parentDirective Data about the backing directive.
6464
* @param {SourceCode} sourceCode The source code object for the file being linted.
6565
* @returns {{ description, fix, unprocessedDirective }[]} Details for later creation of output Problems.
6666
*/
67-
function createIndividualDirectivesRemoval(directives, node, sourceCode) {
68-
69-
const range = sourceCode.getRange(node);
70-
71-
/*
72-
* `node.value` starts right after `//` or `/*`.
73-
* All calculated offsets will be relative to this index.
74-
*/
75-
const commentValueStart = range[0] + "//".length;
76-
77-
// Find where the list of rules starts. `\S+` matches with the directive name (e.g. `eslint-disable-line`)
78-
const listStartOffset = /^\s*\S+\s+/u.exec(node.value)[0].length;
67+
function createIndividualDirectivesRemoval(directives, parentDirective, sourceCode) {
7968

8069
/*
81-
* Get the list text without any surrounding whitespace. In order to preserve the original
70+
* Get the list of the rules text without any surrounding whitespace. In order to preserve the original
8271
* formatting, we don't want to change that whitespace.
8372
*
8473
* // eslint-disable-line rule-one , rule-two , rule-three -- comment
8574
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8675
*/
87-
const listText = node.value
88-
.slice(listStartOffset) // remove directive name and all whitespace before the list
89-
.split(/\s-{2,}\s/u)[0] // remove `-- comment`, if it exists
90-
.trimEnd(); // remove all whitespace after the list
76+
const listText = parentDirective.value.trim();
77+
78+
// Calculate where it starts in the source code text
79+
const listStart = sourceCode.text.indexOf(listText, sourceCode.getRange(parentDirective.node)[0]);
9180

9281
/*
9382
* We can assume that `listText` contains multiple elements.
@@ -101,13 +90,13 @@ function createIndividualDirectivesRemoval(directives, node, sourceCode) {
10190
const regex = new RegExp(String.raw`(?:^|\s*,\s*)(?<quote>['"]?)${escapeRegExp(ruleId)}\k<quote>(?:\s*,\s*|$)`, "u");
10291
const match = regex.exec(listText);
10392
const matchedText = match[0];
104-
const matchStartOffset = listStartOffset + match.index;
105-
const matchEndOffset = matchStartOffset + matchedText.length;
93+
const matchStart = listStart + match.index;
94+
const matchEnd = matchStart + matchedText.length;
10695

10796
const firstIndexOfComma = matchedText.indexOf(",");
10897
const lastIndexOfComma = matchedText.lastIndexOf(",");
10998

110-
let removalStartOffset, removalEndOffset;
99+
let removalStart, removalEnd;
111100

112101
if (firstIndexOfComma !== lastIndexOfComma) {
113102

@@ -123,8 +112,8 @@ function createIndividualDirectivesRemoval(directives, node, sourceCode) {
123112
* // eslint-disable-line rule-one , rule-two , rule-three -- comment
124113
* ^^^^^^^^^^^
125114
*/
126-
removalStartOffset = matchStartOffset + firstIndexOfComma;
127-
removalEndOffset = matchStartOffset + lastIndexOfComma;
115+
removalStart = matchStart + firstIndexOfComma;
116+
removalEnd = matchStart + lastIndexOfComma;
128117

129118
} else {
130119

@@ -146,16 +135,16 @@ function createIndividualDirectivesRemoval(directives, node, sourceCode) {
146135
* // eslint-disable-line rule-one , rule-two , rule-three -- comment
147136
* ^^^^^^^^^^^^^
148137
*/
149-
removalStartOffset = matchStartOffset;
150-
removalEndOffset = matchEndOffset;
138+
removalStart = matchStart;
139+
removalEnd = matchEnd;
151140
}
152141

153142
return {
154143
description: `'${ruleId}'`,
155144
fix: {
156145
range: [
157-
commentValueStart + removalStartOffset,
158-
commentValueStart + removalEndOffset
146+
removalStart,
147+
removalEnd
159148
],
160149
text: ""
161150
},
@@ -206,7 +195,7 @@ function processUnusedDirectives(allDirectives, sourceCode) {
206195
}
207196

208197
return remainingRuleIds.size
209-
? createIndividualDirectivesRemoval(directives, parentDirective.node, sourceCode)
198+
? createIndividualDirectivesRemoval(directives, parentDirective, sourceCode)
210199
: [createDirectiveRemoval(directives, parentDirective.node, sourceCode)];
211200
}
212201
);

lib/linter/linter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ function createDisableDirectives({ type, value, justification, node }, ruleMappe
332332
directives: [], // valid disable directives
333333
directiveProblems: [] // problems in directives
334334
};
335-
const parentDirective = { node, ruleIds };
335+
const parentDirective = { node, value, ruleIds };
336336

337337
for (const ruleId of directiveRules) {
338338

0 commit comments

Comments
 (0)