From ed3b1cf8f398a284353e9f8d41a4ab7b1c31a543 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 23 Dec 2024 13:59:32 -0800 Subject: [PATCH] [Tests] `jsx-uses-vars`, `jsx-uses-react`: fix `no-unused-vars` tests in eslint v9.17 https://github.com/eslint/eslint/pull/18352 added suggestions to no-unused-vars in eslint v9.17.0 --- tests/index.js | 1 + tests/lib/rules/jsx-uses-react.js | 51 ++++++++++-- tests/lib/rules/jsx-uses-vars.js | 124 ++++++++++++++++++++++++++++-- 3 files changed, 162 insertions(+), 14 deletions(-) diff --git a/tests/index.js b/tests/index.js index 9f7a052943..3539f48136 100644 --- a/tests/index.js +++ b/tests/index.js @@ -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'); diff --git a/tests/lib/rules/jsx-uses-react.js b/tests/lib/rules/jsx-uses-react.js index 3a8e39013c..381b44bc89 100644 --- a/tests/lib/rules/jsx-uses-react.js +++ b/tests/lib/rules/jsx-uses-react.js @@ -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;
;', - 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 */
;', + }], + }], }, { 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*/
;', + }], + }], 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; + })), }); diff --git a/tests/lib/rules/jsx-uses-vars.js b/tests/lib/rules/jsx-uses-vars.js index 00f801fd65..8b9db2a956 100644 --- a/tests/lib/rules/jsx-uses-vars.js +++ b/tests/lib/rules/jsx-uses-vars.js @@ -10,6 +10,7 @@ // ----------------------------------------------------------------------------- const ruleNoUnusedVars = require('../../helpers/getESLintCoreRule')('no-unused-vars'); + const rulePreferConst = require('../../helpers/getESLintCoreRule')('prefer-const'); const RuleTester = require('../../helpers/ruleTester'); @@ -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: ` @@ -145,7 +152,18 @@ ruleTester.run('no-unused-vars', ruleNoUnusedVars, { var unused; React.render(); `, - 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(); + `, + }], + }], }, { code: ` @@ -155,8 +173,30 @@ ruleTester.run('no-unused-vars', ruleNoUnusedVars, { React.render(); `, 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(); + `, + }], + }, + { + message: '\'Hello\' is defined but never used.', + suggestions: [{ + messageId: 'removeVar', + output: ` + /* eslint react/jsx-uses-vars: 1 */ + var App; + ${''} + React.render(); + `, + }], + }, ], features: ['jsx namespace'], }, @@ -167,14 +207,34 @@ ruleTester.run('no-unused-vars', ruleNoUnusedVars, { var Input; React.render(); `, - 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(); + `, + }], + }], }, { 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: ` @@ -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 */ + ${''} + `, + }], }, ], }, @@ -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 ; + } + Greetings(); + `, + }], }, ], }, @@ -216,7 +295,17 @@ ruleTester.run('no-unused-vars', ruleNoUnusedVars, { var lowercase; React.render(); `, - 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(); + `, + }], + }], }, { code: ` @@ -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
; + } + 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)