Skip to content

Commit

Permalink
Move name sanitizing logic into SymbolTable so that it doesn't have t…
Browse files Browse the repository at this point in the history
…o run over and over for the same Symbol.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=179497476
  • Loading branch information
tbreisacher committed Dec 21, 2017
1 parent 881fb05 commit 86ca586
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/com/google/javascript/jscomp/SymbolTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -642,9 +643,21 @@ private Symbol copySymbolTo(
sym.getJSDocInfo());
}

/**
* Replace all \ with \\ so there will be no \0 or \n in the string, then replace all '\0'
* (NULL) with \0 and all '\n' (newline) with \n.
*/
private static String sanitizeSpecialChars(String s) {
return s.replaceAll("\\\\", Matcher.quoteReplacement("\\\\"))
.replaceAll("\0", Matcher.quoteReplacement("\\0"))
.replaceAll("\n", Matcher.quoteReplacement("\\n"));
}

private Symbol addSymbol(
String name, JSType type, boolean inferred, SymbolScope scope,
Node declNode) {
name = sanitizeSpecialChars(name);

Symbol symbol = new Symbol(name, type, inferred, scope);
Symbol replacedSymbol = symbols.put(declNode, name, symbol);
Preconditions.checkState(
Expand Down
30 changes: 25 additions & 5 deletions test/com/google/javascript/jscomp/SymbolTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,27 @@ public void testLocalThisReferences3() throws Exception {
assertNull(t);
}

public void testObjectLiteral() throws Exception {
SymbolTable table = createSymbolTable("var obj = {foo: 0};");

Symbol foo = getGlobalVar(table, "obj.foo");
assertThat(foo.getName()).isEqualTo("foo");
}

public void testObjectLiteralQuoted() throws Exception {
SymbolTable table = createSymbolTable("var obj = {'foo': 0};");

Symbol foo = getGlobalVar(table, "obj.foo");
assertThat(foo.getName()).isEqualTo("foo");
}

public void testObjectLiteralWithNewlineInKey() throws Exception {
SymbolTable table = createSymbolTable("var obj = {'foo\\nbar': 0};");

Symbol foo = getGlobalVar(table, "obj.foo\\nbar");
assertThat(foo.getName()).isEqualTo("obj.foo\\nbar");
}

public void testNamespacedReferences() throws Exception {
// Because the type of goog is anonymous, we build its properties into
// the global scope.
Expand Down Expand Up @@ -1156,8 +1177,7 @@ private Symbol getLocalVar(SymbolTable table, String name) {
private List<Symbol> getVars(SymbolTable table) {
List<Symbol> result = new ArrayList<>();
for (Symbol symbol : table.getAllSymbols()) {
if (symbol.getDeclaration() != null &&
!symbol.getDeclaration().getNode().isFromExterns()) {
if (symbol.getDeclaration() != null && !symbol.getDeclaration().getNode().isFromExterns()) {
result.add(symbol);
}
}
Expand Down Expand Up @@ -1194,9 +1214,9 @@ private SymbolTable assertSymbolTableValid(SymbolTable table) {

Symbol scope = table.getSymbolForScope(table.getScope(sym));
assertTrue(
"The symbol's scope is a zombie scope that shouldn't exist.\n" +
"Symbol: " + sym + "\n" +
"Scope: " + table.getScope(sym),
"The symbol's scope is a zombie scope that shouldn't exist.\n"
+ "Symbol: " + sym + "\n"
+ "Scope: " + table.getScope(sym),
scope == null || allSymbols.contains(scope));
}

Expand Down

0 comments on commit 86ca586

Please sign in to comment.