2
2
3
3
const path = require ( 'path' ) ;
4
4
const semver = require ( 'semver' ) ;
5
+ const entries = require ( 'object.entries' ) ;
5
6
const version = require ( 'eslint/package.json' ) . version ;
6
7
const flatMap = require ( 'array.prototype.flatmap' ) ;
7
8
const tsParserVersion = require ( '@typescript-eslint/parser/package.json' ) . version ;
@@ -10,6 +11,25 @@ const disableNewTS = semver.satisfies(tsParserVersion, '>= 4.1') // this rule is
10
11
? ( x ) => Object . assign ( { } , x , { features : [ ] . concat ( x . features , 'no-ts-new' ) } )
11
12
: ( x ) => x ;
12
13
14
+ function minEcmaVersion ( features , parserOptions ) {
15
+ const minEcmaVersionForFeatures = {
16
+ 'class fields' : 2022 ,
17
+ 'optional chaining' : 2020 ,
18
+ } ;
19
+ const result = Math . max . apply (
20
+ Math ,
21
+ [ ] . concat (
22
+ ( parserOptions && parserOptions . ecmaVersion ) || [ ] ,
23
+ flatMap ( entries ( minEcmaVersionForFeatures ) , ( entry ) => {
24
+ const f = entry [ 0 ] ;
25
+ const y = entry [ 1 ] ;
26
+ return features . has ( f ) ? y : [ ] ;
27
+ } )
28
+ ) . map ( ( y ) => ( y > 5 && y < 2015 ? y + 2009 : y ) ) // normalize editions to years
29
+ ) ;
30
+ return Number . isFinite ( result ) ? result : undefined ;
31
+ }
32
+
13
33
const NODE_MODULES = '../../node_modules' ;
14
34
15
35
const parsers = {
@@ -58,7 +78,7 @@ const parsers = {
58
78
const features = new Set ( [ ] . concat ( test . features || [ ] ) ) ;
59
79
delete test . features ;
60
80
61
- const es = features . has ( 'class fields' ) ? 2022 : ( features . has ( 'optional chaining' ) ? 2020 : ( test . parserOptions && test . parserOptions . ecmaVersion ) || undefined ) ; // eslint-disable-line no-nested-ternary
81
+ const es = minEcmaVersion ( features , test . parserOptions ) ;
62
82
63
83
function addComment ( testObject , parser ) {
64
84
const extras = [ ] . concat (
@@ -133,10 +153,8 @@ const parsers = {
133
153
134
154
return [ ] . concat (
135
155
skipBase ? [ ] : addComment (
136
- Object . assign ( { } , test , typeof es !== 'undefined' && {
137
- parserOptions : Object . assign ( { } , test . parserOptions , {
138
- ecmaVersion : Math . max ( ( test . parserOptions && test . parserOptions . ecmaVersion ) || 0 , es ) ,
139
- } ) ,
156
+ Object . assign ( { } , test , typeof es === 'number' && {
157
+ parserOptions : Object . assign ( { } , test . parserOptions , { ecmaVersion : es } ) ,
140
158
} ) ,
141
159
'default'
142
160
) ,
0 commit comments