reasonml / reasonml/reason

Escaping object field names: possibly confusing underscore

Open
#1,917 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

KIND: FEATURE REQUEST Parser
Dominant language
OCaml
Stars
10.3k
Forks
438
PR merge metrics
No merged PRs in 30d

Description

Consider this JavaScript object:

var node = {
  type: "Identifier"
};

To create this object in Reason, we have to escape the type field by prefixing it with an underscore because type is a keyword:

let node = {
  "_type": "Identifier"
};

I find this syntax to be quite confusing for the following reasons:

  • The double quotes suggest that _type is the literal name of the property. To me, the underscore doesn't look like an escape character in this position.
  • The underscore is commonly used in JavaScript to denote a (pseudo-)private property.
  • The underscore is a valid code point at the start of an identifier.

What are your thoughts on moving the underscore outside of the double quotes?

let node = {
  _"type": "Identifier"
};

This way, it's clearer that the generated property name will be type (not _type).


Another approach worth exploring might be to choose another character for escaping. Either way, I'd suggest moving the escape character outside of the double quotes.

Contributor guide

No contributing guide indexed for this repository

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 by reviewing the issue's Reason and JavaScript object examples and the two proposed escaping syntaxes. Determine whether the language should change how keyword field names are escaped or use another escape character. Done means reaching and documenting a decided syntax direction; no files or tests are identified in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, ocaml
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.