Skip to content

Commit 835a961

Browse files
authored
Remove keys mangling for gentype (#6359)
* Remove keys mangling for gentype * Run tsc for gentype tests
1 parent 260d770 commit 835a961

File tree

10 files changed

+32
-129
lines changed

10 files changed

+32
-129
lines changed

jscomp/gentype/Runtime.ml

-91
Original file line numberDiff line numberDiff line change
@@ -46,94 +46,3 @@ let isMutableObjectField name =
4646
let checkMutableObjectField ~previousName ~name = previousName = name ^ "#="
4747

4848
let default = "$$default"
49-
50-
module Mangle = struct
51-
let keywords =
52-
[|
53-
"and";
54-
"as";
55-
"assert";
56-
"begin";
57-
"class";
58-
"constraint";
59-
"do";
60-
"done";
61-
"downto";
62-
"else";
63-
"end";
64-
"exception";
65-
"external";
66-
"false";
67-
"for";
68-
"fun";
69-
"function";
70-
"functor";
71-
"if";
72-
"in";
73-
"include";
74-
"inherit";
75-
"initializer";
76-
"lazy";
77-
"let";
78-
"match";
79-
"method";
80-
"module";
81-
"mutable";
82-
"new";
83-
"nonrec";
84-
"object";
85-
"of";
86-
"open";
87-
"or";
88-
"private";
89-
"rec";
90-
"sig";
91-
"struct";
92-
"then";
93-
"to";
94-
"true";
95-
"try";
96-
"type";
97-
"val";
98-
"virtual";
99-
"when";
100-
"while";
101-
"with";
102-
"mod";
103-
"land";
104-
"lor";
105-
"lxor";
106-
"lsl";
107-
"lsr";
108-
"asr";
109-
|]
110-
111-
let table = Hashtbl.create (keywords |> Array.length);;
112-
113-
keywords |> Array.iter (fun x -> Hashtbl.add table ("_" ^ x) x)
114-
115-
(**
116-
Apply ReScript's mangling rules for object field names:
117-
Remove trailing "__" if present.
118-
Otherwise remove leading "_" when followed by an uppercase letter, or keyword.
119-
*)
120-
let translate x =
121-
let len = x |> String.length in
122-
if
123-
len > 2
124-
&& (x.[len - 1] [@doesNotRaise]) = '_'
125-
&& (x.[len - 2] [@doesNotRaise]) = '_'
126-
then (* "foo__" -> "foo" *) String.sub x 0 (len - 2) [@doesNotRaise]
127-
else if len > 1 && (x.[0] [@doesNotRaise]) = '_' then
128-
if (x.[1] [@doesNotRaise]) >= 'A' && (x.[1] [@doesNotRaise]) <= 'Z' then
129-
(* "_Uppercase" => "Uppercase"s *)
130-
String.sub x 1 (len - 1) [@doesNotRaise]
131-
else
132-
(* "_rec" -> "rec" *)
133-
match Hashtbl.find table x with
134-
| y -> y
135-
| exception Not_found -> x
136-
else x
137-
end
138-
139-
let mangleObjectField = Mangle.translate

jscomp/gentype/Runtime.mli

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ val default : string
1010
val emitModuleAccessPath : config:Config.t -> moduleAccessPath -> string
1111

1212
val isMutableObjectField : string -> bool
13-
val mangleObjectField : string -> string
1413
val newModuleItem : name:string -> moduleItem
1514
val newRecordValue : unboxed:bool -> recordGen -> recordValue
1615
val recordGen : unit -> recordGen

jscomp/gentype/TranslateCoreType.ml

+2-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ let rec translateArrowType ~config ~typeVarsGen ~noFunctionReturnDependencies
8080
~revArgs:
8181
(( Label
8282
(match asLabel = "" with
83-
| true -> lbl |> Runtime.mangleObjectField
83+
| true -> lbl
8484
| false -> asLabel),
8585
type1 )
8686
:: revArgs)
@@ -92,8 +92,7 @@ let rec translateArrowType ~config ~typeVarsGen ~noFunctionReturnDependencies
9292
coreType2
9393
|> translateArrowType ~config ~typeVarsGen ~noFunctionReturnDependencies
9494
~typeEnv ~revArgDeps:nextRevDeps
95-
~revArgs:
96-
((OptLabel (lbl |> Runtime.mangleObjectField), type1) :: revArgs))
95+
~revArgs:((OptLabel lbl, type1) :: revArgs))
9796
| _ ->
9897
let {dependencies; type_ = retType} =
9998
coreType |> translateCoreType_ ~config ~typeVarsGen ~typeEnv

jscomp/gentype/TranslateTypeExprFromTypes.ml

+2-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ let translateObjType closedFlag fieldsTranslations =
4444
| Option t -> (Optional, t)
4545
| _ -> (Mandatory, t)
4646
in
47-
let name = name |> Runtime.mangleObjectField in
4847
{
4948
mutable_;
5049
nameJS = name;
@@ -298,8 +297,7 @@ let rec translateArrowType ~config ~typeVarsGen ~typeEnv ~revArgDeps ~revArgs
298297
typeExpr2
299298
|> translateArrowType ~config ~typeVarsGen ~typeEnv
300299
~revArgDeps:nextRevDeps
301-
~revArgs:
302-
((Label (lbl |> Runtime.mangleObjectField), type1) :: revArgs)
300+
~revArgs:((Label lbl, type1) :: revArgs)
303301
| Some (lbl, t1) ->
304302
let {dependencies; type_ = type1} =
305303
t1 |> translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
@@ -308,8 +306,7 @@ let rec translateArrowType ~config ~typeVarsGen ~typeEnv ~revArgDeps ~revArgs
308306
typeExpr2
309307
|> translateArrowType ~config ~typeVarsGen ~typeEnv
310308
~revArgDeps:nextRevDeps
311-
~revArgs:
312-
((OptLabel (lbl |> Runtime.mangleObjectField), type1) :: revArgs))
309+
~revArgs:((OptLabel lbl, type1) :: revArgs))
313310
| _ ->
314311
let {dependencies; type_ = retType} =
315312
typeExpr |> translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv

jscomp/gentype_tests/typescript-react-example/Makefile

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
SHELL = /bin/bash
22

3-
node_modules/.bin/tsc:
3+
test:
44
npm install
5-
6-
test: node_modules/.bin/tsc
75
npm run build
6+
npm run tsc
87

98
clean:
109
rm -rf node_modules lib src/*.bs.js src/*.gen.tsx

jscomp/gentype_tests/typescript-react-example/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/gentype_tests/typescript-react-example/src/Hooks.bs.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/gentype_tests/typescript-react-example/src/Hooks.gen.tsx

+9-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {TypedArray2_Uint8Array_t as Js_TypedArray2_Uint8Array_t} from '../s
1414
export type vehicle = { readonly name: string };
1515

1616
// tslint:disable-next-line:interface-over-type-literal
17-
export type cb = (_1:{ readonly to: vehicle }) => void;
17+
export type cb = (_1:{ readonly _to: vehicle }) => void;
1818

1919
// tslint:disable-next-line:interface-over-type-literal
2020
export type r = { readonly x: string };
@@ -72,21 +72,21 @@ export type NoProps_make_Props = {};
7272
export const NoProps_make: React.ComponentType<{}> = HooksBS.NoProps.make;
7373

7474
export const functionWithRenamedArgs: (_1:{
75-
readonly to: vehicle;
76-
readonly Type: vehicle;
75+
readonly _to: vehicle;
76+
readonly _Type: vehicle;
7777
readonly cb: cb
7878
}) => string = HooksBS.functionWithRenamedArgs;
7979

8080
// tslint:disable-next-line:interface-over-type-literal
8181
export type WithRename_componentWithRenamedArgs_Props = {
82-
readonly Type: vehicle;
83-
readonly to: vehicle;
82+
readonly _Type: vehicle;
83+
readonly _to: vehicle;
8484
readonly cb: cb
8585
};
8686

8787
export const WithRename_componentWithRenamedArgs: React.ComponentType<{
88-
readonly Type: vehicle;
89-
readonly to: vehicle;
88+
readonly _Type: vehicle;
89+
readonly _to: vehicle;
9090
readonly cb: cb
9191
}> = HooksBS.WithRename.componentWithRenamedArgs;
9292

@@ -153,8 +153,8 @@ export const Inner: {
153153
export const RenderPropRequiresConversion: { make: React.ComponentType<{ readonly renderVehicle: React.ComponentType<{ readonly number: number; readonly vehicle: vehicle }> }> } = HooksBS.RenderPropRequiresConversion
154154

155155
export const WithRename: { componentWithRenamedArgs: React.ComponentType<{
156-
readonly Type: vehicle;
157-
readonly to: vehicle;
156+
readonly _Type: vehicle;
157+
readonly _to: vehicle;
158158
readonly cb: cb
159159
}> } = HooksBS.WithRename
160160

jscomp/gentype_tests/typescript-react-example/src/nested/Types.bs.js

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/gentype_tests/typescript-react-example/src/nested/Types.gen.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,19 @@ export type decorator<a,b> = (_1:a) => b;
6767

6868
// tslint:disable-next-line:interface-over-type-literal
6969
export type marshalFields = {
70-
readonly rec: string;
70+
readonly _rec: string;
7171
readonly _switch: string;
7272
readonly switch: string;
7373
readonly __: string;
74-
readonly _: string;
75-
readonly foo: string;
76-
readonly _foo: string;
77-
readonly Uppercase: string;
78-
readonly _Uppercase: string
74+
readonly ___: string;
75+
readonly foo__: string;
76+
readonly _foo__: string;
77+
readonly _Uppercase: string;
78+
readonly _Uppercase__: string
7979
};
8080

8181
// tslint:disable-next-line:interface-over-type-literal
82-
export type marshalMutableField = { match: number };
82+
export type marshalMutableField = { _match: number };
8383

8484
// tslint:disable-next-line:interface-over-type-literal
8585
export type ocaml_array<a> = a[];

0 commit comments

Comments
 (0)