This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1431533: Part 4 - Add ESLint support for ChromeUtils import metho…
…ds. r=florian This adds support for detecting globals created by these helpers, as well as a rule to enforce their use over the older XPConnect variants. The latter rule also supports fixing code to use the newer variants, and will be used in the next part to rewrite in-tree ESLint-enabled code that fails it. MozReview-Commit-ID: 6Bgo6ohQA5j
- Loading branch information
Showing
7 changed files
with
171 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
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
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
79 changes: 79 additions & 0 deletions
79
tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-chromeutils-import.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,79 @@ | ||
/** | ||
* @fileoverview Reject use of Cu.import and XPCOMUtils.defineLazyModuleGetter | ||
* in favor of ChromeUtils. | ||
* | ||
* 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; | ||
} | ||
|
||
function isMemberExpression(node, object, member) { | ||
return (node.type === "MemberExpression" && | ||
isIdentifier(node.object, object) && | ||
isIdentifier(node.property, member)); | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"allowCu": { | ||
"type": "boolean" | ||
} | ||
}, | ||
"additionalProperties": false | ||
} | ||
], | ||
fixable: "code" | ||
}, | ||
|
||
create(context) { | ||
return { | ||
"CallExpression": function(node) { | ||
if (node.callee.type !== "MemberExpression") { | ||
return; | ||
} | ||
|
||
let {allowCu} = context.options[0] || {}; | ||
let {callee} = node; | ||
|
||
// Is the expression starting with `Cu` or `Components.utils`? | ||
if (((!allowCu && isIdentifier(callee.object, "Cu")) || | ||
isMemberExpression(callee.object, "Components", "utils")) && | ||
isIdentifier(callee.property, "import")) { | ||
context.report({ | ||
node, | ||
message: "Please use ChromeUtils.import instead of Cu.import", | ||
fix(fixer) { | ||
return fixer.replaceText(callee, "ChromeUtils.import"); | ||
} | ||
}); | ||
} | ||
|
||
if (isMemberExpression(callee, "XPCOMUtils", "defineLazyModuleGetter") && | ||
node.arguments.length < 4) { | ||
context.report({ | ||
node, | ||
message: ("Please use ChromeUtils.defineModuleGetter instead of " + | ||
"XPCOMUtils.defineLazyModuleGetter"), | ||
fix(fixer) { | ||
return fixer.replaceText(callee, "ChromeUtils.defineModuleGetter"); | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
80 changes: 80 additions & 0 deletions
80
tools/lint/eslint/eslint-plugin-mozilla/tests/use-chromeutils-import.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,80 @@ | ||
/* Any copyright is dedicated to the Public Domain. | ||
* http://creativecommons.org/publicdomain/zero/1.0/ */ | ||
|
||
"use strict"; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require("../lib/rules/use-chromeutils-import"); | ||
var RuleTester = require("eslint/lib/testers/rule-tester"); | ||
|
||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } }); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
function callError(message) { | ||
return [{message, type: "CallExpression"}]; | ||
} | ||
|
||
const MESSAGE_IMPORT = "Please use ChromeUtils.import instead of Cu.import"; | ||
const MESSAGE_DEFINE = ("Please use ChromeUtils.defineModuleGetter instead of " + | ||
"XPCOMUtils.defineLazyModuleGetter"); | ||
|
||
ruleTester.run("use-chromeutils-import", rule, { | ||
valid: [ | ||
`ChromeUtils.import("resource://gre/modules/Service.jsm");`, | ||
`ChromeUtils.import("resource://gre/modules/Service.jsm", this);`, | ||
`ChromeUtils.defineModuleGetter(this, "Services", | ||
"resource://gre/modules/Service.jsm");`, | ||
`XPCOMUtils.defineLazyModuleGetter(this, "Services", | ||
"resource://gre/modules/Service.jsm", | ||
"Foo");`, | ||
`XPCOMUtils.defineLazyModuleGetter(this, "Services", | ||
"resource://gre/modules/Service.jsm", | ||
undefined, preServicesLambda);`, | ||
{ | ||
options: [{allowCu: true}], | ||
code: `Cu.import("resource://gre/modules/Service.jsm");` | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: `Cu.import("resource://gre/modules/Services.jsm");`, | ||
output: `ChromeUtils.import("resource://gre/modules/Services.jsm");`, | ||
errors: callError(MESSAGE_IMPORT) | ||
}, | ||
{ | ||
code: `Cu.import("resource://gre/modules/Services.jsm", this);`, | ||
output: `ChromeUtils.import("resource://gre/modules/Services.jsm", this);`, | ||
errors: callError(MESSAGE_IMPORT) | ||
}, | ||
{ | ||
code: `Components.utils.import("resource://gre/modules/Services.jsm");`, | ||
output: `ChromeUtils.import("resource://gre/modules/Services.jsm");`, | ||
errors: callError(MESSAGE_IMPORT) | ||
}, | ||
{ | ||
code: `Components.utils.import("resource://gre/modules/Services.jsm");`, | ||
output: `ChromeUtils.import("resource://gre/modules/Services.jsm");`, | ||
errors: callError(MESSAGE_IMPORT) | ||
}, | ||
{ | ||
options: [{allowCu: true}], | ||
code: `Components.utils.import("resource://gre/modules/Services.jsm", this);`, | ||
output: `ChromeUtils.import("resource://gre/modules/Services.jsm", this);`, | ||
errors: callError(MESSAGE_IMPORT) | ||
}, | ||
{ | ||
code: `XPCOMUtils.defineLazyModuleGetter(this, "Services", | ||
"resource://gre/modules/Services.jsm");`, | ||
output: `ChromeUtils.defineModuleGetter(this, "Services", | ||
"resource://gre/modules/Services.jsm");`, | ||
errors: callError(MESSAGE_DEFINE) | ||
} | ||
] | ||
}); | ||
|