Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default-rule implemented with parsers.all #2491

Draft
wants to merge 14 commits into
base: parsers
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"mocha": "^3.5.3",
"npm-which": "^3.0.1",
"nyc": "^11.9.0",
"object.fromentries": "^2.0.5",
"redux": "^3.7.2",
"rimraf": "^2.7.1",
"safe-publish-latest": "^2.0.0",
Expand Down
158 changes: 158 additions & 0 deletions tests/src/parsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
'use strict';

// const path = require('path');
const semver = require('semver');
const version = require('eslint/package.json').version;
const flatMap = require('array.prototype.flatmap');
const tsParserVersion = require('@typescript-eslint/parser/package.json').version;

const disableNewTS = semver.satisfies(tsParserVersion, '>= 4.1') // this rule is not useful on v4.1+ of the TS parser
? (x) => Object.assign({}, x, { features: [].concat(x.features, 'no-ts-new') })
: (x) => x;

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

// TODO: consolidate
import { parsers as utilsParsers } from './utils';

const parsers = {
...utilsParsers,
disableNewTS,
babelParserOptions: function parserOptions(test, features) {
return Object.assign({}, test.parserOptions, {
requireConfigFile: false,
babelOptions: {
presets: [
'@babel/preset-react',
],
plugins: [
'@babel/plugin-syntax-do-expressions',
'@babel/plugin-syntax-function-bind',
['@babel/plugin-syntax-decorators', { legacy: true }],
],
parserOpts: {
allowSuperOutsideMethod: false,
allowReturnOutsideFunction: false,
},
},
ecmaFeatures: Object.assign(
{},
test.parserOptions && test.parserOptions.ecmaFeatures,
{
jsx: true,
modules: true,
legacyDecorators: features.has('decorators'),
},
),
});
},
all: function all(tests) {
const t = flatMap(tests, (test) => {
if (typeof test === 'string') {
test = { code: test };
}
if ('parser' in test) {
delete test.features;
return test;
}
const features = new Set([].concat(test.features || []));
delete test.features;
const es = test.parserOptions && test.parserOptions.ecmaVersion;

function addComment(testObject, parser) {
const extras = [].concat(
`features: [${Array.from(features).join(',')}]`,
`parser: ${parser}`,
testObject.parserOptions ? `parserOptions: ${JSON.stringify(testObject.parserOptions)}` : [],
testObject.options ? `options: ${JSON.stringify(testObject.options)}` : [],
testObject.settings ? `settings: ${JSON.stringify(testObject.settings)}` : [],
);

const extraComment = `\n// ${extras.join(', ')}`;

// Augment expected fix code output with extraComment
const nextCode = { code: testObject.code + extraComment };
const nextOutput = testObject.output && { output: testObject.output + extraComment };

// Augment expected suggestion outputs with extraComment
// `errors` may be a number (expected number of errors) or an array of
// error objects.
const nextErrors = testObject.errors
&& typeof testObject.errors !== 'number'
&& {
errors: testObject.errors.map(
(errorObject) => {
const nextSuggestions = errorObject.suggestions && {
suggestions: errorObject.suggestions.map((suggestion) => Object.assign({}, suggestion, {
output: suggestion.output + extraComment,
})),
};

return Object.assign({}, errorObject, nextSuggestions);
},
),
};

return Object.assign(
{},
testObject,
nextCode,
nextOutput,
nextErrors,
);
}

const skipBase = (features.has('class fields') && semver.satisfies(version, '< 8'))
|| (es >= 2020 && semver.satisfies(version, '< 6'))
|| features.has('no-default')
|| features.has('bind operator')
|| features.has('do expressions')
|| features.has('decorators')
|| features.has('flow')
|| features.has('ts')
|| features.has('types')
|| (features.has('fragment') && semver.satisfies(version, '< 5'));

const skipBabel = features.has('no-babel');
const skipOldBabel = skipBabel || features.has('no-babel-old') || semver.satisfies(version, '>= 8');
const skipNewBabel = skipBabel
|| features.has('no-babel-new')
|| !semver.satisfies(version, '^7.5.0') // require('@babel/eslint-parser/package.json').peerDependencies.eslint
|| features.has('flow')
|| features.has('types')
|| features.has('ts');
const skipTS = semver.satisfies(version, '<= 5') // TODO: make these pass on eslint 5
|| features.has('no-ts')
|| features.has('flow')
|| features.has('jsx namespace')
|| features.has('bind operator')
|| features.has('do expressions');
const tsOld = !skipTS && !features.has('no-ts-old');
const tsNew = !skipTS && !features.has('no-ts-new');

return [].concat(
skipBase ? [] : addComment(
Object.assign({}, test, features.has('class fields') && {
parserOptions: Object.assign({}, test.parserOptions, {
ecmaVersion: Math.max((test.parserOptions && test.parserOptions.ecmaVersion) || 0, 2022),
}),
}),
'default',
),
skipOldBabel ? [] : addComment(Object.assign({}, test, {
parser: parsers.BABEL_ESLINT,
parserOptions: parsers.babelParserOptions(test, features),
}), 'babel-eslint'),
skipNewBabel ? [] : addComment(Object.assign({}, test, {
parser: parsers['@BABEL_ESLINT'],
parserOptions: parsers.babelParserOptions(test, features),
}), '@babel/eslint-parser'),
tsOld ? addComment(Object.assign({}, test, { parser: parsers.TYPESCRIPT_ESLINT }), 'typescript-eslint') : [],
tsNew ? addComment(Object.assign({}, test, { parser: parsers['@TYPESCRIPT_ESLINT'] }), '@typescript-eslint/parser') : [],
);
});
return t;
},
};

module.exports = parsers;
Loading