Skip to content

Commit 6835a10

Browse files
authoredDec 20, 2023
feat: Add syntax check for ES2021 to no-unsupported-features/es-syntax rule (#152)
the change was borrowed from mysticatea#283 credits goes to @ota-meshi
1 parent 6409e34 commit 6835a10

File tree

3 files changed

+129
-8
lines changed

3 files changed

+129
-8
lines changed
 

‎lib/rules/no-unsupported-features/es-syntax.js

+30
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,28 @@ const features = {
402402
},
403403
],
404404
},
405+
406+
//--------------------------------------------------------------------------
407+
// ES2021
408+
//--------------------------------------------------------------------------
409+
logicalAssignmentOperators: {
410+
ruleId: "no-logical-assignment-operators",
411+
cases: [
412+
{
413+
supported: "15.0.0",
414+
messageId: "no-logical-assignment-operators",
415+
},
416+
],
417+
},
418+
numericSeparators: {
419+
ruleId: "no-numeric-separators",
420+
cases: [
421+
{
422+
supported: "12.5.0",
423+
messageId: "no-numeric-separators",
424+
},
425+
],
426+
},
405427
}
406428
const keywords = Object.keys(features)
407429

@@ -654,6 +676,14 @@ module.exports = {
654676
"Optional chainings are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
655677
"no-nullish-coalescing-operators":
656678
"Nullish coalescing operators are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
679+
680+
//------------------------------------------------------------------
681+
// ES2021
682+
//------------------------------------------------------------------
683+
"no-logical-assignment-operators":
684+
"Logical assignment operators are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
685+
"no-numeric-separators":
686+
"Numeric separators are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
657687
},
658688
},
659689
create(context) {

‎tests/lib/rules/no-path-concat.js

-2
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,3 @@ new RuleTester({
169169
},
170170
],
171171
})
172-
173-
/*eslint-enable no-template-curly-in-string */

‎tests/lib/rules/no-unsupported-features/es-syntax.js

+99-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ const { builtin } = require("globals")
1010
const { Range } = require("semver")
1111
const rule = require("../../../../lib/rules/no-unsupported-features/es-syntax")
1212

13-
const ES2020Supported = (() => {
14-
const config = { parserOptions: { ecmaVersion: 2020 } }
13+
const ES2021Supported = (() => {
14+
const config = { parserOptions: { ecmaVersion: 2021 } }
1515
const messages = new Linter().verify("0n", config)
1616
return messages.length === 0
1717
})()
18-
const ecmaVersion = ES2020Supported ? 2020 : 2019
18+
const ES2020Supported =
19+
ES2021Supported ||
20+
(() => {
21+
const config = { parserOptions: { ecmaVersion: 2020 } }
22+
const messages = new Linter().verify("0n", config)
23+
return messages.length === 0
24+
})()
25+
const ecmaVersion = ES2021Supported ? 2021 : ES2020Supported ? 2020 : 2019
1926

2027
/**
2128
* Makes a file path to a fixture.
@@ -1553,7 +1560,6 @@ ruleTester.run(
15531560
],
15541561
},
15551562
],
1556-
/*eslint-enable no-template-curly-in-string */
15571563
},
15581564
{
15591565
keyword: "unicodeCodePointEscapes",
@@ -2592,6 +2598,93 @@ ruleTester.run(
25922598
],
25932599
},
25942600

2601+
//----------------------------------------------------------------------
2602+
// ES2021
2603+
//----------------------------------------------------------------------
2604+
{
2605+
keyword: "logicalAssignmentOperators",
2606+
requiredEcmaVersion: 2021,
2607+
valid: [
2608+
{
2609+
code: "a ||= b",
2610+
options: [{ version: "15.0.0" }],
2611+
},
2612+
{
2613+
code: "a &&= b",
2614+
options: [{ version: "15.0.0" }],
2615+
},
2616+
{
2617+
code: "a ??= b",
2618+
options: [{ version: "15.0.0" }],
2619+
},
2620+
],
2621+
invalid: [
2622+
{
2623+
code: "a ||= b",
2624+
options: [{ version: "14.0.0" }],
2625+
errors: [
2626+
{
2627+
messageId: "no-logical-assignment-operators",
2628+
data: {
2629+
supported: "15.0.0",
2630+
version: "14.0.0",
2631+
},
2632+
},
2633+
],
2634+
},
2635+
{
2636+
code: "a &&= b",
2637+
options: [{ version: "14.0.0" }],
2638+
errors: [
2639+
{
2640+
messageId: "no-logical-assignment-operators",
2641+
data: {
2642+
supported: "15.0.0",
2643+
version: "14.0.0",
2644+
},
2645+
},
2646+
],
2647+
},
2648+
{
2649+
code: "a ??= b",
2650+
options: [{ version: "14.0.0" }],
2651+
errors: [
2652+
{
2653+
messageId: "no-logical-assignment-operators",
2654+
data: {
2655+
supported: "15.0.0",
2656+
version: "14.0.0",
2657+
},
2658+
},
2659+
],
2660+
},
2661+
],
2662+
},
2663+
{
2664+
keyword: "numericSeparators",
2665+
requiredEcmaVersion: 2021,
2666+
valid: [
2667+
{
2668+
code: "a = 123_456_789",
2669+
options: [{ version: "12.5.0" }],
2670+
},
2671+
],
2672+
invalid: [
2673+
{
2674+
code: "a = 123_456_789",
2675+
options: [{ version: "12.4.0" }],
2676+
errors: [
2677+
{
2678+
messageId: "no-numeric-separators",
2679+
data: {
2680+
supported: "12.5.0",
2681+
version: "12.4.0",
2682+
},
2683+
},
2684+
],
2685+
},
2686+
],
2687+
},
25952688
//----------------------------------------------------------------------
25962689
// MISC
25972690
//----------------------------------------------------------------------
@@ -2663,7 +2756,7 @@ ruleTester.run(
26632756
{
26642757
filename: fixture("invalid/a.js"),
26652758
code: "var a = { ...obj }",
2666-
options: [{version: '>=8.0.0'}],
2759+
options: [{ version: ">=8.0.0" }],
26672760
errors: [
26682761
{
26692762
messageId: "no-rest-spread-properties",
@@ -2684,7 +2777,7 @@ ruleTester.run(
26842777
{
26852778
filename: fixture("nothing/a.js"),
26862779
code: "var a = { ...obj }",
2687-
options: [{version: '>=8.0.0'}],
2780+
options: [{ version: ">=8.0.0" }],
26882781
errors: [
26892782
{
26902783
messageId: "no-rest-spread-properties",

0 commit comments

Comments
 (0)
Please sign in to comment.