Skip to content

Commit b7eaadc

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

File tree

5 files changed

+79
-5
lines changed

5 files changed

+79
-5
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
"string.prototype.matchall": "^4.0.6"
4545
},
4646
"devDependencies": {
47+
"@babel/eslint-parser": "^7.15.8",
48+
"@babel/plugin-syntax-decorators": "^7.14.5",
49+
"@babel/plugin-syntax-do-expressions": "^7.14.5",
50+
"@babel/plugin-syntax-function-bind": "^7.14.5",
51+
"@babel/preset-react": "^7.14.5",
4752
"@types/eslint": "=7.2.10",
4853
"@types/estree": "^0.0.50",
4954
"@types/node": "^14.17.27",

tests/helpers/parsers.js

Lines changed: 33 additions & 2 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,6 +71,8 @@ const parsers = {
4671
|| (features.has('fragment') && semver.satisfies(version, '< 5'));
4772

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

5885
return [].concat(
5986
skipBase ? [] : addComment(test, 'default'),
60-
skipBabel ? [] : addComment(Object.assign({}, test, { parser: parsers.BABEL_ESLINT }), 'babel-eslint'),
87+
skipOldBabel ? [] : addComment(Object.assign({}, test, { parser: parsers.BABEL_ESLINT }), 'babel-eslint'),
88+
skipNewBabel ? [] : addComment(Object.assign({}, test, {
89+
parser: parsers['@BABEL_ESLINT'],
90+
parserOptions: parsers.babelParserOptions(test, features),
91+
}), '@babel/eslint-parser'),
6192
tsOld ? addComment(Object.assign({}, test, { parser: parsers.TYPESCRIPT_ESLINT }), 'typescript-eslint') : [],
6293
tsNew ? addComment(Object.assign({}, test, { parser: parsers['@TYPESCRIPT_ESLINT'] }), '@typescript/eslint') : []
6394
);

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,43 @@ 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+
],
570+
},
571+
{ // TODO: figure out how to make all other parsers work this well
572+
code: `
573+
<MyComponent>
574+
{'foo'}
575+
<div>
576+
{'bar'}
577+
</div>
578+
{'baz'}
579+
{'some-complicated-exp'}
580+
</MyComponent>
581+
`,
582+
output: `
583+
<MyComponent>
584+
foo
585+
<div>
586+
bar
587+
</div>
588+
baz
589+
some-complicated-exp
590+
</MyComponent>
591+
`,
592+
parser: parsers['@BABEL_ESLINT'],
593+
parserOptions: parsers.babelParserOptions({}, new Set()),
594+
options: [{ children: 'never' }],
595+
errors: [
596+
{ messageId: 'unnecessaryCurly', line: 3 },
597+
{ messageId: 'unnecessaryCurly', line: 5 },
598+
{ messageId: 'unnecessaryCurly', line: 7 },
599+
{ messageId: 'unnecessaryCurly', line: 8 },
600+
],
567601
},
568602
{
569603
code: `<MyComponent prop='bar'>foo</MyComponent>`,

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)