Bug 1464548: Part 2 - Add ESLint support for defineLazyGlobalGetters. r?standard8
MozReview-Commit-ID: 38hk1MquFrg
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
@@ -32,16 +32,17 @@ const callExpressionDefinitions = [
/^DevToolsUtils\.defineLazyModuleGetter\(this, "(\w+)"/,
/^DevToolsUtils\.defineLazyGetter\(this, "(\w+)"/,
/^Object\.defineProperty\(this, "(\w+)"/,
/^Reflect\.defineProperty\(this, "(\w+)"/,
/^this\.__defineGetter__\("(\w+)"/
];
const callExpressionMultiDefinitions = [
+ "XPCOMUtils.defineLazyGlobalGetters(this,",
"XPCOMUtils.defineLazyModuleGetters(this,",
"XPCOMUtils.defineLazyServiceGetters(this,"
];
const imports = [
/^(?:Cu|Components\.utils|ChromeUtils)\.import\(".*\/((.*?)\.jsm?)"(?:, this)?\)/
];
@@ -301,21 +302,28 @@ module.exports = {
for (let reg of callExpressionDefinitions) {
let match = source.match(reg);
if (match) {
return [{ name: match[1], writable: true, explicit: true }];
}
}
if (callExpressionMultiDefinitions.some(expr => source.startsWith(expr)) &&
- node.expression.arguments[1] &&
- node.expression.arguments[1].type === "ObjectExpression") {
- return node.expression.arguments[1].properties
- .map(p => ({ name: p.type === "Property" && p.key.name, writable: true, explicit: true }))
- .filter(g => g.name);
+ node.expression.arguments[1]) {
+ let arg = node.expression.arguments[1];
+ if (arg.type === "ObjectExpression") {
+ return arg.properties
+ .map(p => ({ name: p.type === "Property" && p.key.name, writable: true, explicit: true }))
+ .filter(g => g.name);
+ }
+ if (arg.type === "ArrayExpression") {
+ return arg.elements
+ .map(p => ({ name: p.type === "Literal" && p.value, writable: true, explicit: true }))
+ .filter(g => typeof g.name == "string");
+ }
}
if (node.expression.callee.type == "MemberExpression" &&
node.expression.callee.property.type == "Identifier" &&
node.expression.callee.property.name == "defineLazyScriptGetter") {
// The case where we have a single symbol as a string has already been
// handled by the regexp, so we have an array of symbols here.
return node.expression.arguments[1].elements.map(n => ({ name: n.value, writable: true, explicit: true }));