Skip to content

Commit fd46de9

Browse files
committed
Implement printing string literals and calls (js)
1 parent f3c1bd2 commit fd46de9

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/JavaScriptSyntax.hs

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ printExpression expression =
3232
intercalate
3333
" "
3434
[printExpression a, printOperator operator, printExpression b]
35+
String' string -> show string
36+
Call name args ->
37+
s name ++ "(" ++ intercalate ", " (map printExpression args) ++ ")"
3538
_ -> error $ "not implemented " ++ show expression
3639

3740
printOperator :: OperatorExpr -> String

test/JavaScriptSyntaxSpec.hs

+20
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ javaScriptSyntaxSpecs =
5252
]
5353
expected = "function test(a, b) { return a + b }"
5454
in printModule code `shouldBe` expected
55+
it "prints a function that returns a string" $ do
56+
let code =
57+
Module
58+
[Function $ Declaration Nothing (ne "test") [] (String' "hey")]
59+
expected = "function test() { return \"hey\" }"
60+
in printModule code `shouldBe` expected
61+
it "prints a function call with arguments" $ do
62+
let code =
63+
Module
64+
[ Function $
65+
Declaration
66+
Nothing
67+
(ne "test")
68+
[ne "a", ne "b"]
69+
(Call
70+
(ne "func")
71+
[(Identifier (ne "a")), (Identifier (ne "b"))])
72+
]
73+
expected = "function test(a, b) { return func(a, b) }"
74+
in printModule code `shouldBe` expected
5575

5676
ne :: String -> Ident
5777
ne = Ident . NonEmptyString . NE.fromList

0 commit comments

Comments
 (0)