From 8a964a11ea12ad169d1146d32ed8cc8f0108930f Mon Sep 17 00:00:00 2001 From: Mark Banner Date: Tue, 17 May 2022 16:50:32 +0000 Subject: [PATCH] Bug 1769569 - Make use-cc-etc auto-fixable. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D146450 --- .../lib/rules/use-cc-etc.js | 16 +++++++++++----- .../eslint-plugin-mozilla/tests/use-cc-etc.js | 15 ++++++++++----- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-cc-etc.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-cc-etc.js index 91e3749870e7..d86b70a7dc0d 100644 --- a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-cc-etc.js +++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-cc-etc.js @@ -22,6 +22,7 @@ module.exports = { "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/use-cc-etc.html", }, type: "suggestion", + fixable: "code", }, create(context) { @@ -33,12 +34,17 @@ module.exports = { node.property.type === "Identifier" && Object.getOwnPropertyNames(componentsMap).includes(node.property.name) ) { - context.report( + context.report({ node, - `Use ${componentsMap[node.property.name]} rather than Components.${ - node.property.name - }` - ); + message: `Use ${ + componentsMap[node.property.name] + } rather than Components.${node.property.name}`, + fix: fixer => + fixer.replaceTextRange( + [node.range[0], node.range[1]], + componentsMap[node.property.name] + ), + }); } }, }; diff --git a/tools/lint/eslint/eslint-plugin-mozilla/tests/use-cc-etc.js b/tools/lint/eslint/eslint-plugin-mozilla/tests/use-cc-etc.js index 7ad729d22bfa..935d42debbb3 100644 --- a/tools/lint/eslint/eslint-plugin-mozilla/tests/use-cc-etc.js +++ b/tools/lint/eslint/eslint-plugin-mozilla/tests/use-cc-etc.js @@ -16,9 +16,10 @@ const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } }); // Tests // ------------------------------------------------------------------------------ -function invalidCode(code, originalName, newName) { +function invalidCode(code, originalName, newName, output) { return { code, + output, errors: [ { message: `Use ${newName} rather than ${originalName}`, @@ -34,22 +35,26 @@ ruleTester.run("use-cc-etc", rule, { invalidCode( "let foo = Components.classes['bar'];", "Components.classes", - "Cc" + "Cc", + "let foo = Cc['bar'];" ), invalidCode( "let bar = Components.interfaces.bar;", "Components.interfaces", - "Ci" + "Ci", + "let bar = Ci.bar;" ), invalidCode( "Components.results.NS_ERROR_ILLEGAL_INPUT;", "Components.results", - "Cr" + "Cr", + "Cr.NS_ERROR_ILLEGAL_INPUT;" ), invalidCode( "Components.utils.reportError('fake');", "Components.utils", - "Cu" + "Cu", + "Cu.reportError('fake');" ), ], });