Skip to content

Commit

Permalink
[Tests] test on the new babel eslint parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Oct 25, 2021
1 parent 4bc3499 commit a8ae170
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`no-unstable-components`]: improve handling of objects containing render function properties ([#3111] @fizwidget)
* [`prop-types`], `propTypes`: add forwardRef<>, ForwardRefRenderFunction<> prop-types ([#3112] @vedadeepta)

### Changed
* [Tests] test on the new babel eslint parser ([#3113] @ljharb)

[#3113]: https://github.com/yannickcr/eslint-plugin-react/pull/3113
[#3112]: https://github.com/yannickcr/eslint-plugin-react/pull/3112
[#3111]: https://github.com/yannickcr/eslint-plugin-react/pull/3111
[#3110]: https://github.com/yannickcr/eslint-plugin-react/pull/3110
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
"string.prototype.matchall": "^4.0.6"
},
"devDependencies": {
"@babel/core": "^7.15.8",
"@babel/eslint-parser": "^7.15.8",
"@babel/plugin-syntax-decorators": "^7.14.5",
"@babel/plugin-syntax-do-expressions": "^7.14.5",
"@babel/plugin-syntax-function-bind": "^7.14.5",
"@babel/preset-react": "^7.14.5",
"@types/eslint": "=7.2.10",
"@types/estree": "^0.0.50",
"@types/node": "^14.17.27",
Expand Down
35 changes: 33 additions & 2 deletions tests/helpers/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,33 @@ const NODE_MODULES = '../../node_modules';

const parsers = {
BABEL_ESLINT: path.join(__dirname, NODE_MODULES, 'babel-eslint'),
'@BABEL_ESLINT': path.join(__dirname, NODE_MODULES, '@babel/eslint-parser'),
TYPESCRIPT_ESLINT: path.join(__dirname, NODE_MODULES, 'typescript-eslint-parser'),
'@TYPESCRIPT_ESLINT': path.join(__dirname, NODE_MODULES, '@typescript-eslint/parser'),
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 }],
],
},
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') {
Expand All @@ -25,7 +50,7 @@ const parsers = {
const es = test.parserOptions && test.parserOptions.ecmaVersion;

function addComment(testObject, parser) {
const extraComment = `\n// features: [${Array.from(features).join(',')}], parser: ${parser}`;
const extraComment = `\n// features: [${Array.from(features).join(',')}], parser: ${parser}, parserOptions: ${testObject.parserOptions}`;
return Object.assign(
{},
testObject,
Expand All @@ -46,6 +71,8 @@ const parsers = {
|| (features.has('fragment') && semver.satisfies(version, '< 5'));

const skipBabel = features.has('no-babel');
const skipOldBabel = skipBabel || semver.satisfies(version, '>= 8');
const skipNewBabel = skipBabel || features.has('types') || features.has('flow') || features.has('ts') || features.has('no-babel-new');
const skipTS = semver.satisfies(version, '< 5')
|| features.has('flow')
|| features.has('no-ts')
Expand All @@ -57,7 +84,11 @@ const parsers = {

return [].concat(
skipBase ? [] : addComment(test, 'default'),
skipBabel ? [] : addComment(Object.assign({}, test, { parser: parsers.BABEL_ESLINT }), 'babel-eslint'),
skipOldBabel ? [] : addComment(Object.assign({}, test, { parser: parsers.BABEL_ESLINT }), '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') : []
);
Expand Down
38 changes: 36 additions & 2 deletions tests/lib/rules/jsx-curly-brace-presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,43 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
{'some-complicated-exp'}
</MyComponent>
`,
features: ['no-default', 'no-ts-new'], // TODO: FIXME: remove no-default and no-ts-new and fix
features: ['no-default', 'no-ts-new', 'no-babel-new'], // TODO: FIXME: remove no-default and no-ts-new and fix
options: [{ children: 'never' }],
errors: [{ messageId: 'unnecessaryCurly' }, { messageId: 'unnecessaryCurly' }],
errors: [
{ messageId: 'unnecessaryCurly', line: 3 },
{ messageId: 'unnecessaryCurly', line: 5 },
],
},
{ // TODO: figure out how to make all other parsers work this well
code: `
<MyComponent>
{'foo'}
<div>
{'bar'}
</div>
{'baz'}
{'some-complicated-exp'}
</MyComponent>
`,
output: `
<MyComponent>
foo
<div>
bar
</div>
baz
some-complicated-exp
</MyComponent>
`,
parser: parsers['@BABEL_ESLINT'],
parserOptions: parsers.babelParserOptions({}, new Set()),
options: [{ children: 'never' }],
errors: [
{ messageId: 'unnecessaryCurly', line: 3 },
{ messageId: 'unnecessaryCurly', line: 5 },
{ messageId: 'unnecessaryCurly', line: 7 },
{ messageId: 'unnecessaryCurly', line: 8 },
],
},
{
code: `<MyComponent prop='bar'>foo</MyComponent>`,
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3227,7 +3227,7 @@ ruleTester.run('no-unused-prop-types', rule, {
}
}
`,
features: ['class fields'],
features: ['class fields', 'types'],
},
{
code: `
Expand Down

0 comments on commit a8ae170

Please sign in to comment.