Bug 1388215: Part 2 - Add eslint plugin support for defineLazy*Getters() methods. r?florian draft
authorKris Maglione <maglione.k@gmail.com>
Tue, 08 Aug 2017 14:11:16 -0700
changeset 642902 f67ff4d3ef52
parent 642901 73568e194eeb
child 642903 3f402c830c87
push id72914
push usermaglione.k@gmail.com
push dateTue, 08 Aug 2017 23:49:25 +0000
reviewersflorian
bugs1388215
milestone57.0a1
Bug 1388215: Part 2 - Add eslint plugin support for defineLazy*Getters() methods. r?florian MozReview-Commit-ID: AMX0VbPncmI
tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
--- a/tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
@@ -29,16 +29,21 @@ const callExpressionDefinitions = [
   /^XPCOMUtils\.defineConstant\(this, "(\w+)"/,
   /^DevToolsUtils\.defineLazyModuleGetter\(this, "(\w+)"/,
   /^DevToolsUtils\.defineLazyGetter\(this, "(\w+)"/,
   /^Object\.defineProperty\(this, "(\w+)"/,
   /^Reflect\.defineProperty\(this, "(\w+)"/,
   /^this\.__defineGetter__\("(\w+)"/
 ];
 
+const callExpressionMultiDefinitions = [
+  "XPCOMUtils.defineLazyModuleGetters(this,",
+  "XPCOMUtils.defineLazyServiceGetters(this,"
+];
+
 const imports = [
   /^(?:Cu|Components\.utils)\.import\(".*\/((.*?)\.jsm?)"(?:, this)?\)/
 ];
 
 const workerImportFilenameMatch = /(.*\/)*(.*?\.jsm?)/;
 
 module.exports = {
   get modulesGlobalData() {
@@ -276,16 +281,24 @@ module.exports = {
 
     for (let reg of callExpressionDefinitions) {
       let match = source.match(reg);
       if (match) {
         return [{ name: match[1], writable: 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 }))
+                 .filter(g => g.name);
+    }
+
     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 }));
     }