Skip to content

Commit 597c475

Browse files
Closure Teamcopybara-github
Closure Team
authored andcommitted
Move the value property for IdentifierToken to a getter method with specific methods for common tasks
PiperOrigin-RevId: 722824771
1 parent 05b6eeb commit 597c475

File tree

3 files changed

+50
-30
lines changed

3 files changed

+50
-30
lines changed

src/com/google/javascript/jscomp/parsing/IRFactory.java

+17-10
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,11 @@ void setLengthFrom(Node node, Node ref) {
12111211

12121212
private class TransformDispatcher {
12131213

1214+
// For now, this is just a pass-through. Soon will distinguish based on an IdentifierType.
1215+
String getIdentifierValue(IdentifierToken identifierToken) {
1216+
return identifierToken.getValue();
1217+
}
1218+
12141219
/**
12151220
* Transforms the given object key `input` into a Node with token `output`.
12161221
*
@@ -1527,7 +1532,8 @@ Node processBreakStatement(BreakStatementTree statementNode) {
15271532

15281533
Node transformLabelName(IdentifierToken token) {
15291534
Node label =
1530-
newStringNodeWithNonJSDocComment(Token.LABEL_NAME, token.value, token.getStart());
1535+
newStringNodeWithNonJSDocComment(
1536+
Token.LABEL_NAME, getIdentifierValue(token), token.getStart());
15311537
setSourceInfo(label, token);
15321538
return label;
15331539
}
@@ -1919,7 +1925,7 @@ Node processFunction(FunctionDeclarationTree functionTree) {
19191925

19201926
if (isMember) {
19211927
setSourceInfo(node, functionTree);
1922-
Node member = newStringNode(Token.MEMBER_FUNCTION_DEF, name.value);
1928+
Node member = newStringNode(Token.MEMBER_FUNCTION_DEF, getIdentifierValue(name));
19231929
member.addChildToBack(node);
19241930
member.setStaticMember(functionTree.isStatic);
19251931
// The source info should only include the identifier, not the entire function expression
@@ -1938,7 +1944,7 @@ Node processField(FieldDeclarationTree tree) {
19381944

19391945
Node node =
19401946
newStringNodeWithNonJSDocComment(
1941-
Token.MEMBER_FIELD_DEF, tree.name.value, tree.getStart());
1947+
Token.MEMBER_FIELD_DEF, getIdentifierValue(tree.name), tree.getStart());
19421948
if (tree.initializer != null) {
19431949
Node initializer = transform(tree.initializer);
19441950
node.addChildToBack(initializer);
@@ -2170,7 +2176,7 @@ Node processName(IdentifierExpressionTree nameNode) {
21702176
Node processName(IdentifierToken identifierToken, Token output) {
21712177
NonJSDocComment comment = parseNonJSDocCommentAt(identifierToken.getStart(), true);
21722178

2173-
Node node = newStringNode(output, identifierToken.value);
2179+
Node node = newStringNode(output, getIdentifierValue(identifierToken));
21742180

21752181
if (output == Token.NAME) {
21762182
maybeWarnReservedKeyword(identifierToken);
@@ -2226,7 +2232,7 @@ Node processNameWithInlineComments(IdentifierToken identifierToken) {
22262232
NonJSDocComment comment = parseNonJSDocCommentAt(identifierToken.getStart(), false);
22272233

22282234
maybeWarnReservedKeyword(identifierToken);
2229-
Node node = newStringNode(Token.NAME, identifierToken.value);
2235+
Node node = newStringNode(Token.NAME, getIdentifierValue(identifierToken));
22302236

22312237
if (info != null) {
22322238
node.setJSDocInfo(info);
@@ -2254,7 +2260,7 @@ private void maybeWarnKeywordProperty(Node node) {
22542260
}
22552261

22562262
private void maybeWarnReservedKeyword(IdentifierToken token) {
2257-
String identifier = token.value;
2263+
String identifier = getIdentifierValue(token);
22582264
boolean isIdentifier = false;
22592265
if (TokenStream.isKeyword(identifier)) {
22602266
features = features.with(Feature.ES3_KEYWORDS_AS_IDENTIFIERS);
@@ -2463,7 +2469,7 @@ Node processPropertyGet(MemberExpressionTree getNode) {
24632469

24642470
Node getProp =
24652471
newStringNodeWithNonJSDocComment(
2466-
Token.GETPROP, propName.value, getNode.memberName.getStart());
2472+
Token.GETPROP, getIdentifierValue(propName), getNode.memberName.getStart());
24672473
getProp.addChildToBack(leftChild);
24682474
setSourceInfo(getProp, propName);
24692475
maybeWarnKeywordProperty(getProp);
@@ -2481,7 +2487,7 @@ Node processOptChainPropertyGet(OptionalMemberExpressionTree getNode) {
24812487

24822488
Node getProp =
24832489
newStringNodeWithNonJSDocComment(
2484-
Token.OPTCHAIN_GETPROP, propName.value, getNode.memberName.getStart());
2490+
Token.OPTCHAIN_GETPROP, getIdentifierValue(propName), getNode.memberName.getStart());
24852491
getProp.addChildToBack(leftChild);
24862492
getProp.setIsOptionalChainStart(getNode.isStartOfOptionalChain);
24872493
setSourceInfo(getProp, propName);
@@ -2928,7 +2934,7 @@ private boolean validateClassConstructorMember(ParseTree member) {
29282934
}
29292935

29302936
if (!memberName.type.equals(TokenType.IDENTIFIER)
2931-
|| !memberName.asIdentifier().value.equals("constructor")) {
2937+
|| !memberName.asIdentifier().valueEquals("constructor")) {
29322938
// There's only a potential issue if the member is named "constructor".
29332939
// TODO(b/123769080): Also check for quoted string literals with the value "constructor".
29342940
return false;
@@ -3022,7 +3028,8 @@ Node processImportDecl(ImportDeclarationTree tree) {
30223028
// changed to use only "{foo}" part.
30233029
setSourceInfo(secondChild, tree);
30243030
} else {
3025-
secondChild = newStringNode(Token.IMPORT_STAR, tree.nameSpaceImportIdentifier.value);
3031+
secondChild =
3032+
newStringNode(Token.IMPORT_STAR, getIdentifierValue(tree.nameSpaceImportIdentifier));
30263033
setSourceInfo(secondChild, tree.nameSpaceImportIdentifier);
30273034
}
30283035
Node thirdChild = processString(tree.moduleSpecifier);

src/com/google/javascript/jscomp/parsing/parser/IdentifierToken.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
/** A token representing an identifier. */
2222
public class IdentifierToken extends Token {
23+
private final String value;
24+
2325
public IdentifierToken(SourceRange location, String value) {
2426
super(TokenType.IDENTIFIER, location);
2527
this.value = value;
@@ -30,5 +32,21 @@ public String toString() {
3032
return value;
3133
}
3234

33-
public final String value;
35+
public boolean isKeyword() {
36+
return Keywords.isKeyword(value);
37+
}
38+
39+
public boolean valueEquals(String str) {
40+
return value.equals(str);
41+
}
42+
43+
/**
44+
* Gets the value of the identifier.
45+
*
46+
* <p>Prefer calling {@link #isKeyword()} or {@link #valueEquals(String)} if those methods meet
47+
* your needs.
48+
*/
49+
public String getValue() {
50+
return value;
51+
}
3452
}

src/com/google/javascript/jscomp/parsing/parser/Parser.java

+14-19
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,6 @@ public FeatureSet getFeatures() {
257257
return sourceMapURL;
258258
}
259259

260-
/** Returns true if the string value should be treated as a keyword in the current context. */
261-
private boolean isKeyword(String value) {
262-
return Keywords.isKeyword(value);
263-
}
264-
265260
// 14 Program
266261
public @Nullable ProgramTree parseProgram() {
267262
try {
@@ -394,7 +389,7 @@ private ParseTree parseImportSpecifier() {
394389
if (peekPredefinedString(PredefinedName.AS)) {
395390
eatPredefinedString(PredefinedName.AS);
396391
destinationName = eatId();
397-
} else if (isKeyword(importedName.value)) {
392+
} else if (importedName.isKeyword()) {
398393
reportExpectedError(null, PredefinedName.AS);
399394
}
400395
return new ImportSpecifierTree(getTreeLocation(start), importedName, destinationName);
@@ -481,8 +476,8 @@ private ParseTree parseExportDeclaration() {
481476
} else if (isExportSpecifier) {
482477
for (ParseTree tree : exportSpecifierList) {
483478
IdentifierToken importedName = tree.asExportSpecifier().importedName;
484-
if (isKeyword(importedName.value)) {
485-
reportError(importedName, "cannot use keyword '%s' here.", importedName.value);
479+
if (importedName.isKeyword()) {
480+
reportError(importedName, "cannot use keyword '%s' here.", importedName);
486481
}
487482
}
488483
}
@@ -646,7 +641,7 @@ private void parseClassElementName(ClassOrObjectElementInfo elementInfo) {
646641
if (peekPropertyName(0)) {
647642
if (peekIdOrKeyword()) {
648643
elementInfo.setName(eatIdOrKeywordAsId());
649-
if (Keywords.isKeyword(elementInfo.getName().value)) {
644+
if (elementInfo.getName().isKeyword()) {
650645
recordFeatureUsed(Feature.KEYWORDS_AS_PROPERTIES);
651646
}
652647
} else {
@@ -2060,7 +2055,7 @@ private boolean peekPredefinedString(String string) {
20602055

20612056
private @Nullable Token eatPredefinedString(String string) {
20622057
Token token = eatId();
2063-
if (token == null || !token.asIdentifier().value.equals(string)) {
2058+
if (token == null || !token.asIdentifier().valueEquals(string)) {
20642059
reportExpectedError(token, string);
20652060
return null;
20662061
}
@@ -2069,7 +2064,7 @@ private boolean peekPredefinedString(String string) {
20692064

20702065
private boolean peekPredefinedString(int index, String string) {
20712066
return peek(index, TokenType.IDENTIFIER)
2072-
&& ((IdentifierToken) peekToken(index)).value.equals(string);
2067+
&& ((IdentifierToken) peekToken(index)).valueEquals(string);
20732068
}
20742069

20752070
private ParseTree parseObjectLiteralGetAccessor() {
@@ -2144,7 +2139,7 @@ private ParseTree parseObjectLiteralPropertyNameAssignment() {
21442139
if (colon == null) {
21452140
if (name.type != TokenType.IDENTIFIER) {
21462141
reportExpectedError(peekToken(), TokenType.COLON);
2147-
} else if (Keywords.isKeyword(name.asIdentifier().value)) {
2142+
} else if (name.asIdentifier().isKeyword()) {
21482143
reportError(name, "Cannot use keyword in short object literal");
21492144
} else if (peek(TokenType.EQUAL)) {
21502145
IdentifierExpressionTree idTree =
@@ -2367,7 +2362,7 @@ private boolean isStartOfAsyncArrowFunction(ParseTree partialExpression) {
23672362
partialExpression.asIdentifierExpression().identifierToken;
23682363
// partialExpression is `async`
23692364
// followed by `[no newline] bindingIdentifier [no newline] =>`
2370-
return identifierToken.value.equals(ASYNC)
2365+
return identifierToken.valueEquals(ASYNC)
23712366
&& !peekImplicitSemiColon(0)
23722367
&& peekId()
23732368
&& !peekImplicitSemiColon(1)
@@ -2379,7 +2374,7 @@ && peekId()
23792374
// partialExpression is `async [no newline] (parameters)`
23802375
// followed by `[no newline] =>`
23812376
return callee.type == ParseTreeType.IDENTIFIER_EXPRESSION
2382-
&& callee.asIdentifierExpression().identifierToken.value.equals(ASYNC)
2377+
&& callee.asIdentifierExpression().identifierToken.valueEquals(ASYNC)
23832378
&& callee.location.end.line == arguments.location.start.line
23842379
&& !peekImplicitSemiColon()
23852380
&& peek(TokenType.ARROW);
@@ -3380,11 +3375,10 @@ private ParseTree parseObjectPatternField(PatternKind kind) {
33803375

33813376
Token name;
33823377
if (peekIdOrKeyword()) {
3383-
name = eatIdOrKeywordAsId();
3378+
IdentifierToken idToken = eatIdOrKeywordAsId();
33843379
if (!peek(TokenType.COLON)) {
3385-
IdentifierToken idToken = (IdentifierToken) name;
3386-
if (Keywords.isKeyword(idToken.value)) {
3387-
reportError("cannot use keyword '%s' here.", name);
3380+
if (idToken.isKeyword()) {
3381+
reportError("cannot use keyword '%s' here.", idToken);
33883382
}
33893383
if (peek(TokenType.EQUAL)) {
33903384
IdentifierExpressionTree idTree =
@@ -3393,8 +3387,9 @@ private ParseTree parseObjectPatternField(PatternKind kind) {
33933387
ParseTree defaultValue = parseAssignmentExpression();
33943388
return new DefaultParameterTree(getTreeLocation(start), idTree, defaultValue);
33953389
}
3396-
return new PropertyNameAssignmentTree(getTreeLocation(start), name, null);
3390+
return new PropertyNameAssignmentTree(getTreeLocation(start), idToken, null);
33973391
}
3392+
name = idToken;
33983393
} else {
33993394
name = parseLiteralExpression().literalToken;
34003395
}

0 commit comments

Comments
 (0)