forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1771751 - Reject static import for system module from non-system …
…module. r=Standard8 Differential Revision: https://phabricator.services.mozilla.com/D149183
- Loading branch information
Showing
6 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...t/linters/eslint-plugin-mozilla/reject-import-system-module-from-non-system.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
reject-import-system-module-from-non-system | ||
=========================================== | ||
|
||
Rejects static import declaration for system modules (``.sys.mjs``) from non-system | ||
modules. | ||
|
||
Using static import for a system module into a non-system module would create a separate instance of the imported object(s) that is not shared with the other system modules and would break the per-process singleton expectation. | ||
|
||
The reason for this is that inside system modules, a static import will load the module into the shared global. Inside non-system modules, the static import will load into a different global (e.g. window). This will cause the module to be loaded into different scopes, and hence create separate instances. The fix is to use ``ChromeUtils.importESM`` which will import the object via the system module shared global scope. | ||
|
||
|
||
Examples of incorrect code for this rule: | ||
----------------------------------------- | ||
|
||
Inside a non-system module: | ||
|
||
.. code-block:: js | ||
import { Services } from "resource://gre/modules/Services.sys.mjs"; | ||
Examples of correct code for this rule: | ||
--------------------------------------- | ||
|
||
Inside a non-system module: | ||
|
||
.. code-block:: js | ||
const { Services } = ChromeUtils.importESM( | ||
"resource://gre/modules/Services.sys.mjs" | ||
); | ||
Inside a system module: | ||
|
||
.. code-block:: js | ||
import { Services } from "resource://gre/modules/Services.sys.mjs"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...int/eslint/eslint-plugin-mozilla/lib/rules/reject-import-system-module-from-non-system.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* 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"; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: | ||
"https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/reject-import-system-module-from-non-system.html", | ||
}, | ||
messages: { | ||
rejectStaticImportSystemModuleFromNonSystem: | ||
"System modules (*.sys.mjs) can be imported with static import declaration only from system modules.", | ||
}, | ||
type: "problem", | ||
}, | ||
|
||
create(context) { | ||
return { | ||
ImportDeclaration(node) { | ||
if (!node.source.value.endsWith(".sys.mjs")) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node, | ||
messageId: "rejectStaticImportSystemModuleFromNonSystem", | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
33 changes: 33 additions & 0 deletions
33
tools/lint/eslint/eslint-plugin-mozilla/tests/reject-import-system-module-from-non-system.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* Any copyright is dedicated to the Public Domain. | ||
* http://creativecommons.org/publicdomain/zero/1.0/ */ | ||
|
||
"use strict"; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require("../lib/rules/reject-import-system-module-from-non-system"); | ||
var RuleTester = require("eslint").RuleTester; | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { ecmaVersion: 13, sourceType: "module" }, | ||
}); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
ruleTester.run("reject-import-system-module-from-non-system", rule, { | ||
valid: [ | ||
{ | ||
code: `const { Services } = ChromeUtils.importESM("resource://gre/modules/Services.sys.mjs");`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: `import { Services } from "resource://gre/modules/Services.sys.mjs";`, | ||
errors: [{ messageId: "rejectStaticImportSystemModuleFromNonSystem" }], | ||
}, | ||
], | ||
}); |