Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module type has no match in show() #4

Open
mfidemraizer opened this issue Jul 22, 2018 · 2 comments
Open

Module type has no match in show() #4

mfidemraizer opened this issue Jul 22, 2018 · 2 comments

Comments

@mfidemraizer
Copy link

mfidemraizer commented Jul 22, 2018

Dynamic imports in ES return a Promise Module and the String.toStringTag pattern matches of show() don't cover [object Module].

Related sanctuary-js/sanctuary-def#218

@davidchambers
Copy link
Member

How would one represent a module as a string?

@Avaq
Copy link
Member

Avaq commented Jul 25, 2018

To stay true to eval(show(x)) == x, the following representation would be our goal (for a module created by importing a file with the contents export default 42):

'Object.assign(Object.create(null), {[Symbol.toStringTag]: "Module", "default": 42})'

There are three additions needed to achieve this goal, the first being the most important:

  1. Handing objects with a custom toStringTag: if [object Module] would be treated like a normal object, things would not crash, and we'd get '{"default": 42}'. The problem is that it has a Symbol.toStringTag-property that modifies the behaviour of Object.prototype.toString. In other words: In recent versions of JavaScript, it is no longer safe to use Object.prototype.toString.call(x) to determine the base type of a value. We will see a big influx of new return values from that expression now that JS gave users control to modify it. To make matters worse, the introduction of Object.create(null) made String(x) an unsafe operation. To tackle this issue, I think the [object Object] case should be removed, and the default case needs its own conditional logic based on the typeof x, allowing different kinds of objects to be shown as objects.
  2. A special case for objects created with Object.create(null), which would account for the Object.assign(Object.create(null), ...)-part of the string.
  3. Handle properties which are Symbols using Object.getOwnPropertySymbols(), which would account for the [Symbol.toStringTag]: "Module"-part of the string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants