-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
fix(cli/repl): interpret object literals as expressions #7591
fix(cli/repl): interpret object literals as expressions #7591
Conversation
75f0e74
to
1fc75d3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixes #1421. I suppose this is better even if block statements can't be used.
Co-authored-by: Nayeem Rahman <nayeemrmn99@gmail.com>
The existing behaviour is consistent with Node.js. Is there compelling reason to vary from that? |
Ah, but |
Also, Maybe a RegEx that is a bit more specific in looking for things that look like an object literal but ignores things that couldn't be an object literal. The following: /^\s*{\s*(?:\S+|["'][^"']+["']):.+}\s*$/ Matches: { "foo": bar }
{ 'foo bar': bar }
{ [foo]: bar } But ignores: { "" } It isn't perfect, but it is really hard to make it perfect because of the lack of back references in JavaScript. |
Node does the same, but with a different pattern match.
Throws in Safari, not sure about Firefox (not installed at the moment).
Yeah that regex is going to be pretty hard to get right, it should allow for non-quoted keys, but non-quoted keys look like labels, et cetera. I think it might be better to have a rollback mechanism, e.g if our preprocessed code turned out to be a syntax error, do it again without the preprocessing. |
Slightly more forgiving approach that leaves the parsing up to the compiler; if it looks like it might be an object literal we try it as an expression first, if thats a compile error we rollback and retry with the raw original code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool - nice hack.
This wraps object literals in parenthesis so that they're interpreted as expressions rather than block statements which matches the behaviour of Node and web-browsers.
Fixes #1421