Bug 424951 - Incorrect reference marked in function declaration

Change-Id: I33b9cc57150978ffbdca82ada9bb6e850140b600
Signed-off-by: Mike Rennie <Michael_Rennie@ca.ibm.com>
diff --git a/bundles/org.eclipse.orion.client.javascript/web/javascript/occurrences.js b/bundles/org.eclipse.orion.client.javascript/web/javascript/occurrences.js
index 48e65c7..b3a5a04 100644
--- a/bundles/org.eclipse.orion.client.javascript/web/javascript/occurrences.js
+++ b/bundles/org.eclipse.orion.client.javascript/web/javascript/occurrences.js
@@ -47,6 +47,8 @@
 				case Estraverse.Syntax.Program:
 					this.occurrences = [];
 					this.scopes = [{range: node.range, occurrences: []}];
+					this.defnode = null;
+					this.defscope = null;
 					break;
 				case Estraverse.Syntax.FunctionDeclaration:
 					this.checkId(node.id, this.FUNCTION, true);
@@ -73,9 +75,6 @@
 				case Estraverse.Syntax.AssignmentExpression:
 					var leftNode = node.left;
 					this.checkId(leftNode);
-					if (leftNode.type === Estraverse.Syntax.MemberExpression) {
-						this.checkId(leftNode.object);
-					}
 					this.checkId(node.right);
 					break;
 				case Estraverse.Syntax.ArrayExpression: 
@@ -154,12 +153,12 @@
 				node.type === Estraverse.Syntax.FunctionExpression ||
 				node.type === Estraverse.Syntax.Program) {
 				//if we leave the defining scope
-				var scope = this.scopes.pop();
-				var len = scope.occurrences.length;
-				for(var i = 0; i < len; i++) {
-					this.occurrences.push(scope.occurrences[i]);
-				}
 				if(this.defscope) {
+					var scope = this.scopes.pop();
+					var len = scope.occurrences.length;
+					for(var i = 0; i < len; i++) {
+						this.occurrences.push(scope.occurrences[i]);
+					}
 					if(this.defscope.range[0] === scope.range[0] && this.defscope.range[1] === scope.range[1]) {
 						//we just popped out of the scope the word was defined in, we can quit
 						return Estraverse.VisitorOption.Break;
diff --git a/bundles/org.eclipse.orion.client.javascript/web/js-tests/occurrencesTests.js b/bundles/org.eclipse.orion.client.javascript/web/js-tests/occurrencesTests.js
index be73847..7851455 100644
--- a/bundles/org.eclipse.orion.client.javascript/web/js-tests/occurrencesTests.js
+++ b/bundles/org.eclipse.orion.client.javascript/web/js-tests/occurrencesTests.js
@@ -337,5 +337,117 @@
 			}
 		});
 	};
+
+	/**
+	 * Tests function decls with same named params / vars in same scope
+	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=424951
+	 */
+	Tests.test_functionDeclUse1 = function() {
+		editorContext.text = "var foo = function() {function f(prob) {} function f2() {var prob = {};	prob.foo = null;return prob;}};";
+		return occurrences.computeOccurrences(editorContext, setContext(36, 36)).then(function(results) {
+			try {
+				assertOccurrences(results, [{start:33, end:37}]);
+			}
+			finally {
+				tearDown();
+			}
+		});
+	};
+	
+	/**
+	 * Tests function decls with same named params / vars in same scope
+	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=424951
+	 */
+	Tests.test_functionDeclUse2 = function() {
+		editorContext.text = "var foo = function() {function f(prob) {} function f2() {var prob = {};	prob.foo = null;return prob;}};";
+		return occurrences.computeOccurrences(editorContext, setContext(64, 64)).then(function(results) {
+			try {
+				assertOccurrences(results, [{start:61, end:65}, {start:72, end:76}, {start:95, end:99}]);
+			}
+			finally {
+				tearDown();
+			}
+		});
+	};
+	
+	/**
+	 * Tests function decls with same named params / vars in same scope
+	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=424951
+	 */
+	Tests.test_functionDeclUse3 = function() {
+		editorContext.text = "var foo = function() {function f(prob) {} function f2() {var prob = {};	prob.foo = null;return prob;}};";
+		return occurrences.computeOccurrences(editorContext, setContext(75, 75)).then(function(results) {
+			try {
+				assertOccurrences(results, [{start:61, end:65}, {start:72, end:76}, {start:95, end:99}]);
+			}
+			finally {
+				tearDown();
+			}
+		});
+	};
+	
+	/**
+	 * Tests function decls with same named params / vars in same scope
+	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=424951
+	 */
+	Tests.test_functionDeclUse4 = function() {
+		editorContext.text = "var foo = function() {function f(prob) {} function f2() {var prob = {};	prob.foo = null;return prob;}};";
+		return occurrences.computeOccurrences(editorContext, setContext(98, 98)).then(function(results) {
+			try {
+				assertOccurrences(results, [{start:61, end:65}, {start:72, end:76}, {start:95, end:99}]);
+			}
+			finally {
+				tearDown();
+			}
+		});
+	};
+	
+	/**
+	 * Tests function decls with same named params / vars in same scope
+	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=424951
+	 */
+	Tests.test_functionDeclUse5 = function() {
+		editorContext.text = "var bar = function() {function MyObject() {}; var o = {i: function() {return new MyObject().foo;}};return MyObject;};";
+		return occurrences.computeOccurrences(editorContext, setContext(36, 36)).then(function(results) {
+			try {
+				assertOccurrences(results, [{start:31, end:39}, {start:81, end:89}, {start:106, end:114}]);
+			}
+			finally {
+				tearDown();
+			}
+		});
+	};
+	
+	/**
+	 * Tests function decls with same named params / vars in same scope
+	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=424951
+	 */
+	Tests.test_functionDeclUse6 = function() {
+		editorContext.text = "var bar = function() {function MyObject() {}; var o = {i: function() {return new MyObject().foo;}};return MyObject;};";
+		return occurrences.computeOccurrences(editorContext, setContext(86, 86)).then(function(results) {
+			try {
+				assertOccurrences(results, [{start:31, end:39}, {start:81, end:89}, {start:106, end:114}]);
+			}
+			finally {
+				tearDown();
+			}
+		});
+	};
+	
+	/**
+	 * Tests function decls with same named params / vars in same scope
+	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=424951
+	 */
+	Tests.test_functionDeclUse7 = function() {
+		editorContext.text = "var bar = function() {function MyObject() {}; var o = {i: function() {return new MyObject().foo;}};return MyObject;};";
+		return occurrences.computeOccurrences(editorContext, setContext(111, 111)).then(function(results) {
+			try {
+				assertOccurrences(results, [{start:31, end:39}, {start:81, end:89}, {start:106, end:114}]);
+			}
+			finally {
+				tearDown();
+			}
+		});
+	};
 	return Tests;
 });
\ No newline at end of file