Skip to content
Open
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
12 changes: 10 additions & 2 deletions compiler/gentype/TranslateTypeDeclarations.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,20 @@ let create_case (label, attributes) ~poly =
* If @genType.as is used, perform renaming conversion.
* If @as is used (with records-as-objects active), escape and quote if
* the identifier contains characters which are invalid as JS property names.
* For escaped identifiers like \"foo-bar", strip the surrounding \"..."
* since they are part of the ReScript syntax, not the actual field name.
* The resulting name will be quoted later in EmitType if needed.
*)
let rename_record_field ~attributes ~name =
attributes |> Annotation.check_unsupported_gentype_as_renaming;
match attributes |> Annotation.get_as_string with
| Some s -> s |> String.escaped
| None -> name
| Some s -> String.escaped s
| None ->
(* Strip surrounding \"..." from escaped identifiers like \"foo-bar" *)
let len = String.length name in
if len >= 3 && name.[0] = '\\' && name.[1] = '"' && name.[len - 1] = '"'
then String.sub name 2 (len - 3)
else name

let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver
~type_attributes ~type_env ~type_name ~type_vars declaration_kind :
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* TypeScript file generated from EscapedNames.res by genType. */

/* eslint-disable */
/* tslint:disable */

import * as EscapedNamesJS from './EscapedNames.res.js';

export type variant = "Illegal\"Name";

export type polymorphicVariant = "Illegal\"Name";

export type object_ = { readonly normalField: number; readonly "escape\"me": number };

export type record = {
readonly normalField: variant;
readonly "Renamed'Field": number;
readonly "Illegal-field name": number
};

export const myRecord: record = EscapedNamesJS.myRecord as any;
21 changes: 21 additions & 0 deletions tests/gentype_tests/typescript-react-example/src/EscapedNames.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@genType
type variant = | @as("Illegal\"Name") IllegalName

@genType
type polymorphicVariant = [#"Illegal\"Name"]

@genType
type object_ = {"normalField": int, "escape\"me": int}

@genType
type record = {
normalField: variant,
@as("Renamed'Field") renamedField: int,
\"Illegal-field name": int,
}
@genType
let myRecord = {
normalField: IllegalName,
renamedField: 42,
\"Illegal-field name": 7,
}

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

Loading