@@ -10,14 +10,48 @@ module Ast = {
1010 @tag ("type" )
1111 type expression = ObjectExpression ({properties : array <objectProperties >})
1212
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 >})
1716 }
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+
1845 @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 }
2155}
2256
2357module Parser = {
@@ -30,7 +64,7 @@ module Traverse = {
3064}
3165
3266module Generator = {
33- @send external remove : Ast .nodePath => unit = "remove"
67+ @send external remove : Ast .nodePath < 'nodeType > => unit = "remove"
3468
3569 type t = {code : string }
3670 @module ("@babel/generator" ) external generator : Ast .t => t = "default"
@@ -40,26 +74,42 @@ module PlaygroundValidator = {
4074 type validator = {
4175 entryPointExists : bool ,
4276 code : string ,
77+ imports : Dict .t <string >,
4378 }
4479
4580 let validate = ast => {
4681 let entryPoint = ref (false )
82+ let imports = Dict .make ()
4783
4884 let remove = nodePath => Generator .remove (nodePath )
4985 Traverse .traverse (
5086 ast ,
5187 {
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+ },
53102 "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 {
57107 let firstDeclaration = Array .getUnsafe (declarations , 0 )
58108
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 })) =>
63113 let foundEntryPoint = properties -> Array .find (property => {
64114 switch property {
65115 | ObjectProperty ({
@@ -74,12 +124,10 @@ module PlaygroundValidator = {
74124 }
75125 | _ => ()
76126 }
77- | _ => ()
78127 }
79128 },
80129 },
81130 )
82-
83- {entryPointExists : entryPoint .contents , code : Generator .generator (ast ).code }
131+ {entryPointExists : entryPoint .contents , imports , code : Generator .generator (ast ).code }
84132 }
85133}
0 commit comments