Skip to content

Commit

Permalink
support object rest destructuring in arrow functions
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Dec 24, 2024
1 parent ff2e2cd commit 9abf963
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
4 changes: 2 additions & 2 deletions crates/dash_middle/src/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub enum Error {
expect: usize,
token: Token,
},
MultipleRestInDestructuring(Token),
MultipleRestInDestructuring(Span),
RegexSyntaxError(Token, dash_regex::Error),
IncompleteSpread(Token),
/* Compiler */
Expand Down Expand Up @@ -311,7 +311,7 @@ impl fmt::Display for FormattableError<'_, '_> {
diag.message("incorrect number of parameters for accessor");
diag.span_error(span, format!("expected {expect}, got {got}"));
}
Error::MultipleRestInDestructuring(Token { span, .. }) => {
Error::MultipleRestInDestructuring(span) => {
diag.message("multiple rest elements in destructuring");
diag.span_error(span, "second rest element defined here");
}
Expand Down
17 changes: 13 additions & 4 deletions crates/dash_parser/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@ impl Parser<'_, '_> {
ExprKind::Object(ObjectLiteral(properties)) => {
let destructured_id = parser.local_count.inc();
let mut fields = Vec::with_capacity(properties.len());
let mut rest = None;
for (key, value) in properties {
match key {
// `x: a` aliases x to 1
Expand All @@ -926,14 +927,22 @@ impl Parser<'_, '_> {
ObjectMemberKind::Default(symbol) => {
fields.push((parser.local_count.inc(), symbol, None, Some(value)))
}
ObjectMemberKind::Spread => {
if rest.is_none() {
if let Some(symbol) = value.kind.as_identifier() {
rest = Some(parser.create_binding(symbol));
} else {
parser.error(Error::unexpected_token(value.span, TokenType::DUMMY_IDENTIFIER));
}
} else {
parser.error(Error::MultipleRestInDestructuring(value.span))
}
}
_ => parser.error(Error::unexpected_token(value.span, TokenType::DUMMY_IDENTIFIER)),
}
}

Some(Parameter::Pattern(destructured_id, Pattern::Object {
fields,
rest: None,
}))
Some(Parameter::Pattern(destructured_id, Pattern::Object { fields, rest }))
}
_ => {
parser.error(Error::Unimplemented(
Expand Down
4 changes: 2 additions & 2 deletions crates/dash_parser/src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ impl Parser<'_, '_> {
if let Some(sym) = name.ty.as_identifier() {
if rest.is_some() {
// Only allow one rest operator
self.error(Error::MultipleRestInDestructuring(name));
self.error(Error::MultipleRestInDestructuring(name.span));
return None;
}

Expand Down Expand Up @@ -665,7 +665,7 @@ impl Parser<'_, '_> {
if let Some(sym) = name.ty.as_identifier() {
if rest.is_some() {
// Only allow one rest operator
self.error(Error::MultipleRestInDestructuring(name));
self.error(Error::MultipleRestInDestructuring(name.span));
return None;
}

Expand Down

0 comments on commit 9abf963

Please sign in to comment.