Skip to content

Commit 319f0a5

Browse files
authored
feat: use context.languageOptions.ecmaVersion in core rules (#16458)
Updates core rules to use `context.languageOptions.ecmaVersion` instead of `context.parserOptions.ecmaVersion` Fixes #16442
1 parent 8a15968 commit 319f0a5

8 files changed

Lines changed: 224 additions & 17 deletions

lib/rules/comma-dangle.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function normalizeOptions(optionValue, ecmaVersion) {
5050
objects: optionValue,
5151
imports: optionValue,
5252
exports: optionValue,
53-
functions: (!ecmaVersion || ecmaVersion < 8) ? "ignore" : optionValue
53+
functions: ecmaVersion < 2017 ? "ignore" : optionValue
5454
};
5555
}
5656
if (typeof optionValue === "object" && optionValue !== null) {
@@ -134,7 +134,7 @@ module.exports = {
134134
},
135135

136136
create(context) {
137-
const options = normalizeOptions(context.options[0], context.parserOptions.ecmaVersion);
137+
const options = normalizeOptions(context.options[0], context.languageOptions.ecmaVersion);
138138

139139
const sourceCode = context.getSourceCode();
140140

lib/rules/func-name-matching.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function isModuleExports(pattern) {
4444
* @returns {boolean} True if the string is a valid identifier
4545
*/
4646
function isIdentifier(name, ecmaVersion) {
47-
if (ecmaVersion >= 6) {
47+
if (ecmaVersion >= 2015) {
4848
return esutils.keyword.isIdentifierES6(name);
4949
}
5050
return esutils.keyword.isIdentifierES5(name);
@@ -104,7 +104,7 @@ module.exports = {
104104
const nameMatches = typeof context.options[0] === "string" ? context.options[0] : "always";
105105
const considerPropertyDescriptor = options.considerPropertyDescriptor;
106106
const includeModuleExports = options.includeCommonJSModuleExports;
107-
const ecmaVersion = context.parserOptions && context.parserOptions.ecmaVersion ? context.parserOptions.ecmaVersion : 5;
107+
const ecmaVersion = context.languageOptions.ecmaVersion;
108108

109109
/**
110110
* Check whether node is a certain CallExpression.

lib/rules/no-misleading-character-class.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,15 @@ module.exports = {
193193
* ecmaVersion doesn't support the `u` flag.
194194
*/
195195
function isValidWithUnicodeFlag(pattern) {
196-
const { ecmaVersion } = context.parserOptions;
196+
const { ecmaVersion } = context.languageOptions;
197197

198-
// ecmaVersion is unknown or it doesn't support the 'u' flag
199-
if (typeof ecmaVersion !== "number" || ecmaVersion <= 5) {
198+
// ecmaVersion <= 5 doesn't support the 'u' flag
199+
if (ecmaVersion <= 5) {
200200
return false;
201201
}
202202

203203
const validator = new RegExpValidator({
204-
ecmaVersion: Math.min(ecmaVersion + 2009, REGEXPP_LATEST_ECMA_VERSION)
204+
ecmaVersion: Math.min(ecmaVersion, REGEXPP_LATEST_ECMA_VERSION)
205205
});
206206

207207
try {

lib/rules/prefer-regex-literals.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,14 @@ module.exports = {
248248

249249
/**
250250
* Returns a ecmaVersion compatible for regexpp.
251-
* @param {any} ecmaVersion The ecmaVersion to convert.
251+
* @param {number} ecmaVersion The ecmaVersion to convert.
252252
* @returns {import("regexpp/ecma-versions").EcmaVersion} The resulting ecmaVersion compatible for regexpp.
253253
*/
254254
function getRegexppEcmaVersion(ecmaVersion) {
255-
if (typeof ecmaVersion !== "number" || ecmaVersion <= 5) {
255+
if (ecmaVersion <= 5) {
256256
return 5;
257257
}
258-
return Math.min(ecmaVersion + 2009, REGEXPP_LATEST_ECMA_VERSION);
258+
return Math.min(ecmaVersion, REGEXPP_LATEST_ECMA_VERSION);
259259
}
260260

261261
/**
@@ -320,7 +320,7 @@ module.exports = {
320320
flags = getStringValue(node.arguments[1]);
321321
}
322322

323-
const regexppEcmaVersion = getRegexppEcmaVersion(context.parserOptions.ecmaVersion);
323+
const regexppEcmaVersion = getRegexppEcmaVersion(context.languageOptions.ecmaVersion);
324324
const RegExpValidatorInstance = new RegExpValidator({ ecmaVersion: regexppEcmaVersion });
325325

326326
try {

tests/lib/rules/comma-dangle.js

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
const path = require("path"),
1313
{ unIndent } = require("../../_utils"),
1414
rule = require("../../../lib/rules/comma-dangle"),
15-
{ RuleTester } = require("../../../lib/rule-tester");
15+
{ RuleTester } = require("../../../lib/rule-tester"),
16+
FlatRuleTester = require("../../../lib/rule-tester/flat-rule-tester");
1617

1718
//------------------------------------------------------------------------------
1819
// Helpers
@@ -279,6 +280,14 @@ ruleTester.run("comma-dangle", rule, {
279280
code: "function foo(a,\nb) {}",
280281
options: ["always-multiline"]
281282
},
283+
{
284+
code: "foo(a,\nb\n)",
285+
options: ["always-multiline"]
286+
},
287+
{
288+
code: "function foo(a,\nb\n) {}",
289+
options: ["always-multiline"]
290+
},
282291
{
283292
code: "foo(a,\nb)",
284293
options: ["always-multiline"]
@@ -321,6 +330,16 @@ ruleTester.run("comma-dangle", rule, {
321330
options: ["always-multiline"],
322331
parserOptions: { ecmaVersion: 7 }
323332
},
333+
{
334+
code: "function foo(a,\nb\n) {}",
335+
options: ["always-multiline"],
336+
parserOptions: { ecmaVersion: 7 }
337+
},
338+
{
339+
code: "foo(a,\nb\n)",
340+
options: ["always-multiline"],
341+
parserOptions: { ecmaVersion: 7 }
342+
},
324343
{
325344
code: "function foo(a,\nb) {}",
326345
options: ["only-multiline"],
@@ -1853,3 +1872,100 @@ let d = 0;export {d,};
18531872
}
18541873
]
18551874
});
1875+
1876+
const flatRuleTester = new FlatRuleTester();
1877+
1878+
// https://github.com/eslint/eslint/issues/16442
1879+
flatRuleTester.run("comma-dangle", rule, {
1880+
valid: [
1881+
{
1882+
code: "function f(\n a,\n b\n) {}",
1883+
options: ["always-multiline"],
1884+
languageOptions: {
1885+
ecmaVersion: 5,
1886+
sourceType: "script"
1887+
}
1888+
},
1889+
{
1890+
code: "f(\n a,\n b\n);",
1891+
options: ["always-multiline"],
1892+
languageOptions: {
1893+
ecmaVersion: 5,
1894+
sourceType: "script"
1895+
}
1896+
},
1897+
{
1898+
code: "function f(\n a,\n b\n) {}",
1899+
options: ["always-multiline"],
1900+
languageOptions: {
1901+
ecmaVersion: 2016
1902+
}
1903+
},
1904+
{
1905+
code: "f(\n a,\n b\n);",
1906+
options: ["always-multiline"],
1907+
languageOptions: {
1908+
ecmaVersion: 2016
1909+
}
1910+
}
1911+
],
1912+
1913+
invalid: [
1914+
{
1915+
code: "function f(\n a,\n b\n) {}",
1916+
output: "function f(\n a,\n b,\n) {}",
1917+
options: ["always-multiline"],
1918+
languageOptions: {
1919+
ecmaVersion: 2017
1920+
},
1921+
errors: [{
1922+
messageId: "missing",
1923+
type: "Identifier",
1924+
line: 3,
1925+
column: 3
1926+
}]
1927+
},
1928+
{
1929+
code: "f(\n a,\n b\n);",
1930+
output: "f(\n a,\n b,\n);",
1931+
options: ["always-multiline"],
1932+
languageOptions: {
1933+
ecmaVersion: 2017
1934+
},
1935+
errors: [{
1936+
messageId: "missing",
1937+
type: "Identifier",
1938+
line: 3,
1939+
column: 3
1940+
}]
1941+
},
1942+
{
1943+
code: "function f(\n a,\n b\n) {}",
1944+
output: "function f(\n a,\n b,\n) {}",
1945+
options: ["always-multiline"],
1946+
languageOptions: {
1947+
ecmaVersion: "latest"
1948+
},
1949+
errors: [{
1950+
messageId: "missing",
1951+
type: "Identifier",
1952+
line: 3,
1953+
column: 3
1954+
}]
1955+
},
1956+
{
1957+
code: "f(\n a,\n b\n);",
1958+
output: "f(\n a,\n b,\n);",
1959+
options: ["always-multiline"],
1960+
languageOptions: {
1961+
ecmaVersion: "latest"
1962+
},
1963+
errors: [{
1964+
messageId: "missing",
1965+
type: "Identifier",
1966+
line: 3,
1967+
column: 3
1968+
}]
1969+
}
1970+
]
1971+
});

tests/lib/rules/func-name-matching.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
//------------------------------------------------------------------------------
1111

1212
const rule = require("../../../lib/rules/func-name-matching"),
13-
{ RuleTester } = require("../../../lib/rule-tester");
13+
{ RuleTester } = require("../../../lib/rule-tester"),
14+
FlatRuleTester = require("../../../lib/rule-tester/flat-rule-tester");
1415

1516
//------------------------------------------------------------------------------
1617
// Tests
@@ -504,7 +505,8 @@ ruleTester.run("func-name-matching", rule, {
504505
code: "class C { #x; foo() { a.b.#x = function y() {}; } }",
505506
options: ["never"],
506507
parserOptions: { ecmaVersion: 2022 }
507-
}
508+
},
509+
"var obj = { '\\u1885': function foo() {} };" // not a valid identifier in es5
508510
],
509511
invalid: [
510512
{
@@ -878,6 +880,39 @@ ruleTester.run("func-name-matching", rule, {
878880
errors: [
879881
{ messageId: "notMatchProperty", data: { funcName: "x", name: "x" } }
880882
]
883+
},
884+
{
885+
code: "var obj = { '\\u1885': function foo() {} };", // valid identifier in es2015
886+
parserOptions: { ecmaVersion: 6 },
887+
errors: [
888+
{ messageId: "matchProperty", data: { funcName: "foo", name: "\u1885" } }
889+
]
890+
}
891+
]
892+
});
893+
894+
const flatRuleTester = new FlatRuleTester();
895+
896+
flatRuleTester.run("func-name-matching", rule, {
897+
valid: [
898+
{
899+
code: "var obj = { '\\u1885': function foo() {} };", // not a valid identifier in es5
900+
languageOptions: {
901+
ecmaVersion: 5,
902+
sourceType: "script"
903+
}
904+
}
905+
],
906+
907+
invalid: [
908+
{
909+
code: "var obj = { '\\u1885': function foo() {} };", // valid identifier in es2015
910+
languageOptions: {
911+
ecmaVersion: 2015
912+
},
913+
errors: [
914+
{ messageId: "matchProperty", data: { funcName: "foo", name: "\u1885" } }
915+
]
881916
}
882917
]
883918
});

tests/lib/rules/no-misleading-character-class.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
//------------------------------------------------------------------------------
1010

1111
const rule = require("../../../lib/rules/no-misleading-character-class"),
12-
{ RuleTester } = require("../../../lib/rule-tester");
12+
{ RuleTester } = require("../../../lib/rule-tester"),
13+
FlatRuleTester = require("../../../lib/rule-tester/flat-rule-tester");
1314

1415
//------------------------------------------------------------------------------
1516
// Tests
@@ -622,3 +623,33 @@ ruleTester.run("no-misleading-character-class", rule, {
622623
}
623624
]
624625
});
626+
627+
const flatRuleTester = new FlatRuleTester();
628+
629+
flatRuleTester.run("no-misleading-character-class", rule, {
630+
valid: [],
631+
632+
invalid: [
633+
{
634+
code: "var r = /[👍]/",
635+
languageOptions: {
636+
ecmaVersion: 5,
637+
sourceType: "script"
638+
},
639+
errors: [{
640+
messageId: "surrogatePairWithoutUFlag",
641+
suggestions: null // ecmaVersion doesn't support the 'u' flag
642+
}]
643+
},
644+
{
645+
code: "var r = /[👍]/",
646+
languageOptions: {
647+
ecmaVersion: 2015
648+
},
649+
errors: [{
650+
messageId: "surrogatePairWithoutUFlag",
651+
suggestions: [{ messageId: "suggestUnicodeFlag", output: "var r = /[👍]/u" }]
652+
}]
653+
}
654+
]
655+
});

tests/lib/rules/prefer-regex-literals.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
//------------------------------------------------------------------------------
1111

1212
const rule = require("../../../lib/rules/prefer-regex-literals");
13-
const { RuleTester } = require("../../../lib/rule-tester");
13+
const { RuleTester } = require("../../../lib/rule-tester"),
14+
FlatRuleTester = require("../../../lib/rule-tester/flat-rule-tester");
1415

1516
//------------------------------------------------------------------------------
1617
// Tests
@@ -2474,3 +2475,27 @@ ruleTester.run("prefer-regex-literals", rule, {
24742475
}
24752476
]
24762477
});
2478+
2479+
const flatRuleTester = new FlatRuleTester();
2480+
2481+
flatRuleTester.run("prefer-regex-literals", rule, {
2482+
valid: [],
2483+
2484+
invalid: [
2485+
{
2486+
code: "var regex = new RegExp('foo', 'u');",
2487+
languageOptions: {
2488+
ecmaVersion: 2015
2489+
},
2490+
errors: [{
2491+
messageId: "unexpectedRegExp",
2492+
suggestions: [
2493+
{
2494+
messageId: "replaceWithLiteral",
2495+
output: "var regex = /foo/u;"
2496+
}
2497+
]
2498+
}]
2499+
}
2500+
]
2501+
});

0 commit comments

Comments
 (0)