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 1608272 - Extend an ESLint rule to disallow 'this' as the second …
…argument to ChromeUtils.import. r=Gijs Depends on D104684 Differential Revision: https://phabricator.services.mozilla.com/D104685
- Loading branch information
Showing
9 changed files
with
186 additions
and
89 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
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
26 changes: 26 additions & 0 deletions
26
...quality/lint/linters/eslint-plugin-mozilla/reject-chromeutils-import-params.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,26 @@ | ||
================================ | ||
reject-chromeutils-import-params | ||
================================ | ||
|
||
``ChromeUtils.import`` can be called with two arguments, however these are now | ||
largely deprecated. | ||
|
||
The use of object destructuring is preferred over the second parameter being | ||
``this``. | ||
|
||
Using explicit exports is preferred over the second parameter being ``null``. | ||
|
||
Examples of incorrect code for this rule: | ||
----------------------------------------- | ||
|
||
.. code-block:: js | ||
ChromeUtils.import("resource://gre/modules/Services.jsm", this); | ||
ChromeUtils.import("resource://gre/modules/Services.jsm", null); | ||
Examples of correct code for this rule: | ||
--------------------------------------- | ||
|
||
.. code-block:: js | ||
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); |
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
44 changes: 0 additions & 44 deletions
44
tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-chromeutils-import-null.js
This file was deleted.
Oops, something went wrong.
70 changes: 70 additions & 0 deletions
70
tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-chromeutils-import-params.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,70 @@ | ||
/** | ||
* @fileoverview Reject calls to ChromeUtils.import(..., null). This allows to | ||
* retrieve the global object for the JSM, instead we should rely on explicitly | ||
* exported symbols. | ||
* | ||
* 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"; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Rule Definition | ||
// ----------------------------------------------------------------------------- | ||
|
||
function isIdentifier(node, id) { | ||
return node && node.type === "Identifier" && node.name === id; | ||
} | ||
|
||
module.exports = function(context) { | ||
// --------------------------------------------------------------------------- | ||
// Public | ||
// -------------------------------------------------------------------------- | ||
|
||
return { | ||
CallExpression(node) { | ||
let { callee } = node; | ||
if ( | ||
isIdentifier(callee.object, "ChromeUtils") && | ||
isIdentifier(callee.property, "import") && | ||
node.arguments.length >= 2 | ||
) { | ||
if ( | ||
node.arguments[1].type == "Literal" && | ||
node.arguments[1].raw == "null" | ||
) { | ||
context.report( | ||
node, | ||
"ChromeUtils.import should not be called with (..., null) to " + | ||
"retrieve the JSM global object. Rely on explicit exports instead." | ||
); | ||
} else if (node.arguments[1].type == "ThisExpression") { | ||
context.report({ | ||
node, | ||
message: | ||
"ChromeUtils.import should not be called with (..., this) to " + | ||
"retrieve the JSM global object. Use destructuring instead.", | ||
suggest: [ | ||
{ | ||
desc: "Use destructuring for imports.", | ||
fix: fixer => { | ||
let source = context.getSourceCode().getText(node); | ||
let match = source.match( | ||
/ChromeUtils.import\(\s*(".*\/(.*).jsm?")/m | ||
); | ||
|
||
return fixer.replaceText( | ||
node, | ||
`const { ${match[2]} } = ChromeUtils.import(${match[1]})` | ||
); | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}; |
42 changes: 0 additions & 42 deletions
42
tools/lint/eslint/eslint-plugin-mozilla/tests/reject-chromeutils-import-null.js
This file was deleted.
Oops, something went wrong.
86 changes: 86 additions & 0 deletions
86
tools/lint/eslint/eslint-plugin-mozilla/tests/reject-chromeutils-import-params.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,86 @@ | ||
/* Any copyright is dedicated to the Public Domain. | ||
* http://creativecommons.org/publicdomain/zero/1.0/ */ | ||
|
||
"use strict"; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require("../lib/rules/reject-chromeutils-import-params"); | ||
var RuleTester = require("eslint").RuleTester; | ||
|
||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 8 } }); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
function invalidError() { | ||
let message = | ||
"ChromeUtils.import should not be called with (..., null) to " + | ||
"retrieve the JSM global object. Rely on explicit exports instead."; | ||
return [{ message, type: "CallExpression" }]; | ||
} | ||
|
||
ruleTester.run("reject-chromeutils-import-params", rule, { | ||
valid: ['ChromeUtils.import("resource://some/path/to/My.jsm")'], | ||
invalid: [ | ||
{ | ||
code: 'ChromeUtils.import("resource://some/path/to/My.jsm", null)', | ||
errors: invalidError(), | ||
}, | ||
{ | ||
code: ` | ||
ChromeUtils.import( | ||
"resource://some/path/to/My.jsm", | ||
null | ||
);`, | ||
errors: invalidError(), | ||
}, | ||
{ | ||
code: 'ChromeUtils.import("resource://some/path/to/My.jsm", this)', | ||
errors: [ | ||
{ | ||
suggestions: [ | ||
{ | ||
desc: "Use destructuring for imports.", | ||
output: `const { My } = ChromeUtils.import("resource://some/path/to/My.jsm")`, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'ChromeUtils.import("resource://some/path/to/My.js", this)', | ||
errors: [ | ||
{ | ||
suggestions: [ | ||
{ | ||
desc: "Use destructuring for imports.", | ||
output: `const { My } = ChromeUtils.import("resource://some/path/to/My.js")`, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
ChromeUtils.import( | ||
"resource://some/path/to/My.jsm", | ||
this | ||
);`, | ||
errors: [ | ||
{ | ||
suggestions: [ | ||
{ | ||
desc: "Use destructuring for imports.", | ||
output: ` | ||
const { My } = ChromeUtils.import("resource://some/path/to/My.jsm");`, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}); |