summaryrefslogtreecommitdiffstats
path: root/tools/lint/eslint/eslint-plugin-mozilla/tests/use-static-import.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /tools/lint/eslint/eslint-plugin-mozilla/tests/use-static-import.js
parentInitial commit. (diff)
downloadfirefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz
firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/lint/eslint/eslint-plugin-mozilla/tests/use-static-import.js')
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/tests/use-static-import.js88
1 files changed, 88 insertions, 0 deletions
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/tests/use-static-import.js b/tools/lint/eslint/eslint-plugin-mozilla/tests/use-static-import.js
new file mode 100644
index 0000000000..e4d28861ad
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/tests/use-static-import.js
@@ -0,0 +1,88 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+// ------------------------------------------------------------------------------
+// Requirements
+// ------------------------------------------------------------------------------
+
+var rule = require("../lib/rules/use-static-import");
+var RuleTester = require("eslint").RuleTester;
+
+const ruleTester = new RuleTester({
+ parserOptions: { ecmaVersion: 13, sourceType: "module" },
+});
+
+// ------------------------------------------------------------------------------
+// Tests
+// ------------------------------------------------------------------------------
+
+function callError() {
+ return [{ messageId: "useStaticImport", type: "VariableDeclaration" }];
+}
+
+ruleTester.run("use-static-import", rule, {
+ valid: [
+ {
+ // Already converted, no issues.
+ code:
+ 'import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";',
+ filename: "test.sys.mjs",
+ },
+ {
+ // Inside an if statement.
+ code:
+ 'if (foo) { const { XPCOMUtils } = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs") }',
+ filename: "test.sys.mjs",
+ },
+ {
+ // Inside a function.
+ code:
+ 'function foo() { const { XPCOMUtils } = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs") }',
+ filename: "test.sys.mjs",
+ },
+ {
+ // importESModule with two args cannot be converted.
+ code:
+ 'const { f } = ChromeUtils.importESModule("some/module.sys.mjs", { loadInDevToolsLoader : true });',
+ filename: "test.sys.mjs",
+ },
+ {
+ // A non-system file attempting to import a system file should not be
+ // converted.
+ code:
+ 'const { XPCOMUtils } = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs")',
+ filename: "test.mjs",
+ },
+ ],
+ invalid: [
+ {
+ // Simple import in system module should be converted.
+ code:
+ 'const { XPCOMUtils } = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs")',
+ errors: callError(),
+ filename: "test.sys.mjs",
+ output:
+ 'import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs"',
+ },
+ {
+ // Should handle rewritten variables as well.
+ code:
+ 'const { XPCOMUtils: foo } = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs")',
+ errors: callError(),
+ filename: "test.sys.mjs",
+ output:
+ 'import { XPCOMUtils as foo } from "resource://gre/modules/XPCOMUtils.sys.mjs"',
+ },
+ {
+ // Should handle multiple variables.
+ code:
+ 'const { foo, XPCOMUtils } = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs")',
+ errors: callError(),
+ filename: "test.sys.mjs",
+ output:
+ 'import { foo, XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs"',
+ },
+ ],
+});