Skip to content

Commit

Permalink
[Tests] parsers.all: improve min ecmaVersion logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 18, 2022
1 parent 6b42731 commit 9139830
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions tests/helpers/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

function minEcmaVersion(features, parserOptions) {
const minEcmaVersionForFeatures = {
'class fields': 2022,
'optional chaining': 2020,
};
const result = Math.max.apply(
Math,
[].concat(
(parserOptions && parserOptions.ecmaVersion) || [],
flatMap(entries(minEcmaVersionForFeatures), (entry) => {
const f = entry[0];
const y = entry[1];
return features.has(f) ? y : [];
})
).map((y) => (y > 5 && y < 2015 ? y + 2009 : y)) // normalize editions to years
);
return Number.isFinite(result) ? result : undefined;
}

const NODE_MODULES = '../../node_modules';

const parsers = {
Expand Down Expand Up @@ -58,7 +78,7 @@ const parsers = {
const features = new Set([].concat(test.features || []));
delete test.features;

const es = features.has('class fields') ? 2022 : (features.has('optional chaining') ? 2020 : (test.parserOptions && test.parserOptions.ecmaVersion) || undefined); // eslint-disable-line no-nested-ternary
const es = minEcmaVersion(features, test.parserOptions);

function addComment(testObject, parser) {
const extras = [].concat(
Expand Down Expand Up @@ -133,10 +153,8 @@ const parsers = {

return [].concat(
skipBase ? [] : addComment(
Object.assign({}, test, typeof es !== 'undefined' && {
parserOptions: Object.assign({}, test.parserOptions, {
ecmaVersion: Math.max((test.parserOptions && test.parserOptions.ecmaVersion) || 0, es),
}),
Object.assign({}, test, typeof es === 'number' && {
parserOptions: Object.assign({}, test.parserOptions, { ecmaVersion: es }),
}),
'default'
),
Expand Down

0 comments on commit 9139830

Please sign in to comment.