Skip to content

Commit

Permalink
Don't bother creating a 'this' node for empty functions.
Browse files Browse the repository at this point in the history
Unblocks some upcoming changes to ES6 class transpilation.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=158320031
  • Loading branch information
tbreisacher authored and brad4d committed Jun 8, 2017
1 parent c7ce36b commit aa55684
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
42 changes: 18 additions & 24 deletions src/com/google/javascript/jscomp/SymbolTable.java
Expand Up @@ -1504,32 +1504,26 @@ public void enterScope(NodeTraversal t) {
}
} else {
// Otherwise, declare a "this" property when possible.
SymbolScope scope = scopes.get(t.getScopeRoot());
Preconditions.checkNotNull(scope, "No scope found for node: %s", t.getScopeRoot());
Symbol scopeSymbol = getSymbolForScope(scope);
if (scopeSymbol != null) {
SymbolScope propScope = scopeSymbol.getPropertyScope();
if (propScope != null) {
// If a function is assigned multiple times, we only want
// one addressable "this" symbol.
symbol = propScope.getOwnSlot("this");
if (symbol == null) {
JSType rootType = t.getScopeRoot().getJSType();
FunctionType fnType = rootType == null
? null : rootType.toMaybeFunctionType();
JSType type = fnType == null
? null : fnType.getTypeOfThis();
symbol = addSymbol(
"this",
type,
false /* declared */,
scope,
t.getScopeRoot());
Node scopeRoot = t.getScopeRoot();
SymbolScope scope = scopes.get(scopeRoot);
if (NodeUtil.getFunctionBody(scopeRoot).hasChildren()) {
Symbol scopeSymbol = getSymbolForScope(scope);
if (scopeSymbol != null) {
SymbolScope propScope = scopeSymbol.getPropertyScope();
if (propScope != null) {
// If a function is assigned multiple times, we only want
// one addressable "this" symbol.
symbol = propScope.getOwnSlot("this");
if (symbol == null) {
JSType rootType = t.getScopeRoot().getJSType();
FunctionType fnType = rootType == null ? null : rootType.toMaybeFunctionType();
JSType type = fnType == null ? null : fnType.getTypeOfThis();
symbol = addSymbol("this", type, false /* declared */, scope, t.getScopeRoot());
}
}

// TODO(nicksantos): It's non-obvious where the declaration of
// the 'this' symbol should be. Figure this out later.
}
} else {
logger.warning("Skipping empty function: " + scopeRoot);
}
}

Expand Down
6 changes: 2 additions & 4 deletions test/com/google/javascript/jscomp/SymbolTableTest.java
Expand Up @@ -189,6 +189,7 @@ public void testLocalThisReferences2() throws Exception {
assertThat(refs).hasSize(2);
}

// No 'this' reference is created for empty functions.
public void testLocalThisReferences3() throws Exception {
SymbolTable table = createSymbolTable(
"/** @constructor */ function F() {}");
Expand All @@ -197,10 +198,7 @@ public void testLocalThisReferences3() throws Exception {
assertNotNull(baz);

Symbol t = table.getParameterInFunction(baz, "this");
assertNotNull(t);

List<Reference> refs = table.getReferenceList(t);
assertThat(refs).isEmpty();
assertNull(t);
}

public void testNamespacedReferences() throws Exception {
Expand Down

0 comments on commit aa55684

Please sign in to comment.