rescript-lang / rescript-lang/rescript

Emitted JS / Gentype double-escapes output

Open
#8,088 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
OCaml
Stars
7.5k
Forks
485
Avg merge
1d 2h
Merged PRs (30d)
55

Description

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:

https://github.com/rescript-lang/rescript/blob/14721cb26f727e20fa834dbcce28fb439ad2e006/compiler/gentype/TranslateTypeDeclarations.ml#L48

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

https://github.com/rescript-lang/rescript/blob/14721cb26f727e20fa834dbcce28fb439ad2e006/tests/gentype_tests/typescript-react-example/src/Records.res#L125-L139

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

https://github.com/rescript-lang/rescript/blob/14721cb26f727e20fa834dbcce28fb439ad2e006/tests/gentype_tests/typescript-react-example/src/Records.gen.tsx#L51-L58

https://github.com/rescript-lang/rescript/blob/14721cb26f727e20fa834dbcce28fb439ad2e006/tests/gentype_tests/typescript-react-example/src/Records.res.js#L106-L115


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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at compiler/gentype/TranslateTypeDeclarations.ml around line 48, then inspect the @as cases in tests/gentype_tests/typescript-react-example/src/Records.res and their generated .gen.tsx and .res.js outputs. Run the existing gentype test and reproduce both escaped-quote cases. Done means the generated JavaScript and TypeScript do not double-escape the value and the parser accepts the exotic identifier.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, ocaml, typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.