Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit03487b1

Browse files
committed
React code generation works
1 parent419ce8f commit03487b1

File tree

4 files changed

+90
-18
lines changed

4 files changed

+90
-18
lines changed

‎2/generateReason.re‎

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ open Belt;
22

33
exceptionReasonGenerationError(string);
44

5-
letextractName= identifier=>
5+
letrecextractName= identifier=>
66
switch (identifier) {
77
|DotTyped.Identifier(id)=> id
88
|DotTyped.UnknownIdentifier=>"Unknown"
9+
|DotTyped.MemberAccess(id,member)=> id++"."++ extractName(member)
910
};
1011

1112
letextractTypeName= identifier=> {
@@ -41,14 +42,22 @@ let rec fromDotTyped =
4142
(extractName(name), fromDotTyped(type_), optional)
4243
),
4344
fromDotTyped(returnType),
44-
);
45+
)
46+
47+
|_=>raise(ReasonGenerationError("Unknown dottyped type"));
4548

46-
letreccompile= (~moduleName=?, moduleDefinition)=>
49+
letreccompile= (~moduleName=?,~typeTable=?,moduleDefinition)=>
4750
switch (moduleDefinition) {
4851
|DotTyped.ModuleDeclaration({name, declarations})=>
4952
letdeclarations=
50-
Array.map(declarations, compile(~moduleName=extractName(name)));
51-
Js.Array.joinWith("\n", declarations);
53+
Array.map(
54+
declarations,
55+
compile(
56+
~moduleName=extractName(name),
57+
~typeTable=TypeTable2.make(declarations),
58+
),
59+
);
60+
Js.Array.joinWith("\n", declarations)|.Reason.parseRE|.Reason.printRE;
5261

5362
|DotTyped.ReactComponent({name, type_:DotTyped.Object(propTypes)})=>
5463
Rabel.module_(
@@ -78,6 +87,16 @@ let rec compile = (~moduleName=?, moduleDefinition) =>
7887
)
7988

8089
|DotTyped.ReactComponent({name, type_:DotTyped.Named(propTypesName)})=>
90+
letmaybePropTypes=
91+
Map.String.get(Option.getExn(typeTable), extractName(propTypesName));
92+
letpropTypes=
93+
switch (maybePropTypes) {
94+
|Some(DotTyped.Object(p))=> p
95+
|_=>
96+
raise(ReasonGenerationError("React prop types must be an object"))
97+
};
98+
lethasOptional=Array.some(propTypes.properties, prop=> prop.optional);
99+
81100
Rabel.module_(
82101
extractModuleName(name),
83102
[|
@@ -92,12 +111,39 @@ let rec compile = (~moduleName=?, moduleDefinition) =>
92111
Rabel.let_(
93112
"make",
94113
Rabel.function_(
95-
Array.concat([||],[|("children",false,None)|]),
96-
extractModuleName(propTypesName)++".t",
114+
Array.concat(
115+
Array.map(propTypes.properties, prop=>
116+
(
117+
extractName(prop.name),
118+
true,
119+
prop.optional?Some("?"):None,
120+
)
121+
),
122+
[|("children",false,None)|],
123+
),
124+
Rabel.Ast.apply(
125+
"ReasonReact.wrapJsForReason",
126+
[|
127+
"~reactClass",
128+
"~props="
129+
++Rabel.Ast.apply(
130+
extractModuleName(propTypesName)++".t",
131+
Array.concat(
132+
Array.map(propTypes.properties, prop=>
133+
"~"
134+
++ extractName(prop.name)
135+
++ (prop.optional?"?":"")
136+
),
137+
hasOptional?[|"()"|]:[||],
138+
),
139+
),
140+
"children",
141+
|],
142+
),
97143
),
98144
),
99145
|],
100-
)
146+
);
101147

102148
|DotTyped.LetDeclaration({name, type_})=>
103149
Rabel.Decorators.bsModule(
@@ -113,10 +159,10 @@ let rec compile = (~moduleName=?, moduleDefinition) =>
113159
Rabel.module_(
114160
extractModuleName(name),
115161
[|
116-
Rabel.type_(
117-
"t",
118-
Rabel.Decorators.bsDeriving(
119-
"abstract",
162+
Rabel.Decorators.bsDeriving(
163+
"abstract",
164+
Rabel.type_(
165+
"t",
120166
Rabel.Types.record_(
121167
Array.map(properties, prop=>
122168
(
@@ -143,4 +189,6 @@ let rec compile = (~moduleName=?, moduleDefinition) =>
143189
extractName(name),
144190
),
145191
)
192+
193+
|_=>raise(ReasonGenerationError("Unknown dottyped declaration"))
146194
};

‎2/typeTable2.re‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
openBelt;
2+
3+
letmake= (declarations:array(DotTyped.declaration))=>
4+
Array.reduce(declarations,Map.String.empty, (typeTable, declaration)=>
5+
switch (declaration) {
6+
|DotTyped.InterfaceDeclaration({name, type_})
7+
|DotTyped.ClassDeclaration({name, type_})
8+
|DotTyped.LetDeclaration({name, type_})
9+
|DotTyped.FunctionDeclaration({name, type_})
10+
|DotTyped.ReactComponent({name, type_})=>
11+
switch (name) {
12+
|DotTyped.Identifier(id)=>Map.String.set(typeTable, id, type_)
13+
|_=> typeTable
14+
}
15+
16+
|_=> typeTable
17+
}
18+
);

‎bindings/reason.re‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
typeast;
2+
3+
[@bs.module"reason"]externalparseRE:string =>ast="";
4+
[@bs.module"reason"]externalparseREI:string =>ast="";
5+
[@bs.module"reason"]externalparseML:string =>ast="";
6+
[@bs.module"reason"]externalparseMLI:string =>ast="";
7+
[@bs.module"reason"]externalprintRE:ast =>string="";
8+
[@bs.module"reason"]externalprintREI:ast =>string="";
9+
[@bs.module"reason"]externalprintML:ast =>string="";
10+
[@bs.module"reason"]externalprintMLI:ast =>string="";

‎src/compiler.re‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
moduleStage= {
22
letparseSource= (name, source)=> {
3-
letparseFlowSource= (name, source)=> {
3+
letparseFlowSource= (_name, source)=> {
44
let (flowAst,_errors)=
55
Parser_flow.program_file(source,Some(Loc.SourceFile(name)));
66
let (_,statements,_)= flowAst;
@@ -126,11 +126,7 @@ let compile = (moduleName, moduleSource, debug) => {
126126
"",
127127
Diagnostic.diagnosticOfFlow(xs, moduleSource),
128128
)
129-
|Diagnostic.Error(xs)=> (
130-
"Unknown ID",
131-
"",
132-
xs
133-
)
129+
|Diagnostic.Error(xs)=> ("Unknown ID","", xs)
134130
};
135131
if (debug) {
136132
letdebugAsts=Stage.parseSource(moduleName, moduleSource);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp