Skip to content

Emitted JS / Gentype double-escapes output #8088

@illusionalsagacity

Description

@illusionalsagacity

Thank you for filing! Check list:

  • Is it a bug? Usage questions should often be asked in the forum instead.
  • Concise, focused, friendly issue title & description.
  • A minimal, reproducible example.
  • OS and browser versions, if relevant.
  • Is it already fixed in master? Instructions

When using the @as attribute with a value containing escape sequences (e.g., @as("with\"dquote")), the generated JavaScript incorrectly double-escapes the string. Additionally the parser can't handle exotic identifiers with a \".

I found this while looking at #8086 and removing String.escaped from this line:

| Some s -> s |> String.escaped

This existing test with an escaped double-quote \" in the @as annotation

@genType
type myRecBsAs = {
@as("jsValid0")
valid: string,
@as("type")
type_: string,
@as("the-key")
theKey: string,
@as("with\"dquote")
withDQuote: string,
@as("with'squote")
withSQuote: string,
@as("1number")
number1: string,
}

Results it being double-escaped in the generated typescript types and javascript:

export type myRecBsAs = {
readonly jsValid0: string;
readonly type: string;
readonly "the-key": string;
readonly "with\\\"dquote": string;
readonly "with'squote": string;
readonly "1number": string
};

function testMyRecBsAs(x) {
return [
x.jsValid0,
x.type,
x["the-key"],
x["with\\\"dquote"],
x["with'squote"],
x["1number"]
];
}


There's also a parser bug:

https://github.com/illusionalsagacity/rescript-compiler/blob/3293dcb812e416afccb0900df933bbbb533578f2/tests/gentype_tests/typescript-react-example/src/Records.res#L125-L142

Error in @tests/gentype-react-example:

  Syntax error!
  /rescript-compiler/tests/gentype_tests/typescript-react-example/src/Records.res:135:11-17

  133 ┆ @as("with\"dquote")
  134 ┆ withDQuote: string,
  135 ┆ \"with\"dquote2": string,
  136 ┆ @as("with'squote")
  137 ┆ withSQuote: string,

  Did you forget a `,` here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions