@@ -25,6 +25,12 @@ module Language.JavaScript.Parser.AST
2525
2626 -- Modules
2727 , JSModuleItem (.. )
28+ , JSImportDeclaration (.. )
29+ , JSImportClause (.. )
30+ , JSFromClause (.. )
31+ , JSImportNameSpace (.. )
32+ , JSImportsNamed (.. )
33+ , JSImportSpecifier (.. )
2834 , JSExportDeclaration (.. )
2935 , JSExportLocalSpecifier (.. )
3036
@@ -57,11 +63,46 @@ data JSAST
5763-- Shift AST
5864-- https://github.com/shapesecurity/shift-spec/blob/83498b92c436180cc0e2115b225a68c08f43c53e/spec.idl#L229-L234
5965data JSModuleItem
60- -- = JSImportDeclaration
61- = JSModuleExportDeclaration ! JSAnnot ! JSExportDeclaration -- ^ export,decl
66+ = JSModuleImportDeclaration ! JSAnnot ! JSImportDeclaration -- ^ import,decl
67+ | JSModuleExportDeclaration ! JSAnnot ! JSExportDeclaration -- ^ export,decl
6268 | JSModuleStatementListItem ! JSStatement
6369 deriving (Data , Eq , Show , Typeable )
6470
71+ data JSImportDeclaration
72+ = JSImportDeclaration ! JSImportClause ! JSFromClause ! JSSemi -- ^ imports, module, semi
73+ -- | JSImportDeclarationBare -- ^ module, semi
74+ deriving (Data , Eq , Show , Typeable )
75+
76+ data JSImportClause
77+ = JSImportClauseDefault ! JSIdent -- ^ default
78+ | JSImportClauseNameSpace ! JSImportNameSpace -- ^ namespace
79+ | JSImportClauseNamed ! JSImportsNamed -- ^ named imports
80+ | JSImportClauseDefaultNameSpace ! JSIdent ! JSAnnot ! JSImportNameSpace -- ^ default, comma, namespace
81+ | JSImportClauseDefaultNamed ! JSIdent ! JSAnnot ! JSImportsNamed -- ^ default, comma, named imports
82+ deriving (Data , Eq , Show , Typeable )
83+
84+ data JSFromClause
85+ = JSFromClause ! JSAnnot ! JSAnnot ! String -- ^ from, string literal, string literal contents
86+ deriving (Data , Eq , Show , Typeable )
87+
88+ -- | Import namespace, e.g. '* as whatever'
89+ data JSImportNameSpace
90+ = JSImportNameSpace ! JSBinOp ! JSBinOp ! JSIdent -- ^ *, as, ident
91+ deriving (Data , Eq , Show , Typeable )
92+
93+ -- | Named imports, e.g. '{ foo, bar, baz as quux }'
94+ data JSImportsNamed
95+ = JSImportsNamed ! JSAnnot ! (JSCommaList JSImportSpecifier ) ! JSAnnot -- ^ lb, specifiers, rb
96+ deriving (Data , Eq , Show , Typeable )
97+
98+ -- |
99+ -- Note that this data type is separate from ExportSpecifier because the
100+ -- grammar is slightly different (e.g. in handling of reserved words).
101+ data JSImportSpecifier
102+ = JSImportSpecifier ! JSIdent -- ^ ident
103+ | JSImportSpecifierAs ! JSIdent ! JSBinOp ! JSIdent -- ^ ident, as, ident
104+ deriving (Data , Eq , Show , Typeable )
105+
65106data JSExportDeclaration
66107 -- = JSExportAllFrom
67108 -- | JSExportFrom
@@ -338,8 +379,32 @@ instance ShowStripped JSExpression where
338379
339380instance ShowStripped JSModuleItem where
340381 ss (JSModuleExportDeclaration _ x1) = " JSModuleExportDeclaration (" ++ ss x1 ++ " )"
382+ ss (JSModuleImportDeclaration _ x1) = " JSModuleImportDeclaration (" ++ ss x1 ++ " )"
341383 ss (JSModuleStatementListItem x1) = " JSModuleStatementListItem (" ++ ss x1 ++ " )"
342384
385+ instance ShowStripped JSImportDeclaration where
386+ ss (JSImportDeclaration imp from _) = " JSImportDeclaration (" ++ ss imp ++ " ," ++ ss from ++ " )"
387+
388+ instance ShowStripped JSImportClause where
389+ ss (JSImportClauseDefault x) = " JSImportClauseDefault (" ++ ss x ++ " )"
390+ ss (JSImportClauseNameSpace x) = " JSImportClauseNameSpace (" ++ ss x ++ " )"
391+ ss (JSImportClauseNamed x) = " JSImportClauseNameSpace (" ++ ss x ++ " )"
392+ ss (JSImportClauseDefaultNameSpace x1 _ x2) = " JSImportClauseDefaultNameSpace (" ++ ss x1 ++ " ," ++ ss x2 ++ " )"
393+ ss (JSImportClauseDefaultNamed x1 _ x2) = " JSImportClauseDefaultNamed (" ++ ss x1 ++ " ," ++ ss x2 ++ " )"
394+
395+ instance ShowStripped JSFromClause where
396+ ss (JSFromClause _ _ m) = " JSFromClause " ++ singleQuote m
397+
398+ instance ShowStripped JSImportNameSpace where
399+ ss (JSImportNameSpace _ _ x) = " JSImportNameSpace (" ++ ss x ++ " )"
400+
401+ instance ShowStripped JSImportsNamed where
402+ ss (JSImportsNamed _ xs _) = " JSImportsNamed (" ++ ss xs ++ " )"
403+
404+ instance ShowStripped JSImportSpecifier where
405+ ss (JSImportSpecifier x1) = " JSImportSpecifier (" ++ ss x1 ++ " )"
406+ ss (JSImportSpecifierAs x1 _ x2) = " JSImportSpecifierAs (" ++ ss x1 ++ " ," ++ ss x2 ++ " )"
407+
343408instance ShowStripped JSExportDeclaration where
344409 ss (JSExportLocals _ xs _ _) = " JSExportLocals (" ++ ss xs ++ " )"
345410 ss (JSExport x1 _) = " JSExport (" ++ ss x1 ++ " )"
0 commit comments