Bug 1431069 - allow eslint to deal with ChromeUtils imports, r?Standard8
MozReview-Commit-ID: KHVewE1Rrov
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
@@ -35,17 +35,17 @@ const callExpressionDefinitions = [
];
const callExpressionMultiDefinitions = [
"XPCOMUtils.defineLazyModuleGetters(this,",
"XPCOMUtils.defineLazyServiceGetters(this,"
];
const imports = [
- /^(?:Cu|Components\.utils)\.import\(".*\/((.*?)\.jsm?)"(?:, this)?\)/
+ /^(?:Cu|Components\.utils|ChromeUtils)\.import\(".*\/((.*?)\.jsm?)"(?:, this)?\)/
];
const workerImportFilenameMatch = /(.*\/)*(.*?\.jsm?)/;
module.exports = {
get modulesGlobalData() {
if (!gModules) {
if (this.isMozillaCentralBased()) {
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-import-into-var-and-global.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-import-into-var-and-global.js
@@ -30,17 +30,17 @@ module.exports = function(context) {
if (node.callee.type === "MemberExpression" &&
node.parent.type === "VariableDeclarator" &&
node.arguments.length <= 2) {
let memexp = node.callee;
// Is the expression starting with `Cu` or `Components.utils`?
let isACu =
((memexp.object.type === "Identifier" &&
- memexp.object.name === "Cu") ||
+ (memexp.object.name === "Cu" || memexp.object.name === "ChromeUtils")) ||
(memexp.object.type === "MemberExpression" &&
memexp.object.object && memexp.object.property &&
memexp.object.object.name === "Components" &&
memexp.object.property.name === "utils"));
if (isACu &&
// Now check its `Cu.import` (or `Components.utils.import`).
memexp.property.type === "Identifier" &&
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-single-arg-cu-import.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-single-arg-cu-import.js
@@ -18,18 +18,19 @@ module.exports = function(context) {
// Public
// --------------------------------------------------------------------------
return {
"CallExpression": function(node) {
if (node.callee.type === "MemberExpression") {
let memexp = node.callee;
if (memexp.object.type === "Identifier" &&
- // Only Cu, not Components.utils; see bug 1230369.
- memexp.object.name === "Cu" &&
+ // Only Cu and ChromeUtils, not Components.utils; see bug 1230369.
+ (memexp.object.name === "Cu" ||
+ memexp.object.name === "ChromeUtils") &&
memexp.property.type === "Identifier" &&
memexp.property.name === "import" &&
node.arguments.length === 1) {
context.report(node, "Single argument Cu.import exposes new " +
"globals to all modules");
}
}
}
--- a/tools/lint/eslint/eslint-plugin-mozilla/tests/test-no-import-into-var-and-global.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/tests/test-no-import-into-var-and-global.js
@@ -27,10 +27,16 @@ ruleTester.run("no-import-into-var-and-g
"var foo = Components.utils.import('fake', {});"
],
invalid: [{
code: "var foo = Cu.import('fake', this);",
errors: [ExpectedError]
}, {
code: "var foo = Cu.import('fake');",
errors: [ExpectedError]
+ }, {
+ code: "var foo = Components.utils.import('fake');",
+ errors: [ExpectedError]
+ }, {
+ code: "var foo = ChromeUtils.import('fake');",
+ errors: [ExpectedError]
}]
});
--- a/tools/lint/eslint/eslint-plugin-mozilla/tests/test-no-single-arg-cu-import.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/tests/test-no-single-arg-cu-import.js
@@ -23,10 +23,13 @@ const ExpectedError = {
ruleTester.run("no-single-arg-cu-import", rule, {
valid: [
"Cu.import('fake', {});"
],
invalid: [{
code: "Cu.import('fake');",
errors: [ExpectedError]
+ }, {
+ code: "ChromeUtils.import('fake');",
+ errors: [ExpectedError]
}]
});