Skip to content

Remove keys mangling for gentype #6359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 0 additions & 91 deletions jscomp/gentype/Runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,94 +46,3 @@ let isMutableObjectField name =
let checkMutableObjectField ~previousName ~name = previousName = name ^ "#="

let default = "$$default"

module Mangle = struct
let keywords =
[|
"and";
"as";
"assert";
"begin";
"class";
"constraint";
"do";
"done";
"downto";
"else";
"end";
"exception";
"external";
"false";
"for";
"fun";
"function";
"functor";
"if";
"in";
"include";
"inherit";
"initializer";
"lazy";
"let";
"match";
"method";
"module";
"mutable";
"new";
"nonrec";
"object";
"of";
"open";
"or";
"private";
"rec";
"sig";
"struct";
"then";
"to";
"true";
"try";
"type";
"val";
"virtual";
"when";
"while";
"with";
"mod";
"land";
"lor";
"lxor";
"lsl";
"lsr";
"asr";
|]

let table = Hashtbl.create (keywords |> Array.length);;

keywords |> Array.iter (fun x -> Hashtbl.add table ("_" ^ x) x)

(**
Apply ReScript's mangling rules for object field names:
Remove trailing "__" if present.
Otherwise remove leading "_" when followed by an uppercase letter, or keyword.
*)
let translate x =
let len = x |> String.length in
if
len > 2
&& (x.[len - 1] [@doesNotRaise]) = '_'
&& (x.[len - 2] [@doesNotRaise]) = '_'
then (* "foo__" -> "foo" *) String.sub x 0 (len - 2) [@doesNotRaise]
else if len > 1 && (x.[0] [@doesNotRaise]) = '_' then
if (x.[1] [@doesNotRaise]) >= 'A' && (x.[1] [@doesNotRaise]) <= 'Z' then
(* "_Uppercase" => "Uppercase"s *)
String.sub x 1 (len - 1) [@doesNotRaise]
else
(* "_rec" -> "rec" *)
match Hashtbl.find table x with
| y -> y
| exception Not_found -> x
else x
end

let mangleObjectField = Mangle.translate
1 change: 0 additions & 1 deletion jscomp/gentype/Runtime.mli
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ val default : string
val emitModuleAccessPath : config:Config.t -> moduleAccessPath -> string

val isMutableObjectField : string -> bool
val mangleObjectField : string -> string
val newModuleItem : name:string -> moduleItem
val newRecordValue : unboxed:bool -> recordGen -> recordValue
val recordGen : unit -> recordGen
Expand Down
5 changes: 2 additions & 3 deletions jscomp/gentype/TranslateCoreType.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ let rec translateArrowType ~config ~typeVarsGen ~noFunctionReturnDependencies
~revArgs:
(( Label
(match asLabel = "" with
| true -> lbl |> Runtime.mangleObjectField
| true -> lbl
| false -> asLabel),
type1 )
:: revArgs)
Expand All @@ -92,8 +92,7 @@ let rec translateArrowType ~config ~typeVarsGen ~noFunctionReturnDependencies
coreType2
|> translateArrowType ~config ~typeVarsGen ~noFunctionReturnDependencies
~typeEnv ~revArgDeps:nextRevDeps
~revArgs:
((OptLabel (lbl |> Runtime.mangleObjectField), type1) :: revArgs))
~revArgs:((OptLabel lbl, type1) :: revArgs))
| _ ->
let {dependencies; type_ = retType} =
coreType |> translateCoreType_ ~config ~typeVarsGen ~typeEnv
Expand Down
7 changes: 2 additions & 5 deletions jscomp/gentype/TranslateTypeExprFromTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ let translateObjType closedFlag fieldsTranslations =
| Option t -> (Optional, t)
| _ -> (Mandatory, t)
in
let name = name |> Runtime.mangleObjectField in
{
mutable_;
nameJS = name;
Expand Down Expand Up @@ -298,8 +297,7 @@ let rec translateArrowType ~config ~typeVarsGen ~typeEnv ~revArgDeps ~revArgs
typeExpr2
|> translateArrowType ~config ~typeVarsGen ~typeEnv
~revArgDeps:nextRevDeps
~revArgs:
((Label (lbl |> Runtime.mangleObjectField), type1) :: revArgs)
~revArgs:((Label lbl, type1) :: revArgs)
| Some (lbl, t1) ->
let {dependencies; type_ = type1} =
t1 |> translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
Expand All @@ -308,8 +306,7 @@ let rec translateArrowType ~config ~typeVarsGen ~typeEnv ~revArgDeps ~revArgs
typeExpr2
|> translateArrowType ~config ~typeVarsGen ~typeEnv
~revArgDeps:nextRevDeps
~revArgs:
((OptLabel (lbl |> Runtime.mangleObjectField), type1) :: revArgs))
~revArgs:((OptLabel lbl, type1) :: revArgs))
| _ ->
let {dependencies; type_ = retType} =
typeExpr |> translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
Expand Down
5 changes: 2 additions & 3 deletions jscomp/gentype_tests/typescript-react-example/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
SHELL = /bin/bash

node_modules/.bin/tsc:
test:
npm install

test: node_modules/.bin/tsc
npm run build
npm run tsc

clean:
rm -rf node_modules lib src/*.bs.js src/*.gen.tsx
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions jscomp/gentype_tests/typescript-react-example/src/Hooks.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions jscomp/gentype_tests/typescript-react-example/src/Hooks.gen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {TypedArray2_Uint8Array_t as Js_TypedArray2_Uint8Array_t} from '../s
export type vehicle = { readonly name: string };

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

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

export const functionWithRenamedArgs: (_1:{
readonly to: vehicle;
readonly Type: vehicle;
readonly _to: vehicle;
readonly _Type: vehicle;
readonly cb: cb
}) => string = HooksBS.functionWithRenamedArgs;

// tslint:disable-next-line:interface-over-type-literal
export type WithRename_componentWithRenamedArgs_Props = {
readonly Type: vehicle;
readonly to: vehicle;
readonly _Type: vehicle;
readonly _to: vehicle;
readonly cb: cb
};

export const WithRename_componentWithRenamedArgs: React.ComponentType<{
readonly Type: vehicle;
readonly to: vehicle;
readonly _Type: vehicle;
readonly _to: vehicle;
readonly cb: cb
}> = HooksBS.WithRename.componentWithRenamedArgs;

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

export const WithRename: { componentWithRenamedArgs: React.ComponentType<{
readonly Type: vehicle;
readonly to: vehicle;
readonly _Type: vehicle;
readonly _to: vehicle;
readonly cb: cb
}> } = HooksBS.WithRename

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ export type decorator<a,b> = (_1:a) => b;

// tslint:disable-next-line:interface-over-type-literal
export type marshalFields = {
readonly rec: string;
readonly _rec: string;
readonly _switch: string;
readonly switch: string;
readonly __: string;
readonly _: string;
readonly foo: string;
readonly _foo: string;
readonly Uppercase: string;
readonly _Uppercase: string
readonly ___: string;
readonly foo__: string;
readonly _foo__: string;
readonly _Uppercase: string;
readonly _Uppercase__: string
};

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

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