Bug 1205814 - mozilla eslint plug: allow top level var only r=pbrosset
authorMichael Ratcliffe <mratcliffe@mozilla.com>
Mon, 26 Oct 2015 14:39:47 +0000
changeset 290902 da7f20651f168a5a6c43a1930d2db827e3379f9e
parent 290901 9aa057fb1f7410f7d9ffffe4c2752f9adbd9c353
child 290903 21b2dfbe1f24080c03ae8b698ea66e77f9acd236
push id8654
push userraliiev@mozilla.com
push dateThu, 29 Oct 2015 11:48:40 +0000
treeherdermozilla-aurora@bc4551debe17 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspbrosset
bugs1205814
milestone44.0a1
Bug 1205814 - mozilla eslint plug: allow top level var only r=pbrosset
devtools/.eslintrc
testing/eslint-plugin-mozilla/docs/index.rst
testing/eslint-plugin-mozilla/docs/var-only-at-top-level.rst
testing/eslint-plugin-mozilla/lib/index.js
testing/eslint-plugin-mozilla/lib/rules/var-only-at-top-level.js
--- a/devtools/.eslintrc
+++ b/devtools/.eslintrc
@@ -24,16 +24,17 @@
   "rules": {
     // These are the rules that have been configured so far to match the
     // devtools coding style.
 
     // Rules from the mozilla plugin
     "mozilla/components-imports": 1,
     "mozilla/import-headjs-globals": 1,
     "mozilla/mark-test-function-used": 1,
+    "mozilla/var-only-at-top-level": 1,
 
     // Disallow using variables outside the blocks they are defined (especially
     // since only let and const are used, see "no-var").
     "block-scoped-var": 2,
     // Enforce one true brace style (opening brace on the same line) and avoid
     // start and end braces on the same line.
     "brace-style": [2, "1tbs", {"allowSingleLine": false}],
     // Require camel case names
@@ -265,18 +266,19 @@
     "no-unneeded-ternary": 2,
     // Disallow unreachable statements after a return, throw, continue, or break
     // statement.
     "no-unreachable": 2,
     // Disallow declaration of variables that are not used in the code
     "no-unused-vars": 2,
     // Allow using variables before they are defined.
     "no-use-before-define": 0,
-    // Require let or const instead of var.
-    "no-var": 2,
+    // We use var-only-at-top-level instead of no-var as we allow top level
+    // vars.
+    "no-var": 0,
     // Allow using TODO/FIXME comments.
     "no-warning-comments": 0,
     // Disallow use of the with statement.
     "no-with": 2,
     // Don't require method and property shorthand syntax for object literals.
     // We use this in the code a lot, but not consistently, and this seems more
     // like something to check at code review time.
     "object-shorthand": 0,
--- a/testing/eslint-plugin-mozilla/docs/index.rst
+++ b/testing/eslint-plugin-mozilla/docs/index.rst
@@ -26,16 +26,18 @@ avoids ESLint telling us that the functi
 +-------+-----------------------+
 
 Example configuration::
 
    "rules": {
      "mozilla/components-imports": 1,
      "mozilla/import-headjs-globals": 1,
      "mozilla/mark-test-function-used": 1,
+     "mozilla/var-only-at-top-level": 1,
    }
 
 .. toctree::
    :maxdepth: 1
 
    components-imports
    import-headjs-globals
    mark-test-function-used
+   var-only-at-top-level
new file mode 100644
--- /dev/null
+++ b/testing/eslint-plugin-mozilla/docs/var-only-at-top-level.rst
@@ -0,0 +1,10 @@
+.. _var-only-at-top-level:
+
+=======================
+var-only-at-top-level
+=======================
+
+Rule Details
+------------
+
+Marks all var declarations that are not at the top level invalid.
--- a/testing/eslint-plugin-mozilla/lib/index.js
+++ b/testing/eslint-plugin-mozilla/lib/index.js
@@ -10,16 +10,18 @@
 //------------------------------------------------------------------------------
 // Plugin Definition
 //------------------------------------------------------------------------------
 
 module.exports = {
   rules: {
     "components-imports": require("../lib/rules/components-imports"),
     "import-headjs-globals": require("../lib/rules/import-headjs-globals"),
-    "mark-test-function-used": require("../lib/rules/mark-test-function-used")
+    "mark-test-function-used": require("../lib/rules/mark-test-function-used"),
+    "var-only-at-top-level": require("../lib/rules/var-only-at-top-level")
   },
   rulesConfig: {
     "components-imports": 0,
     "import-headjs-globals": 0,
-    "mark-test-function-used": 0
+    "mark-test-function-used": 0,
+    "var-only-at-top-level": 0
   }
 };
new file mode 100644
--- /dev/null
+++ b/testing/eslint-plugin-mozilla/lib/rules/var-only-at-top-level.js
@@ -0,0 +1,36 @@
+/**
+ * @fileoverview Marks all var declarations that are not at the top level
+ *               invalid.
+ *
+ * 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
+//------------------------------------------------------------------------------
+
+var helpers = require("../helpers");
+
+module.exports = function(context) {
+  //--------------------------------------------------------------------------
+  // Public
+  //--------------------------------------------------------------------------
+
+  return {
+    "VariableDeclaration": function(node) {
+      if (node.kind === "var") {
+        var ancestors = context.getAncestors();
+        var parent = ancestors.pop();
+
+        if (parent.type === "Program") {
+          return;
+        }
+
+        context.report(node, "Unexpected var, use let or const instead.");
+      }
+    }
+  };
+};