Skip to content
This repository was archived by the owner on May 30, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions lib/esrefactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
};

function locateDeclaration(ref) {
var node, scope, i, v;
var scope, i, v;

if (ref.resolved) {
return ref.resolved.defs[ref.resolved.defs.length - 1].name;
Expand All @@ -78,7 +78,14 @@
for (i = 0; i < scope.variables.length; ++i) {
v = scope.variables[i];
if (v.name === ref.identifier.name && v.defs.length) {
return v.defs[v.defs.length - 1].name;

// Since we're taking from the end of the array, we can sort
// these so we don't accidentally pick up an implicit definition.

var def = v.defs.length > 1 ? v.defs.sort(function (a) {
return a.type !== 'ImplicitGlobalVariable'
})[v.defs.length - 1] : v.defs[v.defs.length - 1];
return def.type !== 'ImplicitGlobalVariable' ? def.name : null;
}
}
scope = scope.upper;
Expand Down Expand Up @@ -157,7 +164,7 @@
// }

Context.prototype.identify = function (pos) {
var identifier, scopeManager, lookup, result, scope;
var identifier, scopeManager, lookup, result, scope, func;

if (!this._syntax) {
throw new Error('Unable to identify anything without a syntax tree');
Expand All @@ -178,6 +185,13 @@
identifier = node;
return estraverse.VisitorOption.Break;
}
} else if (node.type === esprima.Syntax.FunctionDeclaration) {
if (node.id.range[0] <= pos && node.id.range[1] >= pos) {
func = true;
identifier = node.id;
return estraverse.VisitorOption.Break;
}

}
},
leave: function (node) {
Expand All @@ -192,6 +206,11 @@

result = lookup(scope, identifier);

// If this is a function declaration, we must also check the upper scope.
if (func && !result && scope.upper) {
result = lookup(scope.upper, identifier);
}

if (result) {
// Search for all other identical references (same scope).
result.references = [];
Expand Down Expand Up @@ -263,7 +282,6 @@
}
}

id = identification.identifier.name;
for (i = 0; i < set.length; ++i) {
result = result.slice(0, set[i][0]) + name + result.slice(set[i][1]);
}
Expand Down
8 changes: 8 additions & 0 deletions test/data/function_declaration2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test(123); // reference:test

function test(bar) { // cursor:test declaration:test
if (false) {
test('baz'); // reference:test
}
return x + y
}