From 505df912ade3accdfeb2791370a703a8ef9525e6 Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Mon, 13 Apr 2020 22:29:54 +0200 Subject: [PATCH] Fixed assign operator precedence (#311) Co-authored-by: Iban Eguia --- boa/src/exec/tests.rs | 12 +++++++++++- boa/src/syntax/parser/mod.rs | 8 -------- boa/src/syntax/parser/tests.rs | 18 +++++++++++++++++- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/boa/src/exec/tests.rs b/boa/src/exec/tests.rs index 6a610fa564..96cd08179f 100644 --- a/boa/src/exec/tests.rs +++ b/boa/src/exec/tests.rs @@ -60,7 +60,7 @@ fn spread_with_arguments() { function foo(...a) { return arguments; } - + var result = foo(...a); "#; forward(&mut engine, scenario); @@ -248,6 +248,16 @@ fn test_short_circuit_evaluation() { assert_eq!(exec(short_circuit_eval), String::from("1")); } +#[test] +fn assign_operator_precedence() { + let src = r#" + let a = 1; + a = a + 1; + a + "#; + assert_eq!(exec(src), String::from("2")); +} + #[test] fn test_do_while_loop() { let simple_one = r#" diff --git a/boa/src/syntax/parser/mod.rs b/boa/src/syntax/parser/mod.rs index 22a3232811..99dcc23d69 100644 --- a/boa/src/syntax/parser/mod.rs +++ b/boa/src/syntax/parser/mod.rs @@ -128,14 +128,6 @@ macro_rules! expression { ( $name:ident, $lower:ident, [ $( $op:path ),* ] ) => let mut lhs = self. $lower ()?; while let Some(tok) = self.peek_skip_lineterminator() { match tok.kind { - // Parse assign expression - TokenKind::Punctuator(ref op) if op == &Punctuator::Assign => { - let _ = self.next_skip_lineterminator().expect("token disappeared"); - lhs = Node::Assign( - Box::new(lhs), - Box::new(self. $lower ()?) - ) - } TokenKind::Punctuator(ref op) if $( op == &$op )||* => { let _ = self.next_skip_lineterminator().expect("token disappeared"); lhs = Node::BinOp( diff --git a/boa/src/syntax/parser/tests.rs b/boa/src/syntax/parser/tests.rs index d247af4ca8..75c4ac1ee9 100644 --- a/boa/src/syntax/parser/tests.rs +++ b/boa/src/syntax/parser/tests.rs @@ -710,6 +710,7 @@ fn check_function_declarations() { ); } +// Should be parsed as `new Class().method()` instead of `new (Class().method())` #[test] fn check_do_while() { check_parser( @@ -742,5 +743,20 @@ fn check_construct_call_precedence() { )), vec![], )], - ) + ); +} + +#[test] +fn assing_operator_precedence() { + check_parser( + "a = a + 1", + &[Node::Assign( + Box::new(Node::Local(String::from("a"))), + Box::new(create_bin_op( + BinOp::Num(NumOp::Add), + Node::Local(String::from("a")), + Node::Const(Const::Num(1.0)), + )), + )], + ); }