Browse Source

Fixed assign operator precedence (#311)

Co-authored-by: Iban Eguia <razican@protonmail.ch>
pull/318/head
HalidOdat 4 years ago committed by GitHub
parent
commit
505df912ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      boa/src/exec/tests.rs
  2. 8
      boa/src/syntax/parser/mod.rs
  3. 18
      boa/src/syntax/parser/tests.rs

12
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#"

8
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(

18
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)),
)),
)],
);
}

Loading…
Cancel
Save