@@ -25,6 +25,12 @@ module Language.JavaScript.Parser.AST
25
25
26
26
-- Modules
27
27
, JSModuleItem (.. )
28
+ , JSImportDeclaration (.. )
29
+ , JSImportClause (.. )
30
+ , JSFromClause (.. )
31
+ , JSImportNameSpace (.. )
32
+ , JSImportsNamed (.. )
33
+ , JSImportSpecifier (.. )
28
34
, JSExportDeclaration (.. )
29
35
, JSExportLocalSpecifier (.. )
30
36
@@ -57,11 +63,46 @@ data JSAST
57
63
-- Shift AST
58
64
-- https://github.com/shapesecurity/shift-spec/blob/83498b92c436180cc0e2115b225a68c08f43c53e/spec.idl#L229-L234
59
65
data JSModuleItem
60
- -- = JSImportDeclaration
61
- = JSModuleExportDeclaration ! JSAnnot ! JSExportDeclaration -- ^ export,decl
66
+ = JSModuleImportDeclaration ! JSAnnot ! JSImportDeclaration -- ^ import,decl
67
+ | JSModuleExportDeclaration ! JSAnnot ! JSExportDeclaration -- ^ export,decl
62
68
| JSModuleStatementListItem ! JSStatement
63
69
deriving (Data , Eq , Show , Typeable )
64
70
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
+
65
106
data JSExportDeclaration
66
107
-- = JSExportAllFrom
67
108
-- | JSExportFrom
@@ -338,8 +379,32 @@ instance ShowStripped JSExpression where
338
379
339
380
instance ShowStripped JSModuleItem where
340
381
ss (JSModuleExportDeclaration _ x1) = " JSModuleExportDeclaration (" ++ ss x1 ++ " )"
382
+ ss (JSModuleImportDeclaration _ x1) = " JSModuleImportDeclaration (" ++ ss x1 ++ " )"
341
383
ss (JSModuleStatementListItem x1) = " JSModuleStatementListItem (" ++ ss x1 ++ " )"
342
384
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
+
343
408
instance ShowStripped JSExportDeclaration where
344
409
ss (JSExportLocals _ xs _ _) = " JSExportLocals (" ++ ss xs ++ " )"
345
410
ss (JSExport x1 _) = " JSExport (" ++ ss x1 ++ " )"
0 commit comments