Skip to content

Commit

Permalink
Output correct names for function shorthands which are part of anonym…
Browse files Browse the repository at this point in the history
…ous objects.

I'm not very happy with this workaround but I don't know of a way to represent named functions of anonymous objects.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=243346816
  • Loading branch information
nbeloglazov authored and lauraharker committed Apr 15, 2019
1 parent 9af13ff commit 9f6ba9b
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/com/google/javascript/jscomp/SymbolTable.java
Expand Up @@ -485,23 +485,30 @@ private void addAnonymousFunctionsInScope(SymbolScope scope) {
if (sym == null) {
// JSCompiler has no symbol for this scope. Check to see if it's a
// local function. If it is, give it a name.
Node rootNode = scope.getRootNode();
if (scope.isLexicalScope()
&& !scope.isGlobalScope()
&& scope.getRootNode() != null
&& !scope.getRootNode().isFromExterns()
&& rootNode != null
&& !rootNode.isFromExterns()
&& scope.getParentScope() != null
&& scope.getRootNode().isFunction()) {
&& rootNode.isFunction()) {
SymbolScope parent = scope.getParentScope();

String innerName = "function%" + scope.getIndexInParent();
JSType type = rootNode.getJSType();

// Functions defined on anonymous objects are considered anonymous as well:
// doFoo({bar() {}});
// bar is not technically anonymous, but it's a method on an anonymous object literal so
// effectively it's anonymous/inaccessible. In this case, slightly correct rootNode to
// be a MEMBER_FUNCTION_DEF node instead of a FUNCTION node.
if (rootNode.getParent().isMemberFunctionDef()) {
rootNode = rootNode.getParent();
}

Symbol anonymousFunctionSymbol =
declareSymbol(
innerName,
scope.getRootNode().getJSType(),
/* inferred= */ true,
parent,
scope.getRootNode(),
/* info= */ null);
innerName, type, /* inferred= */ true, parent, rootNode, /* info= */ null);
scope.setSymbolForScope(anonymousFunctionSymbol);
}
}
Expand Down

0 comments on commit 9f6ba9b

Please sign in to comment.