Skip to content

Commit 5705833

Browse files
authored
fix: warn when eslint-env configuration comments are found (#20381)
* feat: warn when `eslint-env` configuration comments are found * include filename and line in ESLintEnvWarning * fix: check if `language` is `jslang` * clarify version wording for eslint-env deprecation warning * use --no-warnings in check-rule-examples * restore missing blank line
1 parent 506f154 commit 5705833

7 files changed

Lines changed: 65 additions & 5 deletions

File tree

lib/linter/linter.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,20 @@ class Linter {
19211921
});
19221922
}
19231923
} else {
1924+
if (config.language === jslang) {
1925+
for (const comment of sourceCode.getInlineConfigNodes()) {
1926+
const { label } = commentParser.parseDirective(
1927+
comment.value,
1928+
);
1929+
if (label === "eslint-env") {
1930+
slots.warningService.emitESLintEnvWarning(
1931+
options.filename,
1932+
comment.loc.start.line,
1933+
);
1934+
}
1935+
}
1936+
}
1937+
19241938
const inlineConfigResult = sourceCode.applyInlineConfig?.();
19251939

19261940
if (inlineConfigResult) {

lib/services/warning-service.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ class WarningService {
9393
"ESLintPoorConcurrencyWarning",
9494
);
9595
}
96+
97+
/**
98+
* Emits a warning when eslint-env configuration comments are found.
99+
* @param {string} filename The name of the file being linted.
100+
* @param {number} line The line number of the comment.
101+
* @returns {void}
102+
*/
103+
emitESLintEnvWarning(filename, line) {
104+
this.emitWarning(
105+
`/* eslint-env */ comments are no longer recognized when linting with flat config and will be reported as errors as of v10.0.0. Replace them with /* global */ comments or define globals in your config file. See https://eslint.org/docs/latest/use/configure/migration-guide#eslint-env-configuration-comments for details. Found in ${filename} at line ${line}.`,
106+
"ESLintEnvWarning",
107+
);
108+
}
96109
}
97110

98111
module.exports = { WarningService };

tests/lib/linter/linter.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18257,6 +18257,25 @@ var a = "test2";
1825718257
}
1825818258
});
1825918259
});
18260+
18261+
describe("eslint-env comments", () => {
18262+
it("should emit a warning", () => {
18263+
const code = "/* eslint-env mocha */";
18264+
linter.verify(code);
18265+
18266+
assert(warningService.emitESLintEnvWarning.calledOnce);
18267+
});
18268+
18269+
it("should be ignored in inline comments", () => {
18270+
const code = "it('Test', () => {}); // eslint-env mocha";
18271+
linter.verify(code);
18272+
18273+
assert.strictEqual(
18274+
warningService.emitESLintEnvWarning.called,
18275+
false,
18276+
);
18277+
});
18278+
});
1826018279
});
1826118280

1826218281
describe("Default Global Variables", () => {

tests/lib/rules/capitalized-comments.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ ruleTester.run("capitalized-comments", rule, {
6060
"// eslint-disable-line",
6161
"// eslint-disable-next-line",
6262
"/* eslint semi:off */",
63-
"/* eslint-env node */",
63+
"/* eslint-enable */",
6464
"/* istanbul ignore next */",
6565
"/* jshint asi:true */",
6666
"/* jscs: enable */",
@@ -121,7 +121,7 @@ ruleTester.run("capitalized-comments", rule, {
121121
{ code: "// eslint-disable-line", options: ["always"] },
122122
{ code: "// eslint-disable-next-line", options: ["always"] },
123123
{ code: "/* eslint semi:off */", options: ["always"] },
124-
{ code: "/* eslint-env node */", options: ["always"] },
124+
{ code: "/* eslint-enable */", options: ["always"] },
125125
{ code: "/* istanbul ignore next */", options: ["always"] },
126126
{ code: "/* jshint asi:true */", options: ["always"] },
127127
{ code: "/* jscs: enable */", options: ["always"] },

tests/lib/rules/spaced-comment.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ ruleTester.run("spaced-comment", rule, {
202202
options: ["always", { markers: ["global"] }],
203203
},
204204
{
205-
code: "/*eslint-env node*/",
206-
options: ["always", { markers: ["eslint-env"] }],
205+
code: "/*eslint-disable-line unicode-bom*/",
206+
options: ["always", { markers: ["eslint-disable-line"] }],
207207
},
208208
{
209209
code: "/*eslint eqeqeq:0, curly: 2*/",

tests/lib/services/warning-service.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ describe("WarningService", () => {
111111
"Expected process.emitWarning to be called with the correct arguments",
112112
);
113113
});
114+
115+
it("emitESLintEnvWarning", () => {
116+
const filename = "/project/file.js";
117+
const line = 1;
118+
warningService.emitESLintEnvWarning(filename, line);
119+
120+
assert(
121+
process.emitWarning.calledOnceWithExactly(
122+
`/* eslint-env */ comments are no longer recognized when linting with flat config and will be reported as errors as of v10.0.0. Replace them with /* global */ comments or define globals in your config file. See https://eslint.org/docs/latest/use/configure/migration-guide#eslint-env-configuration-comments for details. Found in ${filename} at line ${line}.`,
123+
"ESLintEnvWarning",
124+
),
125+
"Expected process.emitWarning to be called with the correct arguments",
126+
);
127+
});
114128
});
115129

116130
describe("should not throw an error when `process` is not defined", () => {

tests/tools/check-rule-examples.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const { LATEST_ECMA_VERSION } = require("../../conf/ecma-version");
2222
async function runCheckRuleExamples(...filenames) {
2323
return await promisify(execFile)(
2424
process.execPath,
25-
["--no-deprecation", "tools/check-rule-examples.js", ...filenames],
25+
["--no-warnings", "tools/check-rule-examples.js", ...filenames],
2626
{ env: { FORCE_COLOR: "3" } }, // 24-bit color mode
2727
);
2828
}

0 commit comments

Comments
 (0)