Skip to content

Commit

Permalink
Avoid a minor but needless repetitive lookups and string comparison w…
Browse files Browse the repository at this point in the history
…hile parsing.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=192154928
  • Loading branch information
concavelenz authored and tjgq committed Apr 10, 2018
1 parent 5cae9b7 commit 750c245
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
24 changes: 13 additions & 11 deletions src/com/google/javascript/jscomp/parsing/parser/Keywords.java
Expand Up @@ -18,7 +18,6 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

import java.util.EnumMap;
import java.util.Map;

Expand Down Expand Up @@ -102,31 +101,33 @@ public enum Keywords {

public final String value;
public final TokenType type;
private final boolean isTypeScriptSpecificKeyword;

Keywords(String value, TokenType type) {
this.value = value;
this.type = type;
this.isTypeScriptSpecificKeyword = isTypeScriptSpecificKeyword(type);
}

@Override
public String toString() {
return value;
}

public static boolean isKeyword(String value) {
return get(value) != null;
public static boolean isKeyword(String value, boolean includeTypeScriptKeywords) {
return get(value, includeTypeScriptKeywords) != null;
}

public static boolean isKeyword(TokenType token) {
return get(token) != null;
}

public static boolean isTypeScriptSpecificKeyword(String value) {
switch (value) {
case "declare":
case "type":
case "module":
case "namespace":
private static boolean isTypeScriptSpecificKeyword(TokenType type) {
switch (type) {
case DECLARE:
case TYPE:
case MODULE:
case NAMESPACE:
return true;
default:
return false;
Expand Down Expand Up @@ -158,8 +159,9 @@ public static TokenType getTokenType(String value) {
return KEYWORDS_BY_NAME.get(value).type;
}

public static Keywords get(String value) {
return KEYWORDS_BY_NAME.get(value);
public static Keywords get(String value, boolean includeTypeScriptKeywords) {
Keywords k = KEYWORDS_BY_NAME.get(value);
return (k != null && (includeTypeScriptKeywords || !k.isTypeScriptSpecificKeyword)) ? k : null;
}

public static Keywords get(TokenType token) {
Expand Down
12 changes: 5 additions & 7 deletions src/com/google/javascript/jscomp/parsing/parser/Parser.java
Expand Up @@ -281,8 +281,7 @@ public String getSourceMapURL() {

/** Returns true if the string value should be treated as a keyword in the current context. */
private boolean isKeyword(String value) {
return Keywords.isKeyword(value)
&& (!Keywords.isTypeScriptSpecificKeyword(value) || config.parseTypeSyntax);
return Keywords.isKeyword(value, config.parseTypeSyntax);
}

// 14 Program
Expand Down Expand Up @@ -888,7 +887,7 @@ private ParseTree parseClassMemberDeclaration(PartialClassElement partial) {
if (peekIdOrKeyword()) {
nameExpr = null;
name = eatIdOrKeywordAsId();
if (Keywords.isKeyword(name.value)) {
if (Keywords.isKeyword(name.value, /* includeTypeScriptKeywords= */ false)) {
features = features.with(Feature.KEYWORDS_AS_PROPERTIES);
}
} else {
Expand Down Expand Up @@ -2649,8 +2648,8 @@ private ParseTree parsePropertyNameAssignment() {
if (colon == null) {
if (name.type != TokenType.IDENTIFIER) {
reportExpectedError(peekToken(), TokenType.COLON);
} else if (Keywords.isKeyword(name.asIdentifier().value)
&& !Keywords.isTypeScriptSpecificKeyword(name.asIdentifier().value)) {
} else if (Keywords.isKeyword(
name.asIdentifier().value, /* includeTypeScriptKeywords= */ false)) {
reportError(name, "Cannot use keyword in short object literal");
} else if (peek(TokenType.EQUAL)) {
IdentifierExpressionTree idTree = new IdentifierExpressionTree(
Expand Down Expand Up @@ -3673,8 +3672,7 @@ private ParseTree parseObjectPatternField(PatternKind kind) {
name = eatIdOrKeywordAsId();
if (!peek(TokenType.COLON)) {
IdentifierToken idToken = (IdentifierToken) name;
if (Keywords.isKeyword(idToken.value)
&& !Keywords.isTypeScriptSpecificKeyword(idToken.value)) {
if (Keywords.isKeyword(idToken.value, /* includeTypeScriptKeywords = */ false)) {
reportError("cannot use keyword '%s' here.", name);
}
if (peek(TokenType.EQUAL)) {
Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/parsing/parser/Scanner.java
Expand Up @@ -733,8 +733,8 @@ private Token scanIdentifierOrKeyword(int beginToken, char ch) {
return createToken(TokenType.ERROR, beginToken);
}

Keywords k = Keywords.get(value);
if (k != null && (!Keywords.isTypeScriptSpecificKeyword(value) || parseTypeSyntax)) {
Keywords k = Keywords.get(value, parseTypeSyntax);
if (k != null) {
return new Token(k.type, getTokenRange(beginToken));
}

Expand Down

0 comments on commit 750c245

Please sign in to comment.