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

Commitca042c4

Browse files
committed
Support inline objects in React component definitions
1 parent032ecc3 commitca042c4

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

‎2/cli.re‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module Args = {
99
input:string,
1010
[@bs.optional]
1111
output:string,
12+
[@bs.optional]
13+
debug:bool,
1214
args:array(string),
1315
};
1416
externaldecode:Js.Json.t =>t="%identity";
@@ -61,6 +63,7 @@ let program =
6163
|.Commander.usage("[options] <file ...>")
6264
|.Commander.option("--input [type]","Input file type")
6365
|.Commander.option("--output [type]","Output file type")
66+
|.Commander.option("--debug","Run in debug mode")
6467
|.Commander.parse(Commander.Process.argv)
6568
|.Args.decode;
6669

@@ -80,17 +83,20 @@ let outputType =
8083
|.Option.map(optionToFileType)
8184
|.Option.getWithDefault(Compiler2.Typed);
8285

86+
letdebug= program|.Args.debug|.Option.getWithDefault(false);
87+
8388
letinputSource=Node.Fs.readFileAsUtf8Sync(filename);
8489

8590
letinputFile=
8691
Compiler2.{type_: inputType, name: filename, source: inputSource};
8792

8893
letoutputs=Compiler2.compile(inputFile, outputType);
8994

90-
Array.forEach(
91-
outputs,
92-
output=> {
95+
Array.forEach(outputs, output=>
96+
if (debug) {
97+
Js.log(output.source);
98+
}else {
9399
letoutputFilename= guessOutuptFilename(output);
94100
Node.Fs.writeFileAsUtf8Sync(outputFilename, output.source);
95-
},
101+
}
96102
);

‎2/generateReason.re‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,26 @@ let rec compile = (~moduleName=?, ~typeTable=?, moduleDefinition) =>
6060
Js.Array.joinWith("\n", declarations)|.Reason.parseRE|.Reason.printRE;
6161

6262
|DotTyped.ReactComponent({name, type_:DotTyped.Object(propTypes)})=>
63+
lethasOptional=Array.some(propTypes.properties, prop=> prop.optional);
64+
6365
Rabel.module_(
6466
extractModuleName(name),
6567
[|
68+
Rabel.Decorators.bsDeriving(
69+
"abstract",
70+
Rabel.type_(
71+
"jsProps",
72+
Rabel.Types.record_(
73+
Array.map(propTypes.properties, prop=>
74+
(
75+
extractName(prop.name),
76+
fromDotTyped(prop.type_),
77+
prop.optional,
78+
)
79+
),
80+
),
81+
),
82+
),
6683
Rabel.Decorators.bsModule(
6784
~module_=Option.getExn(moduleName),
6885
Rabel.external_(
@@ -76,15 +93,37 @@ let rec compile = (~moduleName=?, ~typeTable=?, moduleDefinition) =>
7693
Rabel.function_(
7794
Array.concat(
7895
Array.map(propTypes.properties, prop=>
79-
(extractName(prop.name),true,Some("?"))
96+
(
97+
extractName(prop.name),
98+
true,
99+
prop.optional?Some("?"):None,
100+
)
80101
),
81102
[|("children",false,None)|],
82103
),
83-
"10",
104+
Rabel.Ast.apply(
105+
"ReasonReact.wrapJsForReason",
106+
[|
107+
"~reactClass",
108+
"~props="
109+
++Rabel.Ast.apply(
110+
"jsProps",
111+
Array.concat(
112+
Array.map(propTypes.properties, prop=>
113+
"~"
114+
++ extractName(prop.name)
115+
++ (prop.optional?"?":"")
116+
),
117+
hasOptional?[|"()"|]:[||],
118+
),
119+
),
120+
"children",
121+
|],
122+
),
84123
),
85124
),
86125
|],
87-
)
126+
);
88127

89128
|DotTyped.ReactComponent({name, type_:DotTyped.Named(propTypesName)})=>
90129
letmaybePropTypes=

‎2/parseFlow.re‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ let rec flowTypeToTyped = (flowType: FlowAst.Type.t) => {
5454
types|.Belt.List.toArray|.Belt.Array.map(flowTypeToTyped),
5555
)
5656
|FlowAst.Type.Function(f)=> functionTypeToTyped(f)
57+
|FlowAst.Type.Object(o)=> objectTypeToTyped(o)
5758
|FlowAst.Type.Generic(t)=>
5859
letrecidentifierFromGeneric=
5960
(generic:FlowAst.Type.Generic.Identifier.t)=>
@@ -94,17 +95,15 @@ and functionTypeToTyped = (f: FlowAst.Type.Function.t) => {
9495
);
9596
letreturnType= flowTypeToTyped(returnType);
9697
DotTyped.Function({parameters, rest, returnType, typeParameters:[||]});
97-
};
98-
99-
letobjectPropertyValueToTyped= (value:FlowAst.Type.Object.Property.value)=>
98+
}
99+
andobjectPropertyValueToTyped= (value:FlowAst.Type.Object.Property.value)=>
100100
switch (value) {
101101
|FlowAst.Type.Object.Property.Init(t)=> flowTypeToTyped(t)
102102
|FlowAst.Type.Object.Property.Get((_loc,func))
103103
|FlowAst.Type.Object.Property.Set((_loc,func))=>
104104
functionTypeToTyped(func)
105-
};
106-
107-
letobjectTypeToTyped= (obj:FlowAst.Type.Object.t)=> {
105+
}
106+
andobjectTypeToTyped= (obj:FlowAst.Type.Object.t)=> {
108107
letproperties=List.toArray(obj.properties);
109108
DotTyped.Object({
110109
properties:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp