rescript-association / genType

Auto generation of idiomatic bindings between Reason and JavaScript: either vanilla or typed with TypeScript/FlowType.
https://rescript-lang.org/docs/gentype/latest/introduction
MIT License
757 stars 44 forks source link

Js.t object keys with invalid characters do not get generated with quotes. #603

Closed chrischen closed 2 years ago

chrischen commented 2 years ago

I have an object with an "escaped" string key:

type t = { ... \"fp-z": option<string> };

module Params = {
  @genType
  type t<'a> = {..
    "auto": option<string>,
    "fit": option<string>,
    "crop": option<string>,
    "fp-z": option<string>,
  } as 'a
  @obj
  external make: (
    ~auto: string=?,
    ~fit: string=?,
    ~crop: string=?,
    ~\"fp-z": string=?,
    unit,
  ) => t<'a> = ""
}

Its leading to typescript code where the key isn't enclosed in quotes, causing a syntax error.

export type Props = { readonly items: Model_GalleryItem_t<{
  readonly auto?: string; 
  readonly crop?: string; 
  readonly fit?: string; 
  readonly fp-z?: string
}>[] };

Should be generated as

export type Props = { readonly items: Model_GalleryItem_t<{
  readonly auto?: string; 
  readonly crop?: string; 
  readonly fit?: string; 
  readonly "fp-z"?: string
}>[] };

EDIT: I did some further testing and it doesn't seem to matter if it's from an @obj make function. Any key with invalid characters like "-" doesn't get wrapped in quotes.

chrischen commented 2 years ago

For some reason Github is not letting me edit, so I've isolated the problem:

Objects with keys that are invalid in JS are not enclosed in quotes, whereas a record and an "@as" annotation is generated correctly.

@genType
type someType<'a> = {..
  "auto": option<string>,
  "fit": option<string>,
  "crop": option<string>,
  "fp-z": option<string>,
} as 'a

@genType
type someType2 = {
  auto: option<string>,
  fit: option<string>,
  crop: option<string>,
  @as("fp-z")
  fpz: option<string>,
}

Its leading to typescript code where the key isn't enclosed in quotes, causing a syntax error.

export type someType<a> = {
  readonly auto?: string; 
  readonly fit?: string; 
  readonly crop?: string; 
  readonly fp-z?: string
};

// tslint:disable-next-line:interface-over-type-literal
export type someType2 = {
  readonly auto?: string; 
  readonly fit?: string; 
  readonly crop?: string; 
  readonly "fp-z"?: string
};

Should be generated as

export type someType<a> = {
  readonly auto?: string; 
  readonly fit?: string; 
  readonly crop?: string; 
  readonly "fp-z"?: string
};

// tslint:disable-next-line:interface-over-type-literal
export type someType2 = {
  readonly auto?: string; 
  readonly fit?: string; 
  readonly crop?: string; 
  readonly "fp-z"?: string
};
cristianoc commented 2 years ago

Good catch, thanks for reporting.