diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-static-import.js | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-static-import.js')
-rw-r--r-- | tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-static-import.js | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-static-import.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-static-import.js new file mode 100644 index 0000000000..100b5682de --- /dev/null +++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-static-import.js @@ -0,0 +1,87 @@ +/** + * @fileoverview Require use of static imports where possible. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +"use strict"; + +const helpers = require("../helpers"); + +function isIdentifier(node, id) { + return node && node.type === "Identifier" && node.name === id; +} + +module.exports = { + meta: { + docs: { + url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/rules/use-static-import.html", + }, + fixable: "code", + messages: { + useStaticImport: + "Please use static import instead of ChromeUtils.importESModule", + }, + schema: [], + type: "suggestion", + }, + + create(context) { + return { + VariableDeclarator(node) { + if ( + node.init?.type != "CallExpression" || + node.init?.callee?.type != "MemberExpression" || + !context.getFilename().endsWith(".sys.mjs") || + !helpers.isTopLevel(context.getAncestors()) + ) { + return; + } + + let callee = node.init.callee; + + if ( + isIdentifier(callee.object, "ChromeUtils") && + isIdentifier(callee.property, "importESModule") && + callee.parent.arguments.length == 1 + ) { + let sourceCode = context.getSourceCode(); + let importItemSource; + if (node.id.type != "ObjectPattern") { + importItemSource = sourceCode.getText(node.id); + } else { + importItemSource = "{ "; + let initial = true; + for (let property of node.id.properties) { + if (!initial) { + importItemSource += ", "; + } + initial = false; + if (property.key.name == property.value.name) { + importItemSource += property.key.name; + } else { + importItemSource += `${property.key.name} as ${property.value.name}`; + } + } + importItemSource += " }"; + } + + context.report({ + node: node.parent, + messageId: "useStaticImport", + fix(fixer) { + return fixer.replaceText( + node.parent, + `import ${importItemSource} from ${sourceCode.getText( + callee.parent.arguments[0] + )}` + ); + }, + }); + } + }, + }; + }, +}; |