From 3c57269a3b7228d738acc7bc5ac6b6725ddb7e4a Mon Sep 17 00:00:00 2001 From: Dan Burton Date: Wed, 16 Sep 2020 16:59:49 -0400 Subject: [PATCH] Support export *, see erikd/language-javascript#124 --- src/Language/JavaScript/Parser/AST.hs | 5 +++-- src/Language/JavaScript/Parser/Grammar7.y | 6 ++++-- src/Language/JavaScript/Pretty/Printer.hs | 1 + src/Language/JavaScript/Process/Minify.hs | 1 + test/Test/Language/Javascript/Minify.hs | 1 + 5 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Language/JavaScript/Parser/AST.hs b/src/Language/JavaScript/Parser/AST.hs index 3230272..6a150d0 100644 --- a/src/Language/JavaScript/Parser/AST.hs +++ b/src/Language/JavaScript/Parser/AST.hs @@ -110,8 +110,8 @@ data JSImportSpecifier deriving (Data, Eq, Show, Typeable) data JSExportDeclaration - -- = JSExportAllFrom - = JSExportFrom JSExportClause JSFromClause !JSSemi -- ^exports, module, semi + = JSExportAllFrom !JSBinOp !JSFromClause !JSSemi -- ^*, module, semi + | JSExportFrom !JSExportClause JSFromClause !JSSemi -- ^exports, module, semi | JSExportLocals JSExportClause !JSSemi -- ^exports, autosemi | JSExport !JSStatement !JSSemi -- ^body, autosemi -- | JSExportDefault @@ -485,6 +485,7 @@ instance ShowStripped JSImportSpecifier where ss (JSImportSpecifierAs x1 _ x2) = "JSImportSpecifierAs (" ++ ss x1 ++ "," ++ ss x2 ++ ")" instance ShowStripped JSExportDeclaration where + ss (JSExportAllFrom _ from _) = "JSExportAllFrom (" ++ ss from ++ ")" ss (JSExportFrom xs from _) = "JSExportFrom (" ++ ss xs ++ "," ++ ss from ++ ")" ss (JSExportLocals xs _) = "JSExportLocals (" ++ ss xs ++ ")" ss (JSExport x1 _) = "JSExport (" ++ ss x1 ++ ")" diff --git a/src/Language/JavaScript/Parser/Grammar7.y b/src/Language/JavaScript/Parser/Grammar7.y index 13a1526..5b722ee 100644 --- a/src/Language/JavaScript/Parser/Grammar7.y +++ b/src/Language/JavaScript/Parser/Grammar7.y @@ -1434,7 +1434,7 @@ ImportSpecifier : IdentifierName { AST.JSImportSpecifierAs (identName $1) $2 (identName $3) } -- ExportDeclaration : See 15.2.3 --- [ ] export * FromClause ; +-- [x] export * FromClause ; -- [x] export ExportClause FromClause ; -- [x] export ExportClause ; -- [x] export VariableStatement @@ -1452,7 +1452,9 @@ ImportSpecifier : IdentifierName -- [ ] export default ClassDeclaration[Default] -- [ ] export default [lookahead ∉ { function, class }] AssignmentExpression[In] ; ExportDeclaration :: { AST.JSExportDeclaration } -ExportDeclaration : ExportClause FromClause AutoSemi +ExportDeclaration : Mul FromClause AutoSemi + { AST.JSExportAllFrom $1 $2 $3 {- 'ExportDeclaration0' -} } + | ExportClause FromClause AutoSemi { AST.JSExportFrom $1 $2 $3 {- 'ExportDeclaration1' -} } | ExportClause AutoSemi { AST.JSExportLocals $1 $2 {- 'ExportDeclaration2' -} } diff --git a/src/Language/JavaScript/Pretty/Printer.hs b/src/Language/JavaScript/Pretty/Printer.hs index 6ef75b0..384868e 100644 --- a/src/Language/JavaScript/Pretty/Printer.hs +++ b/src/Language/JavaScript/Pretty/Printer.hs @@ -332,6 +332,7 @@ instance RenderJS JSExportDeclaration where (|>) pacc (JSExport x1 s) = pacc |> x1 |> s (|>) pacc (JSExportLocals xs semi) = pacc |> xs |> semi (|>) pacc (JSExportFrom xs from semi) = pacc |> xs |> from |> semi + (|>) pacc (JSExportAllFrom star from semi) = pacc |> star |> from |> semi instance RenderJS JSExportClause where (|>) pacc (JSExportClause alb JSLNil arb) = pacc |> alb |> "{" |> arb |> "}" diff --git a/src/Language/JavaScript/Process/Minify.hs b/src/Language/JavaScript/Process/Minify.hs index b6121ea..fa94eaa 100644 --- a/src/Language/JavaScript/Process/Minify.hs +++ b/src/Language/JavaScript/Process/Minify.hs @@ -326,6 +326,7 @@ instance MinifyJS JSImportSpecifier where fix _ (JSImportSpecifierAs x1 _ x2) = JSImportSpecifierAs (fixEmpty x1) spaceAnnot (fixSpace x2) instance MinifyJS JSExportDeclaration where + fix a (JSExportAllFrom _ from _) = JSExportAllFrom (JSBinOpTimes a) (fix a from) noSemi fix a (JSExportFrom x1 from _) = JSExportFrom (fix a x1) (fix a from) noSemi fix _ (JSExportLocals x1 _) = JSExportLocals (fix emptyAnnot x1) noSemi fix _ (JSExport x1 _) = JSExport (fixStmt spaceAnnot noSemi x1) noSemi diff --git a/test/Test/Language/Javascript/Minify.hs b/test/Test/Language/Javascript/Minify.hs index 233ac5d..f833353 100644 --- a/test/Test/Language/Javascript/Minify.hs +++ b/test/Test/Language/Javascript/Minify.hs @@ -331,6 +331,7 @@ testMinifyModule = describe "Minify modules:" $ do minifyModule " export const a = 1 ; " `shouldBe` "export const a=1" minifyModule " export function f () { } ; " `shouldBe` "export function f(){}" minifyModule " export function * f () { } ; " `shouldBe` "export function*f(){}" + minifyModule " export * from \"mod\" ; " `shouldBe` "export*from\"mod\"" -- ----------------------------------------------------------------------------- -- Minify test helpers.