Skip to content

Commit

Permalink
Set source info of MEMBER_FUNCTION_DEFs to include only the identifier
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=196915413
  • Loading branch information
lauraharker authored and blickly committed May 17, 2018
1 parent 7af8cf4 commit e1ba9e0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/com/google/javascript/jscomp/parsing/IRFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,8 @@ Node processFunction(FunctionDeclarationTree functionTree) {
member.setStaticMember(functionTree.isStatic);
maybeProcessAccessibilityModifier(functionTree, member, functionTree.access);
node.setDeclaredTypeExpression(node.getDeclaredTypeExpression());
// The source info should only include the identifier, not the entire function expression
setSourceInfo(member, name);
result = member;
} else {
result = node;
Expand Down
16 changes: 16 additions & 0 deletions test/com/google/javascript/jscomp/SymbolTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.javascript.jscomp.SymbolTable.SymbolScope;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.JSDocInfo.Visibility;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token;
import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -217,6 +218,21 @@ public void testObjectLiteralWithNewlineInKey() throws Exception {
assertThat(foo.getName()).isEqualTo("obj.foo\\nbar");
}

public void testObjectLiteralWithMemberFunction() {
String input = lines("var obj = { fn() {} }; obj.fn();");
SymbolTable table = createSymbolTable(input);

Symbol objFn = getGlobalVar(table, "obj.fn");
assertNotNull(objFn);
List<Reference> references = table.getReferenceList(objFn);
assertThat(references).hasSize(2);

// The declaration node corresponds to "fn", not "fn() {}", in the source info.
Node declaration = objFn.getDeclarationNode();
assertThat(declaration.getCharno()).isEqualTo(12);
assertThat(declaration.getLength()).isEqualTo(2);
}

public void testNamespacedReferences() throws Exception {
// Because the type of goog is anonymous, we build its properties into
// the global scope.
Expand Down
24 changes: 24 additions & 0 deletions test/com/google/javascript/jscomp/parsing/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,30 @@ public void testLinenoCharnoObjectLiteral() throws Exception {
assertNode(value).hasCharno(4);
}

public void testLinenoCharnoObjectLiteralMemberFunction() throws Exception {
mode = LanguageMode.ECMASCRIPT6;
Node n = parse("var a = {\n fn() {} };").getFirstFirstChild().getFirstChild();

assertNode(n).hasType(Token.OBJECTLIT);
assertNode(n).hasLineno(1);
assertNode(n).hasCharno(8);

// fn() {}
Node key = n.getFirstChild();

assertNode(key).hasType(Token.MEMBER_FUNCTION_DEF);
assertNode(key).hasLineno(2);
assertNode(key).hasCharno(1);
assertNode(key).hasLength(2); // "fn"

Node value = key.getFirstChild();

assertNode(value).hasType(Token.FUNCTION);
assertNode(value).hasLineno(2);
assertNode(value).hasCharno(1);
assertNode(value).hasLength(7); // "fn() {}"
}

public void testLinenoCharnoAdd() throws Exception {
testLinenoCharnoBinop("+");
}
Expand Down

0 comments on commit e1ba9e0

Please sign in to comment.