Skip to content

Commit 7190b2e

Browse files
kl0tlerikd
authored andcommitted
Add support for export from declarations
1 parent b5d92b6 commit 7190b2e

File tree

7 files changed

+61
-37
lines changed

7 files changed

+61
-37
lines changed

src/Language/JavaScript/Parser/AST.hs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ module Language.JavaScript.Parser.AST
3232
, JSImportsNamed (..)
3333
, JSImportSpecifier (..)
3434
, JSExportDeclaration (..)
35-
, JSExportLocalSpecifier (..)
35+
, JSExportClause (..)
36+
, JSExportSpecifier (..)
3637

3738
, binOpEq
3839
, showStripped
@@ -105,15 +106,19 @@ data JSImportSpecifier
105106

106107
data JSExportDeclaration
107108
-- = JSExportAllFrom
108-
-- | JSExportFrom
109-
= JSExportLocals !JSAnnot !(JSCommaList JSExportLocalSpecifier) !JSAnnot !JSSemi -- ^lb, specifiers, rb, autosemi
109+
= JSExportFrom JSExportClause JSFromClause !JSSemi -- ^exports, module, semi
110+
| JSExportLocals JSExportClause !JSSemi -- ^exports, autosemi
110111
| JSExport !JSStatement !JSSemi -- ^body, autosemi
111112
-- | JSExportDefault
112113
deriving (Data, Eq, Show, Typeable)
113114

114-
data JSExportLocalSpecifier
115-
= JSExportLocalSpecifier !JSIdent -- ^ident
116-
| JSExportLocalSpecifierAs !JSIdent !JSAnnot !JSIdent -- ^ident1, as, ident2
115+
data JSExportClause
116+
= JSExportClause !JSAnnot !(JSCommaList JSExportSpecifier) !JSAnnot -- ^lb, specifiers, rb
117+
deriving (Data, Eq, Show, Typeable)
118+
119+
data JSExportSpecifier
120+
= JSExportSpecifier !JSIdent -- ^ident
121+
| JSExportSpecifierAs !JSIdent !JSAnnot !JSIdent -- ^ident1, as, ident2
117122
deriving (Data, Eq, Show, Typeable)
118123

119124
data JSStatement
@@ -416,12 +421,16 @@ instance ShowStripped JSImportSpecifier where
416421
ss (JSImportSpecifierAs x1 _ x2) = "JSImportSpecifierAs (" ++ ss x1 ++ "," ++ ss x2 ++ ")"
417422

418423
instance ShowStripped JSExportDeclaration where
419-
ss (JSExportLocals _ xs _ _) = "JSExportLocals (" ++ ss xs ++ ")"
424+
ss (JSExportFrom xs from _) = "JSExportFrom (" ++ ss xs ++ "," ++ ss from ++ ")"
425+
ss (JSExportLocals xs _) = "JSExportLocals (" ++ ss xs ++ ")"
420426
ss (JSExport x1 _) = "JSExport (" ++ ss x1 ++ ")"
421427

422-
instance ShowStripped JSExportLocalSpecifier where
423-
ss (JSExportLocalSpecifier x1) = "JSExportLocalSpecifier (" ++ ss x1 ++ ")"
424-
ss (JSExportLocalSpecifierAs x1 _ x2) = "JSExportLocalSpecifierAs (" ++ ss x1 ++ "," ++ ss x2 ++ ")"
428+
instance ShowStripped JSExportClause where
429+
ss (JSExportClause _ xs _) = "JSExportClause (" ++ ss xs ++ ")"
430+
431+
instance ShowStripped JSExportSpecifier where
432+
ss (JSExportSpecifier x1) = "JSExportSpecifier (" ++ ss x1 ++ ")"
433+
ss (JSExportSpecifierAs x1 _ x2) = "JSExportSpecifierAs (" ++ ss x1 ++ "," ++ ss x2 ++ ")"
425434

426435
instance ShowStripped JSTryCatch where
427436
ss (JSCatch _ _lb x1 _rb x3) = "JSCatch (" ++ ss x1 ++ "," ++ ss x3 ++ ")"

src/Language/JavaScript/Parser/Grammar7.y

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ ImportSpecifier : IdentifierName
12581258

12591259
-- ExportDeclaration : See 15.2.3
12601260
-- [ ] export * FromClause ;
1261-
-- [ ] export ExportClause FromClause ;
1261+
-- [x] export ExportClause FromClause ;
12621262
-- [x] export ExportClause ;
12631263
-- [x] export VariableStatement
12641264
-- [ ] export Declaration
@@ -1275,27 +1275,29 @@ ImportSpecifier : IdentifierName
12751275
-- [ ] export default ClassDeclaration[Default]
12761276
-- [ ] export default [lookahead ∉ { function, class }] AssignmentExpression[In] ;
12771277
ExportDeclaration :: { AST.JSExportDeclaration }
1278-
ExportDeclaration : ExportClause AutoSemi
1279-
{ $1 {- 'ExportDeclaration1' -} }
1278+
ExportDeclaration : ExportClause FromClause AutoSemi
1279+
{ AST.JSExportFrom $1 $2 $3 {- 'ExportDeclaration1' -} }
1280+
| ExportClause AutoSemi
1281+
{ AST.JSExportLocals $1 $2 {- 'ExportDeclaration2' -} }
12801282
| VariableStatement AutoSemi
1281-
{ AST.JSExport $1 $2 {- 'ExportDeclaration2' -} }
1283+
{ AST.JSExport $1 $2 {- 'ExportDeclaration3' -} }
12821284
| FunctionDeclaration AutoSemi
1283-
{ AST.JSExport $1 $2 {- 'ExportDeclaration2' -} }
1285+
{ AST.JSExport $1 $2 {- 'ExportDeclaration4' -} }
12841286

12851287
-- ExportClause :
12861288
-- { }
12871289
-- { ExportsList }
12881290
-- { ExportsList , }
1289-
ExportClause :: { AST.JSExportDeclaration }
1290-
ExportClause : LBrace RBrace AutoSemi
1291-
{ AST.JSExportLocals $1 AST.JSLNil $2 $3 {- 'ExportClause1' -} }
1292-
| LBrace ExportsList RBrace AutoSemi
1293-
{ AST.JSExportLocals $1 $2 $3 $4 {- 'ExportClause2' -} }
1291+
ExportClause :: { AST.JSExportClause }
1292+
ExportClause : LBrace RBrace
1293+
{ AST.JSExportClause $1 AST.JSLNil $2 {- 'ExportClause1' -} }
1294+
| LBrace ExportsList RBrace
1295+
{ AST.JSExportClause $1 $2 $3 {- 'ExportClause2' -} }
12941296

12951297
-- ExportsList :
12961298
-- ExportSpecifier
12971299
-- ExportsList , ExportSpecifier
1298-
ExportsList :: { AST.JSCommaList AST.JSExportLocalSpecifier }
1300+
ExportsList :: { AST.JSCommaList AST.JSExportSpecifier }
12991301
ExportsList : ExportSpecifier
13001302
{ AST.JSLOne $1 {- 'ExportsList1' -} }
13011303
| ExportsList Comma ExportSpecifier
@@ -1304,11 +1306,11 @@ ExportsList : ExportSpecifier
13041306
-- ExportSpecifier :
13051307
-- IdentifierName
13061308
-- IdentifierName as IdentifierName
1307-
ExportSpecifier :: { AST.JSExportLocalSpecifier }
1309+
ExportSpecifier :: { AST.JSExportSpecifier }
13081310
ExportSpecifier : IdentifierName
1309-
{ AST.JSExportLocalSpecifier (identName $1) {- 'ExportSpecifier1' -} }
1311+
{ AST.JSExportSpecifier (identName $1) {- 'ExportSpecifier1' -} }
13101312
| IdentifierName As IdentifierName
1311-
{ AST.JSExportLocalSpecifierAs (identName $1) $2 (identName $3) {- 'ExportSpecifier2' -} }
1313+
{ AST.JSExportSpecifierAs (identName $1) $2 (identName $3) {- 'ExportSpecifier2' -} }
13121314

13131315
-- For debugging/other entry points
13141316
LiteralMain :: { AST.JSAST }

src/Language/JavaScript/Pretty/Printer.hs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,16 @@ instance RenderJS JSImportSpecifier where
305305

306306
instance RenderJS JSExportDeclaration where
307307
(|>) pacc (JSExport x1 s) = pacc |> " " |> x1 |> s
308-
(|>) pacc (JSExportLocals alb JSLNil arb semi) = pacc |> alb |> "{" |> arb |> "}" |> semi
309-
(|>) pacc (JSExportLocals alb s arb semi) = pacc |> alb |> "{" |> s |> arb |> "}" |> semi
308+
(|>) pacc (JSExportLocals xs semi) = pacc |> xs |> semi
309+
(|>) pacc (JSExportFrom xs from semi) = pacc |> xs |> from |> semi
310310

311-
instance RenderJS JSExportLocalSpecifier where
312-
(|>) pacc (JSExportLocalSpecifier i) = pacc |> i
313-
(|>) pacc (JSExportLocalSpecifierAs x1 annot x2) = pacc |> x1 |> annot |> "as" |> x2
311+
instance RenderJS JSExportClause where
312+
(|>) pacc (JSExportClause alb JSLNil arb) = pacc |> alb |> "{" |> arb |> "}"
313+
(|>) pacc (JSExportClause alb s arb) = pacc |> alb |> "{" |> s |> arb |> "}"
314+
315+
instance RenderJS JSExportSpecifier where
316+
(|>) pacc (JSExportSpecifier i) = pacc |> i
317+
(|>) pacc (JSExportSpecifierAs x1 annot x2) = pacc |> x1 |> annot |> "as" |> x2
314318

315319
instance RenderJS a => RenderJS (JSCommaList a) where
316320
(|>) pacc (JSLCons pl a i) = pacc |> pl |> a |> "," |> i

src/Language/JavaScript/Process/Minify.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,16 @@ instance MinifyJS JSImportSpecifier where
308308
fix _ (JSImportSpecifierAs x1 _ x2) = JSImportSpecifierAs (fixEmpty x1) spaceAnnot (fixSpace x2)
309309

310310
instance MinifyJS JSExportDeclaration where
311-
fix _ (JSExportLocals _ x1 _ _) = JSExportLocals emptyAnnot (fixEmpty x1) emptyAnnot noSemi
311+
fix a (JSExportFrom x1 from _) = JSExportFrom (fix a x1) (fix a from) noSemi
312+
fix _ (JSExportLocals x1 _) = JSExportLocals (fix emptyAnnot x1) noSemi
312313
fix _ (JSExport x1 _) = JSExport (fixStmt emptyAnnot noSemi x1) noSemi
313314

314-
instance MinifyJS JSExportLocalSpecifier where
315-
fix _ (JSExportLocalSpecifier x1) = JSExportLocalSpecifier (fixEmpty x1)
316-
fix _ (JSExportLocalSpecifierAs x1 _ x2) = JSExportLocalSpecifierAs (fixEmpty x1) spaceAnnot (fixSpace x2)
315+
instance MinifyJS JSExportClause where
316+
fix a (JSExportClause _ x1 _) = JSExportClause emptyAnnot (fixEmpty x1) a
317+
318+
instance MinifyJS JSExportSpecifier where
319+
fix _ (JSExportSpecifier x1) = JSExportSpecifier (fixEmpty x1)
320+
fix _ (JSExportSpecifierAs x1 _ x2) = JSExportSpecifierAs (fixEmpty x1) spaceAnnot (fixSpace x2)
317321

318322
instance MinifyJS JSTryCatch where
319323
fix a (JSCatch _ _ x1 _ x3) = JSCatch a emptyAnnot (fixEmpty x1) emptyAnnot (fixEmpty x3)

test/Test/Language/Javascript/Minify.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ testMinifyModule = describe "Minify modules:" $ do
272272
minifyModule " export { a } ; " `shouldBe` "export{a}"
273273
minifyModule " export { a, b } ; " `shouldBe` "export{a,b}"
274274
minifyModule " export { a, b as c , d } ; " `shouldBe` "export{a,b as c,d}"
275+
minifyModule " export { } from \"mod\" ; " `shouldBe` "export{}from\"mod\""
275276
minifyModule " export const a = 1 ; " `shouldBe` "export const a=1"
276277

277278
-- -----------------------------------------------------------------------------

test/Test/Language/Javascript/ModuleParser.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ testModuleParser = describe "Parse modules:" $ do
4040
it "export" $ do
4141
test "export {}"
4242
`shouldBe`
43-
"Right (JSAstModule [JSModuleExportDeclaration (JSExportLocals (()))])"
43+
"Right (JSAstModule [JSModuleExportDeclaration (JSExportLocals (JSExportClause (())))])"
4444
test "export {};"
4545
`shouldBe`
46-
"Right (JSAstModule [JSModuleExportDeclaration (JSExportLocals (()))])"
46+
"Right (JSAstModule [JSModuleExportDeclaration (JSExportLocals (JSExportClause (())))])"
4747
test "export const a = 1;"
4848
`shouldBe`
4949
"Right (JSAstModule [JSModuleExportDeclaration (JSExport (JSConstant (JSVarInitExpression (JSIdentifier 'a') [JSDecimal '1'])))])"
@@ -52,10 +52,13 @@ testModuleParser = describe "Parse modules:" $ do
5252
"Right (JSAstModule [JSModuleExportDeclaration (JSExport (JSFunction 'f' () (JSBlock [])))])"
5353
test "export { a };"
5454
`shouldBe`
55-
"Right (JSAstModule [JSModuleExportDeclaration (JSExportLocals ((JSExportLocalSpecifier (JSIdentifier 'a'))))])"
55+
"Right (JSAstModule [JSModuleExportDeclaration (JSExportLocals (JSExportClause ((JSExportSpecifier (JSIdentifier 'a')))))])"
5656
test "export { a as b };"
5757
`shouldBe`
58-
"Right (JSAstModule [JSModuleExportDeclaration (JSExportLocals ((JSExportLocalSpecifierAs (JSIdentifier 'a',JSIdentifier 'b'))))])"
58+
"Right (JSAstModule [JSModuleExportDeclaration (JSExportLocals (JSExportClause ((JSExportSpecifierAs (JSIdentifier 'a',JSIdentifier 'b')))))])"
59+
test "export {} from 'mod'"
60+
`shouldBe`
61+
"Right (JSAstModule [JSModuleExportDeclaration (JSExportFrom (JSExportClause (()),JSFromClause ''mod''))])"
5962

6063

6164
test :: String -> String

test/Test/Language/Javascript/RoundTrip.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ testRoundTrip = describe "Roundtrip:" $ do
117117
testRTModule " export {} ; "
118118
testRTModule "export { a , b , c };"
119119
testRTModule "export { a, X as B, c }"
120+
testRTModule "export {} from \"mod\";"
120121

121122

122123
testRT :: String -> Expectation

0 commit comments

Comments
 (0)