JavaScript object literals are printed wth keys that cannot be read

Description

If you evaluate #js {"foo bar" 2}, it prints as #js {:foo bar 2}.

I'd suggest that, for JavaScript keys that cannot be represented as unqualified keywords, string notation would be used.

Example:

could print as

Note that the ability to avoid namespace maps was added with CLJS-2190, which is in master.

Environment

None

Activity

Show:
Mike Fikes
July 28, 2017, 3:13 AM

The attached patch conditionally converts the JavaScript object key from a string to a keyword iff it is a string that can be converted to a legal unqualified keyword.

By "legal": Abides with the first bullet under Symbols https://clojure.org/reference/reader#_symbols, doesn't allow / (because the resulting keyword would be qualified) and doesn't allow . (because the description of keywords below disallows .).

The only bit that is not supported is the last bullet under Symbols: "A symbol can contain one or more non-repeating :'s." (but only inside the string, not at the beginning nor end). I'm presuming that this code is not in an overly performance critical code path, and that a regex approach is sufficiently performant, but trying to match this last aspect might make for a slow regex, compared to the simple one that simply allows zero or more characters from a given character class.

Completed

Assignee

David Nolen

Reporter

Mike Fikes

Labels

Approval

Accepted

Patch

Code and Test