clj->js trims the namespace prefix from keywords while writing them to string

Description

The following behavior was observed and confirmed from the code:

(clj->js :ns/n) => "n"

I believe this is a limitation and the namespace of the keyword should be kept while writing it to string.
The code in core.js does this while handling keywords:

(keyword? x) (name x)

while it should do this (or something similar):

(keyword? x) (str (namespace x) "/" (name x))

Environment

None

Activity

Show:
Andrea Richiardi
January 18, 2017, 7:52 AM

I have just stumbled across this one, shall we at least say it in the docstring of clj->js that we are losing the namespace part?

Jozef Wagner
February 19, 2017, 9:11 PM

With the introduction of specs, the namespaced keywords are being used more and more. This issue prevents streamlined edn->json->edn transformation. I think it should be reopened. IMO the 'lossy' method should never be a default one.

Paulus Esterhazy
February 19, 2017, 10:53 PM

Unless we are willing to break existing code, I don't think it will be possible to change the default behavior.

I'm also not sure that extending IEncodeJS is the best solution, as it affects every call to clj->js, including calls to libraries which may rely on the ns-stripping behavior.

However, the attached patch allows you to make the decision on a per-call basis.

One quibble with the patch: perhaps it would be better to use kwargs style `(clj->js v reserve-namespaces true)` in line with `js->clj`?

import
June 14, 2017, 1:26 AM

Comment made by: lewang

How would extending IEncodeJS work if I want to only include namespaces sometimes, and don't want to change the default behaviour for all libraries I've included? If this is not possible, it seems this is not a valid solution so this problem.

Enzzo Cavallo
July 10, 2017, 12:00 PM

I would like to propose a solution
https://gist.github.com/souenzzo/46da88205f90f5ffba0c0a11b8f32119
(First I made a patch, but I saw that the jira does not accept any more attachments, so I published in the gist)
It allows to process keywords with any function
Dont break any existing code
Use named arguments (kwargs style)

Declined

Assignee

Unassigned

Reporter

import

Labels

Approval

None

Patch

None

Priority

Major