Skip to content

Commit d3d1883

Browse files
feat(parser): implement object spread syntax support
Add JSObjectSpread constructor to JSObjectProperty type and implement grammar rules to parse spread expressions in object literals. - Add JSObjectSpread constructor with annotation and expression fields - Update ShowStripped instance for JSObjectProperty to handle spreads - Add SpreadExpression support to PropertyAssignment grammar rule - Add spreadExpressionToProperty helper function for AST conversion Enables parsing of object spread syntax like {...obj} according to ECMAScript specification for object literal spread expressions. Resolves: Object spread syntax parsing for PR #118
1 parent 1e425e5 commit d3d1883

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/Language/JavaScript/Parser/AST.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ data JSObjectProperty
308308
= JSPropertyNameandValue !JSPropertyName !JSAnnot ![JSExpression] -- ^name, colon, value
309309
| JSPropertyIdentRef !JSAnnot !String
310310
| JSObjectMethod !JSMethodDefinition
311+
| JSObjectSpread !JSAnnot !JSExpression -- ^..., expression
311312
deriving (Data, Eq, Show, Typeable)
312313

313314
data JSMethodDefinition
@@ -531,6 +532,7 @@ instance ShowStripped JSObjectProperty where
531532
ss (JSPropertyNameandValue x1 _colon x2s) = "JSPropertyNameandValue (" ++ ss x1 ++ ") " ++ ss x2s
532533
ss (JSPropertyIdentRef _ s) = "JSPropertyIdentRef " ++ singleQuote s
533534
ss (JSObjectMethod m) = ss m
535+
ss (JSObjectSpread _ expr) = "JSObjectSpread (" ++ ss expr ++ ")"
534536

535537
instance ShowStripped JSMethodDefinition where
536538
ss (JSMethodDefinition x1 _lb1 x2s _rb1 x3) = "JSMethodDefinition (" ++ ss x1 ++ ") " ++ ss x2s ++ " (" ++ ss x3 ++ ")"

src/Language/JavaScript/Parser/Grammar7.y

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ PropertyAssignment :: { AST.JSObjectProperty }
622622
PropertyAssignment : PropertyName Colon AssignmentExpression { AST.JSPropertyNameandValue $1 $2 [$3] }
623623
| IdentifierName { identifierToProperty $1 }
624624
| MethodDefinition { AST.JSObjectMethod $1 }
625+
| SpreadExpression { spreadExpressionToProperty $1 }
625626

626627
-- TODO: not clear if get/set are keywords, or just used in a specific context. Puzzling.
627628
MethodDefinition :: { AST.JSMethodDefinition }
@@ -1610,6 +1611,10 @@ identifierToProperty :: AST.JSExpression -> AST.JSObjectProperty
16101611
identifierToProperty (AST.JSIdentifier a s) = AST.JSPropertyIdentRef a s
16111612
identifierToProperty x = error $ "Cannot convert '" ++ show x ++ "' to a JSObjectProperty."
16121613

1614+
spreadExpressionToProperty :: AST.JSExpression -> AST.JSObjectProperty
1615+
spreadExpressionToProperty (AST.JSSpreadExpression a expr) = AST.JSObjectSpread a expr
1616+
spreadExpressionToProperty x = error $ "Cannot convert '" ++ show x ++ "' to a JSObjectSpread."
1617+
16131618
toArrowParameterList :: AST.JSExpression -> Token -> Alex AST.JSArrowParameterList
16141619
toArrowParameterList (AST.JSIdentifier a s) = const . return $ AST.JSUnparenthesizedArrowParameter (AST.JSIdentName a s)
16151620
toArrowParameterList (AST.JSExpressionParen lb x rb) = const . return $ AST.JSParenthesizedArrowParameterList lb (commasToCommaList x) rb

0 commit comments

Comments
 (0)