-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Sourcemap misreports the locations of string literals which are JSX children #10869
Comments
Hey @TomasHubelbauer! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly. If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite." |
I looked into this, and the short answer is there's no good way to fix it. The best thing it to call But that's not satisfactory for multi line texts. Because we've "cleaned" the text, all |
@jridgewell Even if it's sub-optimal, maybe ensuring that the start of the string and what comes after the string have the correct mappings will be enough: you don't need to step through pieces of a multine-string when debugging, so you'll rarely notice that only the first line is mapped correctly. |
Yah, that works for me. @TomasHubelbauer, are you willing to implement this? |
I am! Could you please break it down for me though, I'm not able to follow what was written so far enough to know what to do. This will be my first contribution to Babel. |
Essentially, the text (Look for the In order for sourcemaps to be accurate, we need to ensure the the important AST nodes contains the location information. Currently, when we transform JSX elements that contain a The So what needs to happen is that we should preserve the location tracking information. We perform that with t.inherits. This will make our generated The code changes are:
|
@JLHwung I have updated my repo to show the problem still reproduces: https://github.com/TomasHubelbauer/babel-sourcemap I have updated all dependencies and simplified the output to show the problem better. The issue affects only string literals which are JSX/TSX children or attribute values. Tag name length and whitespace surrounding the string literal seems to play a role. Let me know if you can still repro on your end and if you could use any assistence from me. Thank you |
@TomasHubelbauer Thanks for the investigation, I'll take a look at it. |
I don't quite get what you mean, the expected output and the actual output in the repro repo look the same. |
Sorry I messed up the readme! I forgot to update the expected output! Please go by the notes below the expected output code block. They detail what I think is wrong in regards to the source map locations. I will check the source map visualization as soon as I can. Perhaps a problem is with my code. But why do you think |
#10869 (comment) |
I see. I think the problem must be on my end now since the visualization appears to be clearly showing the mapping is correct. I guess I am translating things wrong or feeding bad input positions into the sourcemap. I hope I will be able to work around the white-space issue. Thanks for fixing this! |
Bug Report
Current Behavior & Input Code
https://github.com/TomasHubelbauer/babel-sourcemap
In the output:
div
is mapped correctlyApp
is mapped correctlyh1
is mapped correctlyWelcome to my application!
is mapped to theh1
This is my app!
is mapped to thediv
strong
is mapped correctlyMINE.
is mapped to thestrong
So, it appears that string literals which are used as JSX children are not getting
mapped to their correct respective source locations, instead, they are mapped to the
JSX element's tag name.
Expected behavior/code
The string literals which are JSX children should be mapped to their respective
correct locations in the original source code.
Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)
babel.config.js
Ran using
npx babel src -s true --out-dir lib
.Environment
Possible Solution
🤷♂ IDK, probably some heuristic to tell from the source context we're in JSX children
needs to be added. I understand why this happens, it is just a string being passed to a
parameter of a function which is the JSX element as a whole, so I see why it gets mapped
to the JSX syntax start location. I think the way to fix this would be to add extra logic for
realizing this in the source map generation and keep track of the string literals which are
the arguments to the function and use their original locations for the string literals which
are the output arguments instead of defaulting any non-JSX child to the element's start.
The text was updated successfully, but these errors were encountered: