Skip to content

Commit c4bdcd4

Browse files
fix: record field names incorrectly emitted by genType
I managed to find the source of this with Claude's help, though I don't have enough experience with ocaml to know for sure if this is an idiomatic fix. resolves #8086 Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
1 parent 14721cb commit c4bdcd4

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

compiler/gentype/TranslateTypeDeclarations.ml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,20 @@ let create_case (label, attributes) ~poly =
4141
* If @genType.as is used, perform renaming conversion.
4242
* If @as is used (with records-as-objects active), escape and quote if
4343
* the identifier contains characters which are invalid as JS property names.
44+
* For escaped identifiers like \"foo-bar", strip the surrounding \"..."
45+
* since they are part of the ReScript syntax, not the actual field name.
46+
* The resulting name will be quoted later in EmitType if needed.
4447
*)
4548
let rename_record_field ~attributes ~name =
4649
attributes |> Annotation.check_unsupported_gentype_as_renaming;
4750
match attributes |> Annotation.get_as_string with
48-
| Some s -> s |> String.escaped
49-
| None -> name
51+
| Some s -> String.escaped s
52+
| None ->
53+
(* Strip surrounding \"..." from escaped identifiers like \"foo-bar" *)
54+
let len = String.length name in
55+
if len >= 3 && name.[0] = '\\' && name.[1] = '"' && name.[len - 1] = '"'
56+
then String.sub name 2 (len - 3)
57+
else name
5058

5159
let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver
5260
~type_attributes ~type_env ~type_name ~type_vars declaration_kind :
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* TypeScript file generated from EscapedNames.res by genType. */
2+
3+
/* eslint-disable */
4+
/* tslint:disable */
5+
6+
import * as EscapedNamesJS from './EscapedNames.res.js';
7+
8+
export type variant = "Illegal\"Name";
9+
10+
export type polymorphicVariant = "Illegal\"Name";
11+
12+
export type object_ = { readonly normalField: number; readonly "escape\"me": number };
13+
14+
export type record = {
15+
readonly normalField: variant;
16+
readonly "Renamed'Field": number;
17+
readonly "Illegal-field name": number
18+
};
19+
20+
export const myRecord: record = EscapedNamesJS.myRecord as any;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@genType
2+
type variant = | @as("Illegal\"Name") IllegalName
3+
4+
@genType
5+
type polymorphicVariant = [#"Illegal\"Name"]
6+
7+
@genType
8+
type object_ = {"normalField": int, "escape\"me": int}
9+
10+
@genType
11+
type record = {
12+
normalField: variant,
13+
@as("Renamed'Field") renamedField: int,
14+
\"Illegal-field name": int,
15+
}
16+
@genType
17+
let myRecord = {
18+
normalField: IllegalName,
19+
renamedField: 42,
20+
\"Illegal-field name": 7,
21+
}

tests/gentype_tests/typescript-react-example/src/EscapedNames.res.js

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)