Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for spread expressions in object literals #118

Open
wants to merge 2 commits into
base: new-ast
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Language/JavaScript/Parser/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ data JSObjectProperty
= JSPropertyNameandValue !JSPropertyName !JSAnnot ![JSExpression] -- ^name, colon, value
| JSPropertyIdentRef !JSAnnot !String
| JSObjectMethod !JSMethodDefinition
| JSObjectSpread !JSAnnot !JSExpression
deriving (Data, Eq, Show, Typeable)

data JSMethodDefinition
Expand Down Expand Up @@ -512,6 +513,7 @@ instance ShowStripped JSObjectProperty where
ss (JSPropertyNameandValue x1 _colon x2s) = "JSPropertyNameandValue (" ++ ss x1 ++ ") " ++ ss x2s
ss (JSPropertyIdentRef _ s) = "JSPropertyIdentRef " ++ singleQuote s
ss (JSObjectMethod m) = ss m
ss (JSObjectSpread _ x1) = "JSObjectSpread (" ++ ss x1 ++ ")"

instance ShowStripped JSMethodDefinition where
ss (JSMethodDefinition x1 _lb1 x2s _rb1 x3) = "JSMethodDefinition (" ++ ss x1 ++ ") " ++ ss x2s ++ " (" ++ ss x3 ++ ")"
Expand Down
7 changes: 6 additions & 1 deletion src/Language/JavaScript/Parser/Grammar7.y
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ PropertyAssignment :: { AST.JSObjectProperty }
PropertyAssignment : PropertyName Colon AssignmentExpression { AST.JSPropertyNameandValue $1 $2 [$3] }
| IdentifierName { identifierToProperty $1 }
| MethodDefinition { AST.JSObjectMethod $1 }
| SpreadExpression { spreadExpressionToProperty $1 }

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

spreadExpressionToProperty :: AST.JSExpression -> AST.JSObjectProperty
spreadExpressionToProperty (AST.JSSpreadExpression d e) = AST.JSObjectSpread d e
spreadExpressionToProperty x = error $ "Cannot convert '" ++ show x ++ "' to a JSSpreadExpression."

identifierToProperty :: AST.JSExpression -> AST.JSObjectProperty
identifierToProperty (AST.JSIdentifier a s) = AST.JSPropertyIdentRef a s
identifierToProperty x = error $ "Cannot convert '" ++ show x ++ "' to a JSObjectProperty."
identifierToProperty x = error $ "Cannot convert '" ++ show x ++ "' to a JSPropertyIdentRef."

toArrowParameterList :: AST.JSExpression -> Token -> Alex AST.JSArrowParameterList
toArrowParameterList (AST.JSIdentifier a s) = const . return $ AST.JSUnparenthesizedArrowParameter (AST.JSIdentName a s)
Expand Down
1 change: 1 addition & 0 deletions src/Language/JavaScript/Pretty/Printer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ instance RenderJS JSObjectProperty where
(|>) pacc (JSPropertyNameandValue n c vs) = pacc |> n |> c |> ":" |> vs
(|>) pacc (JSPropertyIdentRef a s) = pacc |> a |> s
(|>) pacc (JSObjectMethod m) = pacc |> m
(|>) pacc (JSObjectSpread a e) = pacc |> a |> "..." |> e

instance RenderJS JSMethodDefinition where
(|>) pacc (JSMethodDefinition n alp ps arp b) = pacc |> n |> alp |> "(" |> ps |> arp |> ")" |> b
Expand Down
1 change: 1 addition & 0 deletions src/Language/JavaScript/Process/Minify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ instance MinifyJS JSObjectProperty where
fix a (JSPropertyNameandValue n _ vs) = JSPropertyNameandValue (fix a n) emptyAnnot (map fixEmpty vs)
fix a (JSPropertyIdentRef _ s) = JSPropertyIdentRef a s
fix a (JSObjectMethod m) = JSObjectMethod (fix a m)
fix a (JSObjectSpread _ e) = JSObjectSpread a (fixEmpty e)

instance MinifyJS JSMethodDefinition where
fix a (JSMethodDefinition n _ ps _ b) = JSMethodDefinition (fix a n) emptyAnnot (fixEmpty ps) emptyAnnot (fixEmpty b)
Expand Down
2 changes: 2 additions & 0 deletions test/Test/Language/Javascript/Minify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ testMinifyExpr = describe "Minify expressions:" $ do
minifyExpr " { a ( x, y ) { } } " `shouldBe` "{a(x,y){}}"
minifyExpr " { [ x + y ] ( ) { } } " `shouldBe` "{[x+y](){}}"
minifyExpr " { * a ( x, y ) { } } " `shouldBe` "{*a(x,y){}}"
minifyExpr " { ...z } " `shouldBe` "{...z}"
minifyExpr " { ...w, x: 3, ...y, z: 4, ...o }" `shouldBe` "{...w,x:3,...y,z:4,...o}"

it "parentheses" $ do
minifyExpr " ( 'hello' ) " `shouldBe` "('hello')"
Expand Down