@@ -10,14 +10,48 @@ module Ast = {
10
10
@tag ("type" )
11
11
type expression = ObjectExpression ({properties : array <objectProperties >})
12
12
13
- type variableDeclarator = {
14
- @as ("type" ) type_ : string ,
15
- id : lval ,
16
- init ?: Null .t <expression >,
13
+ module VariableDeclarator = {
14
+ @tag ("type" )
15
+ type t = VariableDeclarator ({id : lval , init ?: Null .t <expression >})
17
16
}
17
+ module Specifier = {
18
+ @tag ("type" )
19
+ type t =
20
+ | ImportSpecifier ({local : lval })
21
+ | ImportDefaultSpecifier ({local : lval })
22
+ | ImportNamespaceSpecifier ({local : lval })
23
+ }
24
+
25
+ module StringLiteral = {
26
+ @tag ("type" )
27
+ type t = StringLiteral ({value : string })
28
+ }
29
+
30
+ module VariableDeclaration = {
31
+ @tag ("type" )
32
+ type t = VariableDeclaration ({kind : string , declarations : array <VariableDeclarator .t >})
33
+ }
34
+
35
+ module ImportDeclaration = {
36
+ @tag ("type" )
37
+ type t = ImportDeclaration ({specifiers : array <Specifier .t >, source : StringLiteral .t })
38
+ }
39
+
40
+ module Identifier = {
41
+ @tag ("type" )
42
+ type t = Identifier ({mutable name : string })
43
+ }
44
+
18
45
@tag ("type" )
19
- type node = VariableDeclaration ({kind : string , declarations : array <variableDeclarator >})
20
- type nodePath = {node : node }
46
+ type node =
47
+ | ... StringLiteral .t
48
+ | ... Specifier .t
49
+ | ... VariableDeclarator .t
50
+ | ... VariableDeclaration .t
51
+ | ... ImportDeclaration .t
52
+ | ... Identifier .t
53
+
54
+ type nodePath <'nodeType > = {node : 'nodeType }
21
55
}
22
56
23
57
module Parser = {
@@ -30,7 +64,7 @@ module Traverse = {
30
64
}
31
65
32
66
module Generator = {
33
- @send external remove : Ast .nodePath => unit = "remove"
67
+ @send external remove : Ast .nodePath < 'nodeType > => unit = "remove"
34
68
35
69
type t = {code : string }
36
70
@module ("@babel/generator" ) external generator : Ast .t => t = "default"
@@ -40,26 +74,42 @@ module PlaygroundValidator = {
40
74
type validator = {
41
75
entryPointExists : bool ,
42
76
code : string ,
77
+ imports : Dict .t <string >,
43
78
}
44
79
45
80
let validate = ast => {
46
81
let entryPoint = ref (false )
82
+ let imports = Dict .make ()
47
83
48
84
let remove = nodePath => Generator .remove (nodePath )
49
85
Traverse .traverse (
50
86
ast ,
51
87
{
52
- "ImportDeclaration" : remove ,
88
+ "ImportDeclaration" : (
89
+ {
90
+ node : ImportDeclaration ({specifiers , source : StringLiteral ({value : source })}),
91
+ } as nodePath : Ast .nodePath <Ast .ImportDeclaration .t >,
92
+ ) => {
93
+ if source -> String .startsWith ("./stdlib" ) {
94
+ switch specifiers {
95
+ | [ImportNamespaceSpecifier ({local : Identifier ({name })})] =>
96
+ imports -> Dict .set (name , source )
97
+ | _ => ()
98
+ }
99
+ }
100
+ remove (nodePath )
101
+ },
53
102
"ExportNamedDeclaration" : remove ,
54
- "VariableDeclaration" : (nodePath : Ast .nodePath ) => {
55
- switch nodePath .node {
56
- | VariableDeclaration ({declarations }) if Array .length (declarations ) > 0 =>
103
+ "VariableDeclaration" : (
104
+ {node : VariableDeclaration ({declarations })}: Ast .nodePath <Ast .VariableDeclaration .t >,
105
+ ) => {
106
+ if Array .length (declarations ) > 0 {
57
107
let firstDeclaration = Array .getUnsafe (declarations , 0 )
58
108
59
- switch ( firstDeclaration . id , firstDeclaration . init ) {
60
- | ( Identifier ({name }), Some ( init ) ) if name === "App" =>
61
- switch init -> Null . toOption {
62
- | Some (ObjectExpression ({properties })) =>
109
+ switch firstDeclaration {
110
+ | VariableDeclarator ({ id : Identifier ({name }), init } ) if name === "App" =>
111
+ switch init {
112
+ | Value (ObjectExpression ({properties })) =>
63
113
let foundEntryPoint = properties -> Array .find (property => {
64
114
switch property {
65
115
| ObjectProperty ({
@@ -74,12 +124,10 @@ module PlaygroundValidator = {
74
124
}
75
125
| _ => ()
76
126
}
77
- | _ => ()
78
127
}
79
128
},
80
129
},
81
130
)
82
-
83
- {entryPointExists : entryPoint .contents , code : Generator .generator (ast ).code }
131
+ {entryPointExists : entryPoint .contents , imports , code : Generator .generator (ast ).code }
84
132
}
85
133
}
0 commit comments