Skip to content

Commit 3b38018

Browse files
authored
feat: allow to define eslint-disable-next-line in multiple lines (#15436)
* feat: `eslint-disable-next-line` directive with multiple lines * test: add cases for multiline `disable-next-line` directives * test: add more cases * fix: correct reporting location for unused disable directives * docs: add examples for multiline eslint-disable-next-line directive * fix: logic * refactor: code * refactor: rename `position` to `unprocessedDirective`
1 parent 51c37b1 commit 3b38018

File tree

5 files changed

+338
-27
lines changed

5 files changed

+338
-27
lines changed

docs/user-guide/configuring/rules.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ alert('foo'); /* eslint-disable-line no-alert, quotes, semi */
200200
201201
/* eslint-disable-next-line no-alert, quotes, semi */
202202
alert('foo');
203+
204+
/* eslint-disable-next-line
205+
no-alert,
206+
quotes,
207+
semi
208+
*/
209+
alert('foo');
203210
```
204211

205212
All of the above methods also work for plugin rules. For example, to disable `eslint-plugin-example`'s `rule-name` rule, combine the plugin's name (`example`) and the rule's name (`rule-name`) into `example/rule-name`:
@@ -214,6 +221,12 @@ Configuration comments can include descriptions to explain why the comment is ne
214221
```js
215222
// eslint-disable-next-line no-console -- Here's a description about why this configuration is necessary.
216223
console.log('hello');
224+
225+
/* eslint-disable-next-line no-console --
226+
* Here's a very long description about why this configuration is necessary
227+
* along with some additional information
228+
**/
229+
console.log('hello');
217230
```
218231

219232
**Note:** Comments that disable warnings for a portion of a file tell ESLint not to report rule violations for the disabled code. ESLint still parses the entire file, however, so disabled code still needs to be syntactically valid JavaScript.

lib/linter/apply-disable-directives.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function groupByParentComment(directives) {
4343
* Creates removal details for a set of directives within the same comment.
4444
* @param {Directive[]} directives Unused directives to be removed.
4545
* @param {Token} commentToken The backing Comment token.
46-
* @returns {{ description, fix, position }[]} Details for later creation of output Problems.
46+
* @returns {{ description, fix, unprocessedDirective }[]} Details for later creation of output Problems.
4747
*/
4848
function createIndividualDirectivesRemoval(directives, commentToken) {
4949

@@ -138,7 +138,7 @@ function createIndividualDirectivesRemoval(directives, commentToken) {
138138
],
139139
text: ""
140140
},
141-
position: directive.unprocessedDirective
141+
unprocessedDirective: directive.unprocessedDirective
142142
};
143143
});
144144
}
@@ -147,7 +147,7 @@ function createIndividualDirectivesRemoval(directives, commentToken) {
147147
* Creates a description of deleting an entire unused disable comment.
148148
* @param {Directive[]} directives Unused directives to be removed.
149149
* @param {Token} commentToken The backing Comment token.
150-
* @returns {{ description, fix, position }} Details for later creation of an output Problem.
150+
* @returns {{ description, fix, unprocessedDirective }} Details for later creation of an output Problem.
151151
*/
152152
function createCommentRemoval(directives, commentToken) {
153153
const { range } = commentToken;
@@ -161,14 +161,14 @@ function createCommentRemoval(directives, commentToken) {
161161
range,
162162
text: " "
163163
},
164-
position: directives[0].unprocessedDirective
164+
unprocessedDirective: directives[0].unprocessedDirective
165165
};
166166
}
167167

168168
/**
169169
* Parses details from directives to create output Problems.
170170
* @param {Directive[]} allDirectives Unused directives to be removed.
171-
* @returns {{ description, fix, position }[]} Details for later creation of output Problems.
171+
* @returns {{ description, fix, unprocessedDirective }[]} Details for later creation of output Problems.
172172
*/
173173
function processUnusedDisableDirectives(allDirectives) {
174174
const directiveGroups = groupByParentComment(allDirectives);
@@ -261,17 +261,21 @@ function applyDirectives(options) {
261261
const processed = processUnusedDisableDirectives(unusedDisableDirectivesToReport);
262262

263263
const unusedDisableDirectives = processed
264-
.map(({ description, fix, position }) => ({
265-
ruleId: null,
266-
message: description
267-
? `Unused eslint-disable directive (no problems were reported from ${description}).`
268-
: "Unused eslint-disable directive (no problems were reported).",
269-
line: position.line,
270-
column: position.column,
271-
severity: options.reportUnusedDisableDirectives === "warn" ? 1 : 2,
272-
nodeType: null,
273-
...options.disableFixes ? {} : { fix }
274-
}));
264+
.map(({ description, fix, unprocessedDirective }) => {
265+
const { parentComment, type, line, column } = unprocessedDirective;
266+
267+
return {
268+
ruleId: null,
269+
message: description
270+
? `Unused eslint-disable directive (no problems were reported from ${description}).`
271+
: "Unused eslint-disable directive (no problems were reported).",
272+
line: type === "disable-next-line" ? parentComment.commentToken.loc.start.line : line,
273+
column: type === "disable-next-line" ? parentComment.commentToken.loc.start.column + 1 : column,
274+
severity: options.reportUnusedDisableDirectives === "warn" ? 1 : 2,
275+
nodeType: null,
276+
...options.disableFixes ? {} : { fix }
277+
};
278+
});
275279

276280
return { problems, unusedDisableDirectives };
277281
}

lib/linter/linter.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,11 @@ function createDisableDirectives(options) {
305305

306306
// push to directives, if the rule is defined(including null, e.g. /*eslint enable*/)
307307
if (ruleId === null || !!ruleMapper(ruleId)) {
308-
result.directives.push({ parentComment, type, line: commentToken.loc.start.line, column: commentToken.loc.start.column + 1, ruleId });
308+
if (type === "disable-next-line") {
309+
result.directives.push({ parentComment, type, line: commentToken.loc.end.line, column: commentToken.loc.end.column + 1, ruleId });
310+
} else {
311+
result.directives.push({ parentComment, type, line: commentToken.loc.start.line, column: commentToken.loc.start.column + 1, ruleId });
312+
}
309313
} else {
310314
result.directiveProblems.push(createLintingProblem({ ruleId, loc: commentToken.loc }));
311315
}
@@ -368,7 +372,7 @@ function getDirectiveComments(ast, ruleMapper, warnInlineConfig) {
368372
return;
369373
}
370374

371-
if (lineCommentSupported && comment.loc.start.line !== comment.loc.end.line) {
375+
if (directiveText === "eslint-disable-line" && comment.loc.start.line !== comment.loc.end.line) {
372376
const message = `${directiveText} comment should not span multiple lines.`;
373377

374378
problems.push(createLintingProblem({

tests/lib/linter/apply-disable-directives.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,20 @@ const applyDisableDirectives = require("../../../lib/linter/apply-disable-direct
2525
*/
2626
function createParentComment(range, value, ruleIds = []) {
2727
return {
28-
commentToken: { range, value },
28+
commentToken: {
29+
range,
30+
loc: {
31+
start: {
32+
line: 1,
33+
column: 1
34+
},
35+
end: {
36+
line: 1,
37+
column: value ? value.length : 10
38+
}
39+
},
40+
value
41+
},
2942
ruleIds
3043
};
3144
}
@@ -1294,7 +1307,7 @@ describe("apply-disable-directives", () => {
12941307
parentComment: createParentComment([0, 27]),
12951308
type: "disable-next-line",
12961309
line: 1,
1297-
column: 1,
1310+
column: 2,
12981311
ruleId: null
12991312
}],
13001313
problems: [],
@@ -1305,7 +1318,7 @@ describe("apply-disable-directives", () => {
13051318
ruleId: null,
13061319
message: "Unused eslint-disable directive (no problems were reported).",
13071320
line: 1,
1308-
column: 1,
1321+
column: 2,
13091322
fix: {
13101323
range: [0, 27],
13111324
text: " "

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy