1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 08:35:09 +00:00

LibJS: Fix conditional expression precedence

This fixes the following from parsing incorrectly due to the comma
that occurs after the conditional:

  let o = {
    foo: true ? 1 : 2,
    bar: 'baz',
  };
This commit is contained in:
Matthew Olsson 2020-05-28 14:42:20 -07:00 committed by Andreas Kling
parent 3847d00727
commit 664085b719
3 changed files with 6 additions and 2 deletions

View file

@ -1105,9 +1105,9 @@ NonnullRefPtr<ContinueStatement> Parser::parse_continue_statement()
NonnullRefPtr<ConditionalExpression> Parser::parse_conditional_expression(NonnullRefPtr<Expression> test) NonnullRefPtr<ConditionalExpression> Parser::parse_conditional_expression(NonnullRefPtr<Expression> test)
{ {
consume(TokenType::QuestionMark); consume(TokenType::QuestionMark);
auto consequent = parse_expression(0); auto consequent = parse_expression(2);
consume(TokenType::Colon); consume(TokenType::Colon);
auto alternate = parse_expression(0); auto alternate = parse_expression(2);
return create_ast_node<ConditionalExpression>(move(test), move(consequent), move(alternate)); return create_ast_node<ConditionalExpression>(move(test), move(consequent), move(alternate));
} }

View file

@ -7,6 +7,7 @@ try {
1: 23, 1: 23,
foo, foo,
bar: "baz", bar: "baz",
qux: true ? 10 : 20,
"hello": "friends", "hello": "friends",
[1 + 2]: 42, [1 + 2]: 42,
["I am a " + computed + " key"]: foo, ["I am a " + computed + " key"]: foo,
@ -17,6 +18,7 @@ try {
assert(o["1"] === 23); assert(o["1"] === 23);
assert(o.foo === "bar"); assert(o.foo === "bar");
assert(o["foo"] === "bar"); assert(o["foo"] === "bar");
assert(o.qux === 10),
assert(o.hello === "friends"); assert(o.hello === "friends");
assert(o["hello"] === "friends"); assert(o["hello"] === "friends");
assert(o[3] === 42); assert(o[3] === 42);

View file

@ -6,6 +6,8 @@ try {
assert(x === 1 ? true : false); assert(x === 1 ? true : false);
assert((x ? x : 0) === x); assert((x ? x : 0) === x);
assert(1 < 2 ? (true) : (false)); assert(1 < 2 ? (true) : (false));
assert((0 ? 1 : 1 ? 10 : 20) === 10);
assert((0 ? 1 ? 1 : 10 : 20) === 20);
var o = {}; var o = {};
o.f = true; o.f = true;