@@ -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,47 @@ 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
+ -- TODO: Check that 'from' can still be used as an identifier.
85
+ data JSFromClause
86
+ = JSFromClause ! JSAnnot ! JSAnnot ! String -- ^ from, string literal, string literal contents
87
+ deriving (Data , Eq , Show , Typeable )
88
+
89
+ -- | Import namespace, e.g. '* as whatever'
90
+ data JSImportNameSpace
91
+ = JSImportNameSpace ! JSBinOp ! JSBinOp ! JSIdent -- ^ *, as, ident
92
+ deriving (Data , Eq , Show , Typeable )
93
+
94
+ -- | Named imports, e.g. '{ foo, bar, baz as quux }'
95
+ data JSImportsNamed
96
+ = JSImportsNamed ! JSAnnot ! (JSCommaList JSImportSpecifier ) ! JSAnnot -- ^ lb, specifiers, rb
97
+ deriving (Data , Eq , Show , Typeable )
98
+
99
+ -- |
100
+ -- Note that this data type is separate from ExportSpecifier because the
101
+ -- grammar is slightly different (e.g. in handling of reserved words).
102
+ data JSImportSpecifier
103
+ = JSImportSpecifier ! JSIdent -- ^ ident
104
+ | JSImportSpecifierAs ! JSIdent ! JSBinOp ! JSIdent -- ^ ident, as, ident
105
+ deriving (Data , Eq , Show , Typeable )
106
+
65
107
data JSExportDeclaration
66
108
-- = JSExportAllFrom
67
109
-- | JSExportFrom
@@ -338,8 +380,32 @@ instance ShowStripped JSExpression where
338
380
339
381
instance ShowStripped JSModuleItem where
340
382
ss (JSModuleExportDeclaration _ x1) = " JSModuleExportDeclaration (" ++ ss x1 ++ " )"
383
+ ss (JSModuleImportDeclaration _ x1) = " JSModuleImportDeclaration (" ++ ss x1 ++ " )"
341
384
ss (JSModuleStatementListItem x1) = " JSModuleStatementListItem (" ++ ss x1 ++ " )"
342
385
386
+ instance ShowStripped JSImportDeclaration where
387
+ ss (JSImportDeclaration imp from _) = " JSImportDeclaration (" ++ ss imp ++ " ," ++ ss from ++ " )"
388
+
389
+ instance ShowStripped JSImportClause where
390
+ ss (JSImportClauseDefault x) = " JSImportClauseDefault (" ++ ss x ++ " )"
391
+ ss (JSImportClauseNameSpace x) = " JSImportClauseNameSpace (" ++ ss x ++ " )"
392
+ ss (JSImportClauseNamed x) = " JSImportClauseNameSpace (" ++ ss x ++ " )"
393
+ ss (JSImportClauseDefaultNameSpace x1 _ x2) = " JSImportClauseDefaultNameSpace (" ++ ss x1 ++ " ," ++ ss x2 ++ " )"
394
+ ss (JSImportClauseDefaultNamed x1 _ x2) = " JSImportClauseDefaultNamed (" ++ ss x1 ++ " ," ++ ss x2 ++ " )"
395
+
396
+ instance ShowStripped JSFromClause where
397
+ ss (JSFromClause _ _ m) = " JSFromClause " ++ singleQuote m
398
+
399
+ instance ShowStripped JSImportNameSpace where
400
+ ss (JSImportNameSpace _ _ x) = " JSImportNameSpace (" ++ ss x ++ " )"
401
+
402
+ instance ShowStripped JSImportsNamed where
403
+ ss (JSImportsNamed _ xs _) = " JSImportsNamed (" ++ ss xs ++ " )"
404
+
405
+ instance ShowStripped JSImportSpecifier where
406
+ ss (JSImportSpecifier x1) = " JSImportSpecifier (" ++ ss x1 ++ " )"
407
+ ss (JSImportSpecifierAs x1 _ x2) = " JSImportSpecifierAs (" ++ ss x1 ++ " ," ++ ss x2 ++ " )"
408
+
343
409
instance ShowStripped JSExportDeclaration where
344
410
ss (JSExportLocals _ xs _ _) = " JSExportLocals (" ++ ss xs ++ " )"
345
411
ss (JSExport x1 _) = " JSExport (" ++ ss x1 ++ " )"
0 commit comments