Skip to content

Commit

Permalink
Support rest params in arrow functions
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmaloney committed Nov 8, 2019
1 parent 472b297 commit 548d418
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/lib/syntax/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,19 @@ impl Parser {
TokenData::Punctuator(Punctuator::CloseParen) => next,
TokenData::Punctuator(Punctuator::Comma) => {
// at this point it's probably gonna be an arrow function
// if first param captured all arguments, we should expect a close paren
match next.def {
ExprDef::UnaryOp(UnaryOp::Spread, _) =>
return Err(ParseError::Expected(
vec![TokenData::Punctuator(
Punctuator::CloseParen,
)],
next_tok.clone(),
"arrow function",
)),
_ => {}
}

let mut args = vec![
match next.def {
ExprDef::Local(ref name) => mk!(self, ExprDef::Local((*name).clone())),
Expand All @@ -425,6 +438,22 @@ impl Parser {
TokenData::Punctuator(Punctuator::Comma) => {
expect_ident = true;
}
TokenData::Punctuator(Punctuator::Spread) => {
let ident_token = self.get_token(self.pos + 1)?;
if let TokenData::Identifier(ref _id) = ident_token.data {
args.push(self.parse()?);
self.pos -= 1;
expect_ident = false;
} else {
return Err(ParseError::Expected(
vec![TokenData::Identifier(
"identifier".to_string(),
)],
ident_token.clone(),
"arrow function",
))
}
}
TokenData::Punctuator(Punctuator::CloseParen) => {
self.pos += 1;
break;
Expand All @@ -443,6 +472,7 @@ impl Parser {
vec![
TokenData::Punctuator(Punctuator::Comma),
TokenData::Punctuator(Punctuator::CloseParen),
TokenData::Punctuator(Punctuator::Spread),
],
curr_tk,
"arrow function",
Expand Down Expand Up @@ -753,6 +783,7 @@ impl Parser {
let mut args = Vec::with_capacity(1);
match result.def {
ExprDef::Local(ref name) => args.push(mk!(self, ExprDef::Local((*name).clone()))),
ExprDef::UnaryOp(UnaryOp::Spread, _) => args.push(result),
_ => return Err(ParseError::ExpectedExpr("identifier", result)),
}
let next = self.parse()?;
Expand Down

0 comments on commit 548d418

Please sign in to comment.