Skip to content

Commit b394c38

Browse files
feat(eslint-plugin-react-internal): support ESLint 8.x (#22249)
Co-authored-by: Dan Abramov <[email protected]>
1 parent 6f64eb5 commit b394c38

14 files changed

+226
-207
lines changed

.eslintrc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@ module.exports = {
183183
},
184184
},
185185
{
186-
files: ['packages/eslint-plugin-react-hooks/src/*.js'],
186+
files: [
187+
'scripts/eslint-rules/*.js',
188+
'packages/eslint-plugin-react-hooks/src/*.js'
189+
],
187190
plugins: ['eslint-plugin'],
188191
rules: {
189192
'eslint-plugin/prefer-object-rule': ERROR,

scripts/eslint-rules/__tests__/invariant-args-test.internal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
const rule = require('../invariant-args');
13-
const RuleTester = require('eslint').RuleTester;
13+
const {RuleTester} = require('eslint');
1414
const ruleTester = new RuleTester();
1515

1616
ruleTester.run('eslint-rules/invariant-args', rule, {

scripts/eslint-rules/__tests__/no-cross-fork-imports-test.internal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
const rule = require('../no-cross-fork-imports');
13-
const RuleTester = require('eslint').RuleTester;
13+
const {RuleTester} = require('eslint');
1414
const ruleTester = new RuleTester({
1515
parserOptions: {
1616
ecmaVersion: 8,

scripts/eslint-rules/__tests__/no-cross-fork-types-test.internal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
const rule = require('../no-cross-fork-types');
13-
const RuleTester = require('eslint').RuleTester;
13+
const {RuleTester} = require('eslint');
1414
const ruleTester = new RuleTester({
1515
parserOptions: {
1616
ecmaVersion: 8,

scripts/eslint-rules/__tests__/no-primitive-constructors-test.internal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
const rule = require('../no-primitive-constructors');
13-
const RuleTester = require('eslint').RuleTester;
13+
const {RuleTester} = require('eslint');
1414
const ruleTester = new RuleTester();
1515

1616
ruleTester.run('eslint-rules/no-primitive-constructors', rule, {

scripts/eslint-rules/__tests__/no-production-logging-test.internal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
const rule = require('../no-production-logging');
13-
const RuleTester = require('eslint').RuleTester;
13+
const {RuleTester} = require('eslint');
1414
const ruleTester = new RuleTester();
1515

1616
ruleTester.run('no-production-logging', rule, {

scripts/eslint-rules/__tests__/no-to-warn-dev-within-to-throw-test.internal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
const rule = require('../no-to-warn-dev-within-to-throw');
13-
const RuleTester = require('eslint').RuleTester;
13+
const {RuleTester} = require('eslint');
1414
const ruleTester = new RuleTester();
1515

1616
ruleTester.run('eslint-rules/no-to-warn-dev-within-to-throw', rule, {

scripts/eslint-rules/__tests__/warning-args-test.internal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
const rule = require('../warning-args');
13-
const RuleTester = require('eslint').RuleTester;
13+
const {RuleTester} = require('eslint');
1414
const ruleTester = new RuleTester();
1515

1616
ruleTester.run('eslint-rules/warning-args', rule, {

scripts/eslint-rules/invariant-args.js

Lines changed: 77 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -24,83 +24,86 @@ Object.keys(existingErrorMap).forEach(key =>
2424
* argument.
2525
*/
2626

27-
module.exports = function(context) {
28-
// we also allow literal strings and concatenated literal strings
29-
function getLiteralString(node) {
30-
if (node.type === 'Literal' && typeof node.value === 'string') {
31-
return node.value;
32-
} else if (node.type === 'BinaryExpression' && node.operator === '+') {
33-
const l = getLiteralString(node.left);
34-
const r = getLiteralString(node.right);
35-
if (l !== null && r !== null) {
36-
return l + r;
27+
module.exports = {
28+
meta: {
29+
schema: [],
30+
},
31+
create(context) {
32+
// we also allow literal strings and concatenated literal strings
33+
function getLiteralString(node) {
34+
if (node.type === 'Literal' && typeof node.value === 'string') {
35+
return node.value;
36+
} else if (node.type === 'BinaryExpression' && node.operator === '+') {
37+
const l = getLiteralString(node.left);
38+
const r = getLiteralString(node.right);
39+
if (l !== null && r !== null) {
40+
return l + r;
41+
}
3742
}
43+
return null;
3844
}
39-
return null;
40-
}
4145

42-
return {
43-
CallExpression: function(node) {
44-
// This could be a little smarter by checking context.getScope() to see
45-
// how warning/invariant was defined.
46-
const isInvariant =
47-
node.callee.type === 'Identifier' && node.callee.name === 'invariant';
48-
if (!isInvariant) {
49-
return;
50-
}
51-
if (node.arguments.length < 2) {
52-
context.report(node, '{{name}} takes at least two arguments', {
53-
name: node.callee.name,
54-
});
55-
return;
56-
}
57-
const format = getLiteralString(node.arguments[1]);
58-
if (format === null) {
59-
context.report(
60-
node,
61-
'The second argument to {{name}} must be a string literal',
62-
{name: node.callee.name}
63-
);
64-
return;
65-
}
66-
if (format.length < 10 || /^[s\W]*$/.test(format)) {
67-
context.report(
68-
node,
69-
'The {{name}} format should be able to uniquely identify this ' +
70-
'{{name}}. Please, use a more descriptive format than: {{format}}',
71-
{name: node.callee.name, format: format}
72-
);
73-
return;
74-
}
75-
// count the number of formatting substitutions, plus the first two args
76-
const expectedNArgs = (format.match(/%s/g) || []).length + 2;
77-
if (node.arguments.length !== expectedNArgs) {
78-
context.report(
79-
node,
80-
'Expected {{expectedNArgs}} arguments in call to {{name}} based on ' +
81-
'the number of "%s" substitutions, but got {{length}}',
82-
{
83-
expectedNArgs: expectedNArgs,
46+
return {
47+
CallExpression: function(node) {
48+
// This could be a little smarter by checking context.getScope() to see
49+
// how warning/invariant was defined.
50+
const isInvariant =
51+
node.callee.type === 'Identifier' && node.callee.name === 'invariant';
52+
if (!isInvariant) {
53+
return;
54+
}
55+
if (node.arguments.length < 2) {
56+
context.report(node, '{{name}} takes at least two arguments', {
8457
name: node.callee.name,
85-
length: node.arguments.length,
86-
}
87-
);
88-
}
58+
});
59+
return;
60+
}
61+
const format = getLiteralString(node.arguments[1]);
62+
if (format === null) {
63+
context.report(
64+
node,
65+
'The second argument to {{name}} must be a string literal',
66+
{name: node.callee.name}
67+
);
68+
return;
69+
}
70+
if (format.length < 10 || /^[s\W]*$/.test(format)) {
71+
context.report(
72+
node,
73+
'The {{name}} format should be able to uniquely identify this ' +
74+
'{{name}}. Please, use a more descriptive format than: {{format}}',
75+
{name: node.callee.name, format: format}
76+
);
77+
return;
78+
}
79+
// count the number of formatting substitutions, plus the first two args
80+
const expectedNArgs = (format.match(/%s/g) || []).length + 2;
81+
if (node.arguments.length !== expectedNArgs) {
82+
context.report(
83+
node,
84+
'Expected {{expectedNArgs}} arguments in call to {{name}} based on ' +
85+
'the number of "%s" substitutions, but got {{length}}',
86+
{
87+
expectedNArgs: expectedNArgs,
88+
name: node.callee.name,
89+
length: node.arguments.length,
90+
}
91+
);
92+
}
8993

90-
if (!messages.has(format)) {
91-
context.report(
92-
node,
93-
'Error message does not have a corresponding production ' +
94-
'error code.\n\n' +
95-
'Run `yarn extract-errors` to add the message to error code ' +
96-
'map, so it can be stripped from the production builds. ' +
97-
"Alternatively, if you're updating an existing error " +
98-
'message, you can modify ' +
99-
'`scripts/error-codes/codes.json` directly.'
100-
);
101-
}
102-
},
103-
};
94+
if (!messages.has(format)) {
95+
context.report(
96+
node,
97+
'Error message does not have a corresponding production ' +
98+
'error code.\n\n' +
99+
'Run `yarn extract-errors` to add the message to error code ' +
100+
'map, so it can be stripped from the production builds. ' +
101+
"Alternatively, if you're updating an existing error " +
102+
'message, you can modify ' +
103+
'`scripts/error-codes/codes.json` directly.'
104+
);
105+
}
106+
},
107+
};
108+
},
104109
};
105-
106-
module.exports.schema = [];

scripts/eslint-rules/no-cross-fork-types.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ function warnIfOldField(context, oldFields, identifier) {
4646
module.exports = {
4747
meta: {
4848
type: 'problem',
49-
fixable: 'code',
5049
},
5150
create(context) {
5251
const sourceFilename = context.getFilename();

0 commit comments

Comments
 (0)