Skip to content

Commit

Permalink
Fixed a small problem in the lexer for things like ?.5
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabergia committed Oct 17, 2024
1 parent d924f24 commit b1d727e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 8 additions & 4 deletions rhino/src/main/java/org/mozilla/javascript/TokenStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -1148,10 +1148,14 @@ && peekChar() == '!'
return Token.COMMA;
case '?':
if (parser.compilerEnv.getLanguageVersion() >= Context.VERSION_ES6) {
if (matchChar('.')) {
return Token.QUESTION_DOT;
}
if (matchChar('?')) {
if (peekChar() == '.') {
// ?.digit is to be treated as ? .num
getChar();
if (!isDigit(peekChar())) {
return Token.QUESTION_DOT;
}
ungetChar('.');
} else if (matchChar('?')) {
return Token.NULLISH_COALESCING;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,9 @@ public void shortCircuits() {
Utils.assertWithAllOptimizationLevelsES6(Undefined.instance, "a = undefined; a?.b.c");
Utils.assertWithAllOptimizationLevelsES6(Undefined.instance, "a = {}; a.b?.c.d.e");
}

@Test
public void optionalChainingOperatorFollowedByDigitsIsAHook() {
Utils.assertWithAllOptimizationLevelsES6(0.5, "true ?.5 : false");
}
}

0 comments on commit b1d727e

Please sign in to comment.