Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add originating function info to onLocalDeclaration #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 14 additions & 6 deletions luaparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1317,15 +1317,15 @@
}

// Add identifier name to the current scope if it doesnt already exist.
function scopeIdentifierName(name) {
if (options.onLocalDeclaration) options.onLocalDeclaration(name);
function scopeIdentifierName(name, data) {
if (options.onLocalDeclaration) options.onLocalDeclaration(name, data);
if (-1 !== indexOf(scopes[scopeDepth], name)) return;
scopes[scopeDepth].push(name);
}

// Add identifier to the current scope
function scopeIdentifier(node) {
scopeIdentifierName(node.name);
function scopeIdentifier(node, data) {
scopeIdentifierName(node.name, data);
attachScope(node, true);
}

Expand Down Expand Up @@ -1838,6 +1838,8 @@
var parameters = [];
expect('(');

var parameterIndex = (name && name.indexer === ':') ? 1 : 0;

// The declaration has arguments
if (!consume(')')) {
// Arguments are a comma separated list of identifiers, optionally ending
Expand All @@ -1846,7 +1848,10 @@
if (Identifier === token.type) {
var parameter = parseIdentifier();
// Function parameters are local.
if (options.scope) scopeIdentifier(parameter);
if (options.scope) scopeIdentifier(parameter, {
parameterOf: name,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name contains either a node with the function name or null (for function expressions); function names, even if present, are themselves subject to scoping and therefore potentially ambiguous. For the originally given use case of code completion, it would be useful to distinguish a local function foo() defined on line 15 from a function foo() defined on line 75.

parameterIndex: parameterIndex
});

parameters.push(parameter);

Expand Down Expand Up @@ -1897,7 +1902,10 @@
pushLocation(marker);
name = parseIdentifier();
base = finishNode(ast.memberExpression(base, ':', name));
if (options.scope) scopeIdentifierName('self');
if (options.scope) scopeIdentifierName('self', {
parameterOf: base,
parameterIndex: 0
});
}

return base;
Expand Down