-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
babel-generator: Ensure ASCII-safe output for string literals #4478
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
Conversation
Current coverage is 88.32% (diff: 100%)
|
| '\x20'; | ||
| "\n\r"; | ||
| "😂"; | ||
| `😂`; |
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.
I’ve added this test to indicate that this patch only handles string literals and not template literals. (Regular expression literals are already taken care of through our use of regexpu.)
| let val = jsesc(node.value, { | ||
| quotes: t.isJSX(parent) ? "double" : this.format.quotes, | ||
| wrap: true | ||
| }); |
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.
The use of jsesc also allows us to get rid of the hacky parts below (i.e. remove double quotes, unescape double quotes, escape single quotes, add single quotes).
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.
Nice to remove all that 😄
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.
Fun fact: I updated Babel to 6.17 at Facebook and it broke one of our (PHP) parsers and the entire transform pipeline as a result :D After tedious investigation, I think this is the culprit. When someone uses "×" in our codebase (within custom tags) it's then transformed to '×' which Babel represented as '\u00D7' before. Now jsesc turns it into '\xD7'. Apparently PHP parser doesn't understand the latter. ¯_(ツ)_/¯
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.
@kangax Which PHP parser is that?
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.
Dear god... Tracked it down further to json_decode returning null whenever there's \x in a string.
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.
That actually makes sense as JSON only supports \uXXXX-style escape sequences. (As a hotfix, you could use jsesc(string, { json: true }) here in Babel to enforce JSON output but really, it sounds like you need another parser since the intention is to parse JavaScript strings rather than JSON strings.)
|
Actually, it would be nice if the changelog included new/removed deps |
Babel now generates ASCII-safe output for string literals, see <babel/babel#4478>.
wenbing
left a comment
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.
make it unreadable!
Fixes #4477.