Skip to content

Commit 9a3b52a

Browse files
committed
Add spread syntax within object literals
1 parent 376b83a commit 9a3b52a

File tree

5 files changed

+10
-0
lines changed

5 files changed

+10
-0
lines changed

src/Language/JavaScript/Parser/AST.hs

+2
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ data JSObjectProperty
297297
= JSPropertyNameandValue !JSPropertyName !JSAnnot ![JSExpression] -- ^name, colon, value
298298
| JSPropertyIdentRef !JSAnnot !String
299299
| JSObjectMethod !JSMethodDefinition
300+
| JSObjectSpread !JSAnnot !JSExpression
300301
deriving (Data, Eq, Show, Typeable)
301302

302303
data JSMethodDefinition
@@ -512,6 +513,7 @@ instance ShowStripped JSObjectProperty where
512513
ss (JSPropertyNameandValue x1 _colon x2s) = "JSPropertyNameandValue (" ++ ss x1 ++ ") " ++ ss x2s
513514
ss (JSPropertyIdentRef _ s) = "JSPropertyIdentRef " ++ singleQuote s
514515
ss (JSObjectMethod m) = ss m
516+
ss (JSObjectSpread _ellipsis e) = "JSObjectSpread " ++ ss e
515517

516518
instance ShowStripped JSMethodDefinition where
517519
ss (JSMethodDefinition x1 _lb1 x2s _rb1 x3) = "JSMethodDefinition (" ++ ss x1 ++ ") " ++ ss x2s ++ " (" ++ ss x3 ++ ")"

src/Language/JavaScript/Parser/Grammar7.y

+4
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ PropertyAssignment :: { AST.JSObjectProperty }
611611
PropertyAssignment : PropertyName Colon AssignmentExpression { AST.JSPropertyNameandValue $1 $2 [$3] }
612612
| IdentifierName { identifierToProperty $1 }
613613
| MethodDefinition { AST.JSObjectMethod $1 }
614+
| SpreadExpression { spreadExpressionToProperty $1 }
614615

615616
-- TODO: not clear if get/set are keywords, or just used in a specific context. Puzzling.
616617
MethodDefinition :: { AST.JSMethodDefinition }
@@ -1564,6 +1565,9 @@ propName (AST.JSOctal a s) = AST.JSPropertyNumber a s
15641565
propName (AST.JSStringLiteral a s) = AST.JSPropertyString a s
15651566
propName x = error $ "Cannot convert '" ++ show x ++ "' to a JSPropertyName."
15661567

1568+
spreadExpressionToProperty :: AST.JSExpression -> AST.JSObjectProperty
1569+
spreadExpressionToProperty (AST.JSSpreadExpression d e) = AST.JSObjectSpread d e
1570+
15671571
identifierToProperty :: AST.JSExpression -> AST.JSObjectProperty
15681572
identifierToProperty (AST.JSIdentifier a s) = AST.JSPropertyIdentRef a s
15691573
identifierToProperty x = error $ "Cannot convert '" ++ show x ++ "' to a JSObjectProperty."

src/Language/JavaScript/Pretty/Printer.hs

+1
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ instance RenderJS JSObjectProperty where
281281
(|>) pacc (JSPropertyNameandValue n c vs) = pacc |> n |> c |> ":" |> vs
282282
(|>) pacc (JSPropertyIdentRef a s) = pacc |> a |> s
283283
(|>) pacc (JSObjectMethod m) = pacc |> m
284+
(|>) pacc (JSObjectSpread d e) = pacc |> d |> "..." |> e
284285

285286
instance RenderJS JSMethodDefinition where
286287
(|>) pacc (JSMethodDefinition n alp ps arp b) = pacc |> n |> alp |> "(" |> ps |> arp |> ")" |> b

src/Language/JavaScript/Process/Minify.hs

+1
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ instance MinifyJS JSObjectProperty where
370370
fix a (JSPropertyNameandValue n _ vs) = JSPropertyNameandValue (fix a n) emptyAnnot (map fixEmpty vs)
371371
fix a (JSPropertyIdentRef _ s) = JSPropertyIdentRef a s
372372
fix a (JSObjectMethod m) = JSObjectMethod (fix a m)
373+
fix a (JSObjectSpread _ e) = JSObjectSpread emptyAnnot (fix a e)
373374

374375
instance MinifyJS JSMethodDefinition where
375376
fix a (JSMethodDefinition n _ ps _ b) = JSMethodDefinition (fix a n) emptyAnnot (fixEmpty ps) emptyAnnot (fixEmpty b)

test/Test/Language/Javascript/Minify.hs

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ testMinifyExpr = describe "Minify expressions:" $ do
4646
minifyExpr " { a ( x, y ) { } } " `shouldBe` "{a(x,y){}}"
4747
minifyExpr " { [ x + y ] ( ) { } } " `shouldBe` "{[x+y](){}}"
4848
minifyExpr " { * a ( x, y ) { } } " `shouldBe` "{*a(x,y){}}"
49+
minifyExpr " { ...z } " `shouldBe` "{...z}"
50+
minifyExpr " { ...w, x: 3, ...y, z: 4, ...o }" `shouldBe` "{...w,x:3,...y,z:4,...o}"
4951

5052
it "parentheses" $ do
5153
minifyExpr " ( 'hello' ) " `shouldBe` "('hello')"

0 commit comments

Comments
 (0)