Skip to content

Commit

Permalink
Allow Initializer after ArrayBindingPattern in FormalParameter (#…
Browse files Browse the repository at this point in the history
…2002)

This Pull Request changes the following:

- Allow `Initializer` after `ArrayBindingPattern` in `FormalParameter`
- Refactor `Initializer` detection in `FormalParameter` to avoid clones
  • Loading branch information
raskad committed Apr 9, 2022
1 parent 314e4ca commit 2e0254b
Showing 1 changed file with 45 additions and 32 deletions.
77 changes: 45 additions & 32 deletions boa_engine/src/syntax/parser/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,50 +399,63 @@ where
if let Some(t) = cursor.peek(0, interner)? {
let declaration = match *t.kind() {
TokenKind::Punctuator(Punctuator::OpenBlock) => {
let param = ObjectBindingPattern::new(true, self.allow_yield, self.allow_await)
.parse(cursor, interner)?;

let init = cursor
let bindings =
ObjectBindingPattern::new(true, self.allow_yield, self.allow_await)
.parse(cursor, interner)?;
let init = if *cursor
.peek(0, interner)?
.cloned()
.filter(|t| {
// Check that this is an initializer before attempting parse.
*t.kind() == TokenKind::Punctuator(Punctuator::Assign)
})
.map(|_| {
.ok_or(ParseError::AbruptEnd)?
.kind()
== TokenKind::Punctuator(Punctuator::Assign)
{
Some(
Initializer::new(None, true, self.allow_yield, self.allow_await)
.parse(cursor, interner)
})
.transpose()?;
.parse(cursor, interner)?,
)
} else {
None
};

Declaration::new_with_object_pattern(param, init)
Declaration::new_with_object_pattern(bindings, init)
}

TokenKind::Punctuator(Punctuator::OpenBracket) => {
Declaration::new_with_array_pattern(
let bindings =
ArrayBindingPattern::new(true, self.allow_yield, self.allow_await)
.parse(cursor, interner)?,
None,
)
}
.parse(cursor, interner)?;
let init = if *cursor
.peek(0, interner)?
.ok_or(ParseError::AbruptEnd)?
.kind()
== TokenKind::Punctuator(Punctuator::Assign)
{
Some(
Initializer::new(None, true, self.allow_yield, self.allow_await)
.parse(cursor, interner)?,
)
} else {
None
};

Declaration::new_with_array_pattern(bindings, init)
}
_ => {
let params = BindingIdentifier::new(self.allow_yield, self.allow_await)
let ident = BindingIdentifier::new(self.allow_yield, self.allow_await)
.parse(cursor, interner)?;
let init = cursor
let init = if *cursor
.peek(0, interner)?
.cloned()
.filter(|t| {
// Check that this is an initializer before attempting parse.
*t.kind() == TokenKind::Punctuator(Punctuator::Assign)
})
.map(|_| {
.ok_or(ParseError::AbruptEnd)?
.kind()
== TokenKind::Punctuator(Punctuator::Assign)
{
Some(
Initializer::new(None, true, self.allow_yield, self.allow_await)
.parse(cursor, interner)
})
.transpose()?;
.parse(cursor, interner)?,
)
} else {
None
};

Declaration::new_with_identifier(params, init)
Declaration::new_with_identifier(ident, init)
}
};
Ok(Self::Output::new(declaration, false))
Expand Down

0 comments on commit 2e0254b

Please sign in to comment.