Use raw value of JSXText and JSXAttribute#5256
Conversation
|
Hey @rattrayalex! It looks like one or more of your builds have failed. I've copied the relevant info below to save you some time. |
|
Tests are failing b/c they're reading from the wrong version of Babylon; not sure how that is best addressed. |
|
Thanks for your PR 👍 If tests are green locally, that's fine for now. We need to wait for babel/babylon#344. |
There was a problem hiding this comment.
Thanks for your PR but this is wrong, as JSX is not escaped as JS but as XHTML. That is the reason why the raw value is currently not present, so that we can escape the raw xhtml value with jsesc in babel when the jsx tag is converted to a StringLiteral. This will currently break I think and jsx->js is probably more used than jsx->jsx.
Having no raw for now is correct.
|
@danez can you provide an example test case? |
|
Ahh, I think I understand... I essentially need to add tests with the If so, didn't quite grasp the nature of the challenge at hand, apologies. Judging by @kittens 's rants about the EDIT: obviously not ideal, but perhaps better than removing an otherwise valid attribute from |
| const raw = node.extra && node.extra.raw; | ||
| this.token("="); | ||
| this.print(node.value, node); | ||
| this.print(raw || node.value, node); |
There was a problem hiding this comment.
I know this PR has a few comments against the current implementation already, but I also want to add that this could in some cases lead to a breaking change. Say you had a transform that worked on these nodes that changed the .value. With this PR, the mutated value would be ignored in the final output since raw would exist. You'd want to use getPossibleRaw:
babel/packages/babel-generator/src/printer.js
Line 390 in 672adba
value is compared to rawValue
There was a problem hiding this comment.
Hmm, I'd intentionally avoided that because of the value and rawValue comparison, thinking that they'd be unequal in exactly the scenarios this PR attempts to address. But I didn't actually test that, and of course your point is valid, so I'll try to find a way to address it (may need to compare an escaped version of value or similar).
Thanks for the helpful catch @loganfsmyth !
There was a problem hiding this comment.
I think you can use getPossibleRaw the same way we do for strings: https://github.com/babel/babel/blob/master/packages/babel-generator/src/generators/types.js#L139, if this PR is in fact accepted.
There was a problem hiding this comment.
Cool, I'll give it a try. Thanks!
a882792 to
8fe77b8
Compare
|
Thanks for the feedback @loganfsmyth – using I'm now deleting I also added tests explicitly for the jsx->js case. @danez does this look better to you? |
|
Hey @rattrayalex! It looks like one or more of your builds have failed. I've copied the relevant info below to save you some time. |
|
(as before, tests failing because they don't have the matching version of babylon, and pass locally) |
| @@ -0,0 +1,3 @@ | |||
| <div id="wôw" />; | |||
| <div id="\w" />; | |||
| <div id="w < w" />; | |||
There was a problem hiding this comment.
would appreciate suggestions for other test-cases, as I think this is the crux of the issue (and was previously untested)
| const raw = this.getPossibleRaw(node); | ||
| this.token("="); | ||
| this.print(node.value, node); | ||
| this.print(raw || node.value, node); |
There was a problem hiding this comment.
I don't think this particular call is actually doing anything, because JSXAttribute nodes don't have a .raw, their .value property does. I'm assuming this is working entirely because StringLiteral already checks for .raw.
There was a problem hiding this comment.
Ah, yes, good catch. Thanks.
| export function JSXText(node: Object) { | ||
| this.token(node.value); | ||
| const raw = this.getPossibleRaw(node); | ||
| this.token(raw || node.value); |
There was a problem hiding this comment.
Can we explicitly check === null since raw being an empty string would also cause this to use .value instead?
Thinking on this all a bit, I'm up for adding raw handling if we need it, but it seems like we should also have additional logic both here and in StringLiteral to handle encoding of .value, otherwise it's still totally possible for people to assign values to JSXText that will not be printed properly. The intention of .raw is to not lose the original formatting in the source file, if possible, but either way we don't want usage of .value without .raw to cause issues.
There was a problem hiding this comment.
Can we explicitly check === null since raw being an empty string would also cause this to use .value instead?
Yep, good catch – though I assume you mean == null ?
I'll respond to the second para in a follow-up comment, since this thread is about to disappear when I push.
8fe77b8 to
262a42d
Compare
|
(pulling from in-thread comment from @loganfsmyth )
I'm not quite sure what you're suggesting? I think the scenario you're concerned about is when a plugin modifies or creates a JSXText node with an invalid |
|
Hey @rattrayalex! It looks like one or more of your builds have failed. I've copied the relevant info below to save you some time. |
|
I think what @loganfsmyth means is that even if |
|
I'm fine with this as is for 7.0. |
|
Gotcha. Thanks @danez ! |
|
Can you rebase on the 7.0 branch? |
|
Will do – though specs will still fail until babel/babylon#344 has gone out on 7.0 |
262a42d to
abf6d58
Compare
|
Rebased and changed target |
|
Looks like this is causing some test failures now that it is merged. Anyone know what's up? Did we land this too early since you said it depends on Babylon? |
|
The tests are failing now because they are assuming new Babylon version. I wanted to create a new Babylon release for 7.0 but haven't gotten around. The change was bc so nothing really is broken. |
Depends on babel/babylon#344 (see that PR for description; happy to elaborate here if desired).