Bug 1218425 - ESLint rule that warns against aArg notation in function params; r=miker
authorPatrick Brosset <pbrosset@mozilla.com>
Wed, 28 Oct 2015 08:32:02 +0100
changeset 269938 f1cef30cb767723c80ffbe186fd17adfca12cef6
parent 269937 604e693761bdc5c28f11afad54674f3856e7f518
child 269939 75cb7b35f0213a448121f15f50511099fb8c58cb
push id29595
push userkwierso@gmail.com
push dateWed, 28 Oct 2015 23:41:46 +0000
treeherdermozilla-central@769f29c92bb2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmiker
bugs1218425
milestone44.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1218425 - ESLint rule that warns against aArg notation in function params; r=miker
devtools/.eslintrc
testing/eslint-plugin-mozilla/docs/index.rst
testing/eslint-plugin-mozilla/docs/no-aArgs.rst
testing/eslint-plugin-mozilla/lib/index.js
testing/eslint-plugin-mozilla/lib/rules/no-aArgs.js
--- a/devtools/.eslintrc
+++ b/devtools/.eslintrc
@@ -25,16 +25,17 @@
     // These are the rules that have been configured so far to match the
     // devtools coding style.
 
     // Rules from the mozilla plugin
     "mozilla/balanced-listeners": 2,
     "mozilla/components-imports": 1,
     "mozilla/import-headjs-globals": 1,
     "mozilla/mark-test-function-used": 1,
+    "mozilla/no-aArgs": 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}],
--- a/testing/eslint-plugin-mozilla/docs/index.rst
+++ b/testing/eslint-plugin-mozilla/docs/index.rst
@@ -11,16 +11,18 @@ removeEventListener (and does the same f
 ``Cu.import("some/path/Blah.jsm")`` adds Blah to the global scope.
 
 ``import-headjs-globals`` imports globals from head.js and from any files that
 should be imported by head.js (as far as we can correctly resolve the path).
 
 ``mark-test-function-used`` simply marks test (the test method) as used. This
 avoids ESLint telling us that the function is never called.
 
+``no-aArgs`` prevents using the hungarian notation in function arguments.
+
 ``var-only-at-top-level`` Marks all var declarations that are not at the top
 level invalid.
 
 +-------+-----------------------+
 | Possible values for all rules |
 +-------+-----------------------+
 | Value | Meaning               |
 +-------+-----------------------+
@@ -43,9 +45,10 @@ Example configuration::
 
 .. toctree::
    :maxdepth: 1
 
    balanced-listeners
    components-imports
    import-headjs-globals
    mark-test-function-used
+   no-aArgs
    var-only-at-top-level
new file mode 100644
--- /dev/null
+++ b/testing/eslint-plugin-mozilla/docs/no-aArgs.rst
@@ -0,0 +1,12 @@
+.. _no-aArgs:
+
+========
+no-aArgs
+========
+
+Rule Details
+------------
+
+Checks that function argument names don't start with lowercase 'a' followed by a
+capital letter. This is to prevent the use of Hungarian notation whereby the
+first letter is a prefix that indicates the type or intended use of a variable.
--- a/testing/eslint-plugin-mozilla/lib/index.js
+++ b/testing/eslint-plugin-mozilla/lib/index.js
@@ -12,18 +12,20 @@
 //------------------------------------------------------------------------------
 
 module.exports = {
   rules: {
     "balanced-listeners": require("../lib/rules/balanced-listeners"),
     "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"),
+    "no-aArgs": require("../lib/rules/no-aArgs"),
     "var-only-at-top-level": require("../lib/rules/var-only-at-top-level")
   },
   rulesConfig: {
     "balanced-listeners": 0,
     "components-imports": 0,
     "import-headjs-globals": 0,
     "mark-test-function-used": 0,
+    "no-aArgs": 0,
     "var-only-at-top-level": 0
   }
 };
new file mode 100644
--- /dev/null
+++ b/testing/eslint-plugin-mozilla/lib/rules/no-aArgs.js
@@ -0,0 +1,50 @@
+/**
+ * @fileoverview warns against using hungarian notation in function arguments
+ * (i.e. aArg).
+ *
+ * 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
+//------------------------------------------------------------------------------
+
+module.exports = function(context) {
+  //--------------------------------------------------------------------------
+  // Helpers
+  //--------------------------------------------------------------------------
+
+  function isPrefixed(name) {
+    return name.length >= 2 && /^a[A-Z]/.test(name);
+  }
+
+  function deHungarianize(name) {
+    return name.substring(1, 2).toLowerCase() +
+           name.substring(2, name.length);
+  }
+
+  function checkFunction(node) {
+    for (var i = 0; i < node.params.length; i ++) {
+      var param = node.params[i];
+      if (param.name && isPrefixed(param.name)) {
+        context.report(param, "Parameter '{{name}}' uses Hungarian Notation, consider using '{{suggestion}}' instead.", {
+          name: param.name,
+          suggestion: deHungarianize(param.name)
+        });
+      }
+    }
+  }
+
+  //--------------------------------------------------------------------------
+  // Public
+  //--------------------------------------------------------------------------
+
+  return {
+    "FunctionDeclaration": checkFunction,
+    "ArrowFunctionExpression": checkFunction,
+    "FunctionExpression": checkFunction
+  };
+};