-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[Bug] transformer can't tag variable scope correctly in some edge case #9791
Comments
It seems If we remove the |
reduced version for anyone interested. |
Possible duplicate of #6820 |
Yes, I found it's a history problem |
Context(for future users) Babel check for existence of conflicting variable names while injecting a variable because they have full control over scope while transpilation. SWC is written in Rust and storing the list of identifiers somewhere else was very hard. So I didn't store it, and as a result, the variable renamer should distinguish the variable written by user and the variable injected by SWC code. |
The key point is why swc removes renamer when top-level // source
const a = 10;
{
const a = 20;
eval("console.log(a);") // 20
} // after babel transform with @babel/plugin-transform-block-scoping
var a = 10;
{
var _a = 20;
eval("console.log("a")"); // 10
} It will unexpected when user It seems swc select another way, sometime it's a good select. But it would occur error in this case: var data = {
name: "xxx",
names: ["x1", "x2", "x3"],
}
var { name, names } = data;
for(var name of names) {
// do something
}
var callback = () => {
console.log(name); // "x3"
eval('yyy')
}
callback() |
const a = 10;
{
const a = 20;
eval("console.log(a);"); // 20
}
eval("console.log(a);"); // 10 Downgrading |
So we don't care about |
If that's the case maybe there's nothing we could do with it. |
Describe the bug
The code after swc transformer, would cause incorrectly behavior.
The code would transform like this:
More detail see: web-infra-dev/rspack#8637 (comment)
Input code
Config
Playground link (or link to the minimal reproduction)
https://play.swc.rs/?version=1.10.1&code=H4sIAAAAAAAAA91VUW%2BbMBB%2Bz6%2B4VlNlTwwlewxNoz1sUqWtk%2FoaRa0DDnFDAIFJE6X8952NDQa6Tdrj8sLB3X333Xf2JczSUkKYHfKs5PdpJEImswIWQPb8nGYR9yDJ4u%2BilBQWd3CZAIgtkCvjhbc3MKaf8DSWO7iFGYWCy6pIYbUOMGGLgCThEgSI1IZTjQVYWhG4qM9PCZbxMDdhUmSpByk7YP1yl70%2ByXPOoUZeJn0l1qr2pQ40iuWkIQwpbfdYYS0p0ooHTuX8%2BKVQ%2FTZUHbIvhqyGsWw7vmyzKRxuHojI0%2BEisjx15uqlR7QDyI8eVEcVaxxglfa3Io0IEa3i9qe6RPibGxC%2BiGjPB1Zz5YLFYoGEAsdfA09KriEMSw3T2L%2BFMqEKrjF7kK1d036PWlQ%2Fr8od6YCx4dauHFsr2b51ik4sdoNaO0OLmGRKqffmlqi5aQLjoSnNR4NrhtCkrJLBtJRe%2BJ2dfVHqJ8mP1JWrrbtv6tLRxK4s29V%2BPRRa%2Be1593Hq%2FPRzSxRDCnfwaTYMxyPC1W3Nz48mCYnb%2FGAQ2kmFQd0lUrOcwRKpIh%2BYY%2Fv4fD%2B34PEJc1P%2BCo88%2FnrKyfOHi2JXP3twHV%2FTcVqPmfvqFzxPWMiJAvU0rVG6Fao5OW72ILQeH%2Byxro7s%2FyqtA%2FH%2FqKuI9bP%2FIO5kaNXOZex2sZ8fHbn8A8uH%2B6vpOWRJWGFRFAr4kSUY1N%2BL%2BM1ZiANdHWJLFwp99ykuTSHPvU6WMO29z%2BEHkzu%2FyCrcr20%2BnbgRf4Xtg87hoTpseEE6PPgIs%2BmU%2BjL7Jk48Ip9p26Pdq%2BUDeyDYKlVoiIGmWXIm1FE2TES4lyxWRwX%2FD5VfqW%2BgTGAwqYNfz1P1mssHAAA%3D&config=H4sIAAAAAAAAA1WQSw6DMAxE95wCed1tu%2BgdeggrNVUQ%2Bch2pCLE3ZtAQmEXv%2FGM7Sxd38MoBp79kp%2B5iMhCfNSZyOwVv5kAGYdi2EaFW1NHKdKAk9CG1l0BRf6Qbi65Q4OMXobA7pzPhEZPoKDk1ToqbkwaHKo1UOX1MmYKQagtUJmz3g7zeYQJLjKJXBtLK%2FrPRNcDupoOLrzTJtav0TnSftAD%2Fk1t2BEMVl7NqZyoW3%2FQBsGgZgEAAA%3D%3D
SWC Info output
No response
Expected behavior
Transformer correctly.
Like babel transform, the code would be like:
Actual behavior
No response
Version
1.10.1
Additional context
No response
The text was updated successfully, but these errors were encountered: