Skip to content

Commit 00775d2

Browse files
rhendricerikd
authored andcommitted
Add support for computed property names
1 parent 9419c64 commit 00775d2

File tree

7 files changed

+8
-0
lines changed

7 files changed

+8
-0
lines changed

src/Language/JavaScript/Parser/AST.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ data JSPropertyName
292292
= JSPropertyIdent !JSAnnot !String
293293
| JSPropertyString !JSAnnot !String
294294
| JSPropertyNumber !JSAnnot !String
295+
| JSPropertyComputed !JSAnnot !JSExpression !JSAnnot -- ^lb, expr, rb
295296
deriving (Data, Eq, Show, Typeable)
296297

297298
type JSObjectPropertyList = JSCommaTrailingList JSObjectProperty
@@ -478,6 +479,7 @@ instance ShowStripped JSPropertyName where
478479
ss (JSPropertyIdent _ s) = "JSIdentifier " ++ singleQuote s
479480
ss (JSPropertyString _ s) = "JSIdentifier " ++ singleQuote s
480481
ss (JSPropertyNumber _ s) = "JSIdentifier " ++ singleQuote s
482+
ss (JSPropertyComputed _ x _) = "JSPropertyComputed (" ++ ss x ++ ")"
481483

482484
instance ShowStripped JSAccessor where
483485
ss (JSAccessorGet _) = "JSAccessorGet"

src/Language/JavaScript/Parser/Grammar7.y

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ PropertyName :: { AST.JSPropertyName }
579579
PropertyName : IdentifierName { propName $1 {- 'PropertyName1' -} }
580580
| StringLiteral { propName $1 {- 'PropertyName2' -} }
581581
| NumericLiteral { propName $1 {- 'PropertyName3' -} }
582+
| LSquare AssignmentExpression RSquare { AST.JSPropertyComputed $1 $2 $3 {- 'PropertyName4' -} }
582583

583584
-- PropertySetParameterList : See 11.1.5
584585
-- Identifier

src/Language/JavaScript/Pretty/Printer.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ instance RenderJS JSPropertyName where
276276
(|>) pacc (JSPropertyIdent a s) = pacc |> a |> s
277277
(|>) pacc (JSPropertyString a s) = pacc |> a |> s
278278
(|>) pacc (JSPropertyNumber a s) = pacc |> a |> s
279+
(|>) pacc (JSPropertyComputed lb x rb) = pacc |> lb |> "[" |> x |> rb |> "]"
279280

280281
instance RenderJS JSAccessor where
281282
(|>) pacc (JSAccessorGet annot) = pacc |> annot |> "get"

src/Language/JavaScript/Process/Minify.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ instance MinifyJS JSPropertyName where
364364
fix a (JSPropertyIdent _ s) = JSPropertyIdent a s
365365
fix a (JSPropertyString _ s) = JSPropertyString a s
366366
fix a (JSPropertyNumber _ s) = JSPropertyNumber a s
367+
fix _ (JSPropertyComputed _ x _) = JSPropertyComputed emptyAnnot (fixEmpty x) emptyAnnot
367368

368369
instance MinifyJS JSAccessor where
369370
fix a (JSAccessorGet _) = JSAccessorGet a

test/Test/Language/Javascript/ExpressionParser.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ testExpressionParser = describe "Parse expressions:" $ do
5858
testExpr "x={get foo() {return 1},set foo(a) {x=a}}" `shouldBe` "Right (JSAstExpression (JSOpAssign ('=',JSIdentifier 'x',JSObjectLiteral [JSPropertyAccessor JSAccessorGet (JSIdentifier 'foo') [] (JSBlock [JSReturn JSDecimal '1' ]),JSPropertyAccessor JSAccessorSet (JSIdentifier 'foo') [JSIdentifier 'a'] (JSBlock [JSOpAssign ('=',JSIdentifier 'x',JSIdentifier 'a')])])))"
5959
testExpr "{evaluate:evaluate,load:function load(s){if(x)return s;1}}" `shouldBe` "Right (JSAstExpression (JSObjectLiteral [JSPropertyNameandValue (JSIdentifier 'evaluate') [JSIdentifier 'evaluate'],JSPropertyNameandValue (JSIdentifier 'load') [JSFunctionExpression 'load' (JSIdentifier 's') (JSBlock [JSIf (JSIdentifier 'x') (JSReturn JSIdentifier 's' JSSemicolon),JSDecimal '1']))]]))"
6060
testExpr "obj = { name : 'A', 'str' : 'B', 123 : 'C', }" `shouldBe` "Right (JSAstExpression (JSOpAssign ('=',JSIdentifier 'obj',JSObjectLiteral [JSPropertyNameandValue (JSIdentifier 'name') [JSStringLiteral 'A'],JSPropertyNameandValue (JSIdentifier ''str'') [JSStringLiteral 'B'],JSPropertyNameandValue (JSIdentifier '123') [JSStringLiteral 'C'],JSComma])))"
61+
testExpr "{[x]:1}" `shouldBe` "Right (JSAstExpression (JSObjectLiteral [JSPropertyNameandValue (JSPropertyComputed (JSIdentifier 'x')) [JSDecimal '1']]))"
6162

6263
it "unary expression" $ do
6364
testExpr "delete y" `shouldBe` "Right (JSAstExpression (JSUnaryExpression ('delete',JSIdentifier 'y')))"

test/Test/Language/Javascript/Minify.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ testMinifyExpr = describe "Minify expressions:" $ do
4242
minifyExpr " { c : 3 , d : 4 , } " `shouldBe` "{c:3,d:4}"
4343
minifyExpr " { 'str' : true , 42 : false , } " `shouldBe` "{'str':true,42:false}"
4444
minifyExpr " { x , } " `shouldBe` "{x}"
45+
minifyExpr " { [ x + y ] : 1 } " `shouldBe` "{[x+y]:1}"
4546

4647
it "parentheses" $ do
4748
minifyExpr " ( 'hello' ) " `shouldBe` "('hello')"

test/Test/Language/Javascript/RoundTrip.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ testRoundTrip = describe "Roundtrip:" $ do
4444
testRT "x=/*a*/{/*b*/x/*c*/:/*d*/1/*e*/,/*f*/y/*g*/:/*h*/2/*i*/}"
4545
testRT "x=/*a*/{/*b*/x/*c*/:/*d*/1/*e*/,/*f*/y/*g*/:/*h*/2/*i*/,/*j*/z/*k*/:/*l*/3/*m*/}"
4646
testRT "a=/*a*/{/*b*/x/*c*/:/*d*/1/*e*/,/*f*/}"
47+
testRT "/*a*/{/*b*/[/*c*/x/*d*/+/*e*/y/*f*/]/*g*/:/*h*/1/*i*/}"
4748

4849
it "miscellaneous" $ do
4950
testRT "/*a*/(/*b*/56/*c*/)"

0 commit comments

Comments
 (0)