summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/no-func-assign.js
diff options
context:
space:
mode:
authorYosuke Furukawa <yosuke.furukawa@gmail.com>2015-04-29 02:03:05 +0900
committerYosuke Furukawa <yosuke.furukawa@gmail.com>2015-05-09 12:09:52 +0900
commitf9dd34d301ab385ae316769b85ef916f9b70b6f6 (patch)
tree9ce5db7bdff46e587535de5549eef7e02656f5d8 /tools/eslint/lib/rules/no-func-assign.js
parent5883a59b21a97e8b7339f435c977155a2c29ba8d (diff)
downloadandroid-node-v8-f9dd34d301ab385ae316769b85ef916f9b70b6f6.tar.gz
android-node-v8-f9dd34d301ab385ae316769b85ef916f9b70b6f6.tar.bz2
android-node-v8-f9dd34d301ab385ae316769b85ef916f9b70b6f6.zip
tools: replace closure-linter with eslint
PR-URL: https://github.com/iojs/io.js/pull/1539 Fixes: https://github.com/iojs/io.js/issues/1253 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com> Reviewed-By: Johan Bergström <bugs@bergstroem.nu> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Diffstat (limited to 'tools/eslint/lib/rules/no-func-assign.js')
-rw-r--r--tools/eslint/lib/rules/no-func-assign.js81
1 files changed, 81 insertions, 0 deletions
diff --git a/tools/eslint/lib/rules/no-func-assign.js b/tools/eslint/lib/rules/no-func-assign.js
new file mode 100644
index 0000000000..a9f2858126
--- /dev/null
+++ b/tools/eslint/lib/rules/no-func-assign.js
@@ -0,0 +1,81 @@
+/**
+ * @fileoverview Rule to flag use of function declaration identifiers as variables.
+ * @author Ian Christian Myers
+ * @copyright 2013 Ian Christian Myers. All rights reserved.
+ */
+
+"use strict";
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+module.exports = function(context) {
+
+ //--------------------------------------------------------------------------
+ // Helpers
+ //--------------------------------------------------------------------------
+
+ /*
+ * Walk the scope chain looking for either a FunctionDeclaration or a
+ * VariableDeclaration with the same name as left-hand side of the
+ * AssignmentExpression. If we find the FunctionDeclaration first, then we
+ * warn, because a FunctionDeclaration is trying to become a Variable or a
+ * FunctionExpression. If we find a VariableDeclaration first, then we have
+ * a legitimate shadow variable.
+ */
+ function checkIfIdentifierIsFunction(scope, name) {
+ var variable,
+ def,
+ i,
+ j;
+
+ // Loop over all of the identifiers available in scope.
+ for (i = 0; i < scope.variables.length; i++) {
+ variable = scope.variables[i];
+
+ // For each identifier, see if it was defined in _this_ scope.
+ for (j = 0; j < variable.defs.length; j++) {
+ def = variable.defs[j];
+
+ // Identifier is a function and was declared in this scope
+ if (def.type === "FunctionName" && def.name.name === name) {
+ return true;
+ }
+
+ // Identifier is a variable and was declared in this scope. This
+ // is a legitimate shadow variable.
+ if (def.name && def.name.name === name) {
+ return false;
+ }
+ }
+ }
+
+ // Check the upper scope.
+ if (scope.upper) {
+ return checkIfIdentifierIsFunction(scope.upper, name);
+ }
+
+ // We've reached the global scope and haven't found anything.
+ return false;
+ }
+
+ //--------------------------------------------------------------------------
+ // Public API
+ //--------------------------------------------------------------------------
+
+ return {
+
+ "AssignmentExpression": function(node) {
+ var scope = context.getScope(),
+ name = node.left.name;
+
+ if (checkIfIdentifierIsFunction(scope, name)) {
+ context.report(node, "'{{name}}' is a function.", { name: name });
+ }
+
+ }
+
+ };
+
+};