Skip to content

Commit

Permalink
[Tests] jsx-uses-vars, jsx-uses-react: fix no-unused-vars tests…
Browse files Browse the repository at this point in the history
… in eslint v9.17

eslint/eslint#18352 added suggestions to no-unused-vars in eslint v9.17.0
  • Loading branch information
ljharb committed Dec 23, 2024
1 parent 033ad19 commit ed3b1cf
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 14 deletions.
1 change: 1 addition & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const plugin = require('..');
const index = require('../lib/rules');

const ruleFiles = fs.readdirSync(path.resolve(__dirname, '../lib/rules/'))
.filter((f) => f.endsWith('.js'))
.map((f) => path.basename(f, '.js'))
.filter((f) => f !== 'index');

Expand Down
51 changes: 45 additions & 6 deletions tests/lib/rules/jsx-uses-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,67 @@ ruleTester.run('no-unused-vars', rule, {
invalid: parsers.all([
{
code: '/*eslint react/jsx-uses-react:1*/ var React;',
errors: [{ message: '\'React\' is defined but never used.' }],
errors: [{
message: '\'React\' is defined but never used.',
suggestions: [{
messageId: 'removeVar',
output: '/*eslint react/jsx-uses-react:1*/ ',
}],
}],
},
{
code: '/*eslint react/jsx-uses-react:1*/ /** @jsx Foo */ var React; <div />;',
errors: [{ message: '\'React\' is defined but never used.' }],
errors: [{
message: '\'React\' is defined but never used.',
suggestions: [{
messageId: 'removeVar',
output: '/*eslint react/jsx-uses-react:1*/ /** @jsx Foo */ <div />;',
}],
}],
},
{
code: '/*eslint react/jsx-uses-react:1*/ var React; <div />;',
errors: [{ message: '\'React\' is defined but never used.' }],
errors: [{
message: '\'React\' is defined but never used.',
suggestions: [{
messageId: 'removeVar',
output: '/*eslint react/jsx-uses-react:1*/ <div />;',
}],
}],
settings,
},
{
code: '/*eslint react/jsx-uses-react:1*/ var Frag; <></>;',
errors: [{ message: '\'Frag\' is defined but never used.' }],
errors: [{
message: '\'Frag\' is defined but never used.',
suggestions: [{
messageId: 'removeVar',
output: '/*eslint react/jsx-uses-react:1*/ <></>;',
}],
}],
features: ['fragment'],
settings: { react: { fragment: 'Fragment' } },
},
{
code: '/*eslint react/jsx-uses-react:1*/ var React; <></>;',
features: ['fragment'],
errors: [{ message: '\'React\' is defined but never used.' }],
errors: [{
message: '\'React\' is defined but never used.',
suggestions: [{
messageId: 'removeVar',
output: '/*eslint react/jsx-uses-react:1*/ <></>;',
}],
}],
settings,
},
].map(parsers.disableNewTS)),
].map(parsers.disableNewTS).map((test) => {
if (!rule.meta.hasSuggestions) {
test.errors = test.errors.map((error) => {
// https://github.com/eslint/eslint/pull/18352 added suggestions to no-unused-vars in eslint v9.17.0
delete error.suggestions;
return error;
});
}
return test;
})),
});
124 changes: 116 additions & 8 deletions tests/lib/rules/jsx-uses-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// -----------------------------------------------------------------------------

const ruleNoUnusedVars = require('../../helpers/getESLintCoreRule')('no-unused-vars');

const rulePreferConst = require('../../helpers/getESLintCoreRule')('prefer-const');

const RuleTester = require('../../helpers/ruleTester');
Expand Down Expand Up @@ -136,7 +137,13 @@ ruleTester.run('no-unused-vars', ruleNoUnusedVars, {
invalid: parsers.all([
{
code: '/* eslint react/jsx-uses-vars: 1 */ var App;',
errors: [{ message: '\'App\' is defined but never used.' }],
errors: [{
message: '\'App\' is defined but never used.',
suggestions: [{
messageId: 'removeVar',
output: '/* eslint react/jsx-uses-vars: 1 */ ',
}],
}],
},
{
code: `
Expand All @@ -145,7 +152,18 @@ ruleTester.run('no-unused-vars', ruleNoUnusedVars, {
var unused;
React.render(<App unused=""/>);
`,
errors: [{ message: '\'unused\' is defined but never used.' }],
errors: [{
message: '\'unused\' is defined but never used.',
suggestions: [{
messageId: 'removeVar',
output: `
/* eslint react/jsx-uses-vars: 1 */
var App;
${''}
React.render(<App unused=""/>);
`,
}],
}],
},
{
code: `
Expand All @@ -155,8 +173,30 @@ ruleTester.run('no-unused-vars', ruleNoUnusedVars, {
React.render(<App:Hello/>);
`,
errors: [
{ message: '\'App\' is defined but never used.' },
{ message: '\'Hello\' is defined but never used.' },
{
message: '\'App\' is defined but never used.',
suggestions: [{
messageId: 'removeVar',
output: `
/* eslint react/jsx-uses-vars: 1 */
${''}
var Hello;
React.render(<App:Hello/>);
`,
}],
},
{
message: '\'Hello\' is defined but never used.',
suggestions: [{
messageId: 'removeVar',
output: `
/* eslint react/jsx-uses-vars: 1 */
var App;
${''}
React.render(<App:Hello/>);
`,
}],
},
],
features: ['jsx namespace'],
},
Expand All @@ -167,14 +207,34 @@ ruleTester.run('no-unused-vars', ruleNoUnusedVars, {
var Input;
React.render(<Button.Input unused=""/>);
`,
errors: [{ message: '\'Input\' is defined but never used.' }],
errors: [{
message: '\'Input\' is defined but never used.',
suggestions: [{
messageId: 'removeVar',
output: `
/* eslint react/jsx-uses-vars: 1 */
var Button;
${''}
React.render(<Button.Input unused=""/>);
`,
}],
}],
},
{
code: `
/* eslint react/jsx-uses-vars: 1 */
class unused {}
`,
errors: [{ message: '\'unused\' is defined but never used.' }],
errors: [{
message: '\'unused\' is defined but never used.',
suggestions: [{
messageId: 'removeVar',
output: `
/* eslint react/jsx-uses-vars: 1 */
${''}
`,
}],
}],
},
{
code: `
Expand All @@ -190,6 +250,13 @@ ruleTester.run('no-unused-vars', ruleNoUnusedVars, {
{
message: '\'HelloMessage\' is defined but never used.',
line: 3,
suggestions: [{
messageId: 'removeVar',
output: `
/* eslint react/jsx-uses-vars: 1 */
${''}
`,
}],
},
],
},
Expand All @@ -207,6 +274,18 @@ ruleTester.run('no-unused-vars', ruleNoUnusedVars, {
{
message: '\'Hello\' is defined but never used.',
line: 3,
suggestions: [{
messageId: 'removeVar',
output: `
/* eslint react/jsx-uses-vars: 1 */
${''}
function Greetings() {
const Hello = require('Hello').default;
return <Hello />;
}
Greetings();
`,
}],
},
],
},
Expand All @@ -216,7 +295,17 @@ ruleTester.run('no-unused-vars', ruleNoUnusedVars, {
var lowercase;
React.render(<lowercase />);
`,
errors: [{ message: '\'lowercase\' is defined but never used.' }],
errors: [{
message: '\'lowercase\' is defined but never used.',
suggestions: [{
messageId: 'removeVar',
output: `
/* eslint react/jsx-uses-vars: 1 */
${''}
React.render(<lowercase />);
`,
}],
}],
},
{
code: `
Expand All @@ -230,10 +319,29 @@ ruleTester.run('no-unused-vars', ruleNoUnusedVars, {
{
message: '\'div\' is defined but never used.',
line: 3,
suggestions: [{
messageId: 'removeVar',
output: `
/* eslint react/jsx-uses-vars: 1 */
function Greetings() {
return <div />;
}
Greetings();
`,
}],
},
],
},
]),
].map((test) => {
if (!ruleNoUnusedVars.meta.hasSuggestions) {
test.errors = test.errors.map((error) => {
// https://github.com/eslint/eslint/pull/18352 added suggestions to no-unused-vars in eslint v9.17.0
delete error.suggestions;
return error;
});
}
return test;
})),
});

// Check compatibility with eslint prefer-const rule (#716)
Expand Down

0 comments on commit ed3b1cf

Please sign in to comment.