Skip to content

Commit 758efbf

Browse files
jeapostropheerikd
authored andcommitted
Add support for bare import declarations
1 parent 00775d2 commit 758efbf

File tree

5 files changed

+7
-1
lines changed

5 files changed

+7
-1
lines changed

src/Language/JavaScript/Parser/AST.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ data JSModuleItem
7373

7474
data JSImportDeclaration
7575
= JSImportDeclaration !JSImportClause !JSFromClause !JSSemi -- ^imports, module, semi
76-
-- | JSImportDeclarationBare -- ^ module, semi
76+
| JSImportDeclarationBare !JSAnnot !String !JSSemi -- ^module, module, semi
7777
deriving (Data, Eq, Show, Typeable)
7878

7979
data JSImportClause
@@ -425,6 +425,7 @@ instance ShowStripped JSModuleItem where
425425

426426
instance ShowStripped JSImportDeclaration where
427427
ss (JSImportDeclaration imp from _) = "JSImportDeclaration (" ++ ss imp ++ "," ++ ss from ++ ")"
428+
ss (JSImportDeclarationBare _ m _) = "JSImportDeclarationBare (" ++ singleQuote m ++ ")"
428429

429430
instance ShowStripped JSImportClause where
430431
ss (JSImportClauseDefault x) = "JSImportClauseDefault (" ++ ss x ++ ")"

src/Language/JavaScript/Parser/Grammar7.y

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,8 @@ ModuleItem : Import ImportDeclaration
12511251
ImportDeclaration :: { AST.JSImportDeclaration }
12521252
ImportDeclaration : ImportClause FromClause AutoSemi
12531253
{ AST.JSImportDeclaration $1 $2 $3 }
1254+
| 'string' AutoSemi
1255+
{ AST.JSImportDeclarationBare (mkJSAnnot $1) (tokenLiteral $1) $2 }
12541256

12551257
ImportClause :: { AST.JSImportClause }
12561258
ImportClause : IdentifierName

src/Language/JavaScript/Pretty/Printer.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ instance RenderJS [JSArrayElement] where
291291

292292
instance RenderJS JSImportDeclaration where
293293
(|>) pacc (JSImportDeclaration imp from annot) = pacc |> imp |> from |> annot
294+
(|>) pacc (JSImportDeclarationBare annot m s) = pacc |> annot |> m |> s
294295

295296
instance RenderJS JSImportClause where
296297
(|>) pacc (JSImportClauseDefault x) = pacc |> x

src/Language/JavaScript/Process/Minify.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ instance MinifyJS JSImportDeclaration where
293293
JSImportClauseNamed {} -> emptyAnnot
294294
JSImportClauseDefaultNameSpace {} -> spaceAnnot
295295
JSImportClauseDefaultNamed {} -> emptyAnnot
296+
fix a (JSImportDeclarationBare _ m _) = JSImportDeclarationBare a m noSemi
296297

297298
instance MinifyJS JSImportClause where
298299
fix _ (JSImportClauseDefault n) = JSImportClauseDefault (fixSpace n)

test/Test/Language/Javascript/Minify.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ testMinifyModule = describe "Minify modules:" $ do
277277
minifyModule "import def, * as foo from \"mod\" ; " `shouldBe` "import def,* as foo from\"mod\""
278278
minifyModule "import { baz, bar as foo } from \"mod\" ; " `shouldBe` "import{baz,bar as foo}from\"mod\""
279279
minifyModule "import def, { baz, bar as foo } from \"mod\" ; " `shouldBe` "import def,{baz,bar as foo}from\"mod\""
280+
minifyModule "import \"mod\" ; " `shouldBe` "import\"mod\""
280281

281282
it "export" $ do
282283
minifyModule " export { } ; " `shouldBe` "export{}"

0 commit comments

Comments
 (0)