Skip to content

Commit e0e16bb

Browse files
committed
[Tests] test on the new babel eslint parser
1 parent 4bc3499 commit e0e16bb

File tree

5 files changed

+90
-9
lines changed

5 files changed

+90
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1919
* [`no-unstable-components`]: improve handling of objects containing render function properties ([#3111] @fizwidget)
2020
* [`prop-types`], `propTypes`: add forwardRef<>, ForwardRefRenderFunction<> prop-types ([#3112] @vedadeepta)
2121

22+
### Changed
23+
* [Tests] test on the new babel eslint parser ([#3113] @ljharb)
24+
25+
[#3113]: https://github.com/yannickcr/eslint-plugin-react/pull/3113
2226
[#3112]: https://github.com/yannickcr/eslint-plugin-react/pull/3112
2327
[#3111]: https://github.com/yannickcr/eslint-plugin-react/pull/3111
2428
[#3110]: https://github.com/yannickcr/eslint-plugin-react/pull/3110

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
"string.prototype.matchall": "^4.0.6"
4545
},
4646
"devDependencies": {
47+
"@babel/core": "^7.15.8",
48+
"@babel/eslint-parser": "^7.15.8",
49+
"@babel/plugin-syntax-decorators": "^7.14.5",
50+
"@babel/plugin-syntax-do-expressions": "^7.14.5",
51+
"@babel/plugin-syntax-function-bind": "^7.14.5",
52+
"@babel/preset-react": "^7.14.5",
4753
"@types/eslint": "=7.2.10",
4854
"@types/estree": "^0.0.50",
4955
"@types/node": "^14.17.27",

tests/helpers/parsers.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,33 @@ const NODE_MODULES = '../../node_modules';
99

1010
const parsers = {
1111
BABEL_ESLINT: path.join(__dirname, NODE_MODULES, 'babel-eslint'),
12+
'@BABEL_ESLINT': path.join(__dirname, NODE_MODULES, '@babel/eslint-parser'),
1213
TYPESCRIPT_ESLINT: path.join(__dirname, NODE_MODULES, 'typescript-eslint-parser'),
1314
'@TYPESCRIPT_ESLINT': path.join(__dirname, NODE_MODULES, '@typescript-eslint/parser'),
15+
babelParserOptions: function parserOptions(test, features) {
16+
return Object.assign({}, test.parserOptions, {
17+
requireConfigFile: false,
18+
babelOptions: {
19+
presets: [
20+
'@babel/preset-react',
21+
],
22+
plugins: [
23+
'@babel/plugin-syntax-do-expressions',
24+
'@babel/plugin-syntax-function-bind',
25+
['@babel/plugin-syntax-decorators', { legacy: true }],
26+
],
27+
},
28+
ecmaFeatures: Object.assign(
29+
{},
30+
test.parserOptions && test.parserOptions.ecmaFeatures,
31+
{
32+
jsx: true,
33+
modules: true,
34+
legacyDecorators: features.has('decorators'),
35+
}
36+
),
37+
});
38+
},
1439
all: function all(tests) {
1540
const t = flatMap(tests, (test) => {
1641
if (typeof test === 'string') {
@@ -25,7 +50,7 @@ const parsers = {
2550
const es = test.parserOptions && test.parserOptions.ecmaVersion;
2651

2752
function addComment(testObject, parser) {
28-
const extraComment = `\n// features: [${Array.from(features).join(',')}], parser: ${parser}`;
53+
const extraComment = `\n// features: [${Array.from(features).join(',')}], parser: ${parser}, parserOptions: ${testObject.parserOptions}`;
2954
return Object.assign(
3055
{},
3156
testObject,
@@ -46,9 +71,16 @@ const parsers = {
4671
|| (features.has('fragment') && semver.satisfies(version, '< 5'));
4772

4873
const skipBabel = features.has('no-babel');
49-
const skipTS = semver.satisfies(version, '< 5')
74+
const skipOldBabel = skipBabel || semver.satisfies(version, '>= 8');
75+
const skipNewBabel = skipBabel
76+
|| features.has('no-babel-new')
77+
|| !semver.satisfies(version, '^7.5.0') // require('@babel/eslint-parser/package.json').peerDependencies.eslint
5078
|| features.has('flow')
79+
|| features.has('types')
80+
|| features.has('ts');
81+
const skipTS = semver.satisfies(version, '< 5')
5182
|| features.has('no-ts')
83+
|| features.has('flow')
5284
|| features.has('jsx namespace')
5385
|| features.has('bind operator')
5486
|| features.has('do expressions');
@@ -57,7 +89,11 @@ const parsers = {
5789

5890
return [].concat(
5991
skipBase ? [] : addComment(test, 'default'),
60-
skipBabel ? [] : addComment(Object.assign({}, test, { parser: parsers.BABEL_ESLINT }), 'babel-eslint'),
92+
skipOldBabel ? [] : addComment(Object.assign({}, test, { parser: parsers.BABEL_ESLINT }), 'babel-eslint'),
93+
skipNewBabel ? [] : addComment(Object.assign({}, test, {
94+
parser: parsers['@BABEL_ESLINT'],
95+
parserOptions: parsers.babelParserOptions(test, features),
96+
}), '@babel/eslint-parser'),
6197
tsOld ? addComment(Object.assign({}, test, { parser: parsers.TYPESCRIPT_ESLINT }), 'typescript-eslint') : [],
6298
tsNew ? addComment(Object.assign({}, test, { parser: parsers['@TYPESCRIPT_ESLINT'] }), '@typescript/eslint') : []
6399
);

tests/lib/rules/jsx-curly-brace-presence.js

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
441441
] : [])
442442
)),
443443

444-
invalid: parsers.all([
444+
invalid: parsers.all([].concat(
445445
{
446446
code: '<App prop={`foo`} />',
447447
output: '<App prop="foo" />',
@@ -561,10 +561,45 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
561561
{'some-complicated-exp'}
562562
</MyComponent>
563563
`,
564-
features: ['no-default', 'no-ts-new'], // TODO: FIXME: remove no-default and no-ts-new and fix
564+
features: ['no-default', 'no-ts-new', 'no-babel-new'], // TODO: FIXME: remove no-default and no-ts-new and fix
565565
options: [{ children: 'never' }],
566-
errors: [{ messageId: 'unnecessaryCurly' }, { messageId: 'unnecessaryCurly' }],
566+
errors: [
567+
{ messageId: 'unnecessaryCurly', line: 3 },
568+
{ messageId: 'unnecessaryCurly', line: 5 },
569+
],
567570
},
571+
semver.satisfies(eslintPkg.version, '^7.5.0') ? { // require('@babel/eslint-parser/package.json').peerDependencies.eslint
572+
// TODO: figure out how to make all other parsers work this well
573+
code: `
574+
<MyComponent>
575+
{'foo'}
576+
<div>
577+
{'bar'}
578+
</div>
579+
{'baz'}
580+
{'some-complicated-exp'}
581+
</MyComponent>
582+
`,
583+
output: `
584+
<MyComponent>
585+
foo
586+
<div>
587+
bar
588+
</div>
589+
baz
590+
some-complicated-exp
591+
</MyComponent>
592+
`,
593+
parser: parsers['@BABEL_ESLINT'],
594+
parserOptions: parsers.babelParserOptions({}, new Set()),
595+
options: [{ children: 'never' }],
596+
errors: [
597+
{ messageId: 'unnecessaryCurly', line: 3 },
598+
{ messageId: 'unnecessaryCurly', line: 5 },
599+
{ messageId: 'unnecessaryCurly', line: 7 },
600+
{ messageId: 'unnecessaryCurly', line: 8 },
601+
],
602+
} : [],
568603
{
569604
code: `<MyComponent prop='bar'>foo</MyComponent>`,
570605
output: '<MyComponent prop={"bar"}>foo</MyComponent>',
@@ -854,6 +889,6 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
854889
output: '<MyComponent prop="< style: true >">foo</MyComponent>',
855890
errors: [{ messageId: 'unnecessaryCurly' }],
856891
options: ['never'],
857-
},
858-
]),
892+
}
893+
)),
859894
});

tests/lib/rules/no-unused-prop-types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3227,7 +3227,7 @@ ruleTester.run('no-unused-prop-types', rule, {
32273227
}
32283228
}
32293229
`,
3230-
features: ['class fields'],
3230+
features: ['class fields', 'types'],
32313231
},
32323232
{
32333233
code: `

0 commit comments

Comments
 (0)