Skip to content

Commit 9139830

Browse files
committed
[Tests] parsers.all: improve min ecmaVersion logic
1 parent 6b42731 commit 9139830

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

tests/helpers/parsers.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const path = require('path');
44
const semver = require('semver');
5+
const entries = require('object.entries');
56
const version = require('eslint/package.json').version;
67
const flatMap = require('array.prototype.flatmap');
78
const tsParserVersion = require('@typescript-eslint/parser/package.json').version;
@@ -10,6 +11,25 @@ const disableNewTS = semver.satisfies(tsParserVersion, '>= 4.1') // this rule is
1011
? (x) => Object.assign({}, x, { features: [].concat(x.features, 'no-ts-new') })
1112
: (x) => x;
1213

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+
1333
const NODE_MODULES = '../../node_modules';
1434

1535
const parsers = {
@@ -58,7 +78,7 @@ const parsers = {
5878
const features = new Set([].concat(test.features || []));
5979
delete test.features;
6080

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);
6282

6383
function addComment(testObject, parser) {
6484
const extras = [].concat(
@@ -133,10 +153,8 @@ const parsers = {
133153

134154
return [].concat(
135155
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 }),
140158
}),
141159
'default'
142160
),

0 commit comments

Comments
 (0)