Skip to content
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

[minifier] References created by with statements is treated as global references #9856

Open
sapphi-red opened this issue Jan 9, 2025 · 2 comments
Labels
Milestone

Comments

@sapphi-red
Copy link

Describe the bug

An identifier named Object is treated as a global reference even if it's inside a with statement. The object passed to the with statement might contain Object property.

I checked that this is not written in the assumptions.

//! Like other minification tools, swc minifier assumes some things about the
//! input code.
//!
//! - TDZ violation does not exist.
//!
//! In other words, TDZ violation will be ignored.
//!
//! - Acesssing top-level identifiers do not have side effects.
//!
//! If you declare a variable on `globalThis` using a getter with side-effects,
//! swc minifier will break it.

Input code

const foo = {
  Object: class {
    constructor() { console.log('Constructor') }
  }
}
with (foo) {
    console.log(new Object())
}

Config

No response

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.10.6&code=H4sIAAAAAAAAA0vOzysuUUjLz1ewVajm5VJQ8E%2FKSk0usVJIzkksLoYIKSgkg1QVlSaX5BdpaCpUg%2Fn5Oal6OfnpGurOCEl1TYVakA4gAUTlmSUZChpAszWRzYHpy0sth1qmoakJVg8A07%2FfAIwAAAA%3D&config=H4sIAAAAAAAAA32UO5LbMAyG%2Bz2FR3WKnS1S5ADpcgYOTYIyHZLQEKDXmh3fPdDDj6whdxI%2B%2FAAJgPh62%2B26I7nu1%2B5LPuVnsJWg3v7FQmNhexZLBy5bcjUO3P240iNNKNhEMJsuC%2BnY1h54VtHH%2B8f7qugSIsFVsdpyLDGMjzkd5qEC0YNNrBKyZShM%2F%2BtXVvFzAlzbo32PmMCWF8RYMrEw9FC1wA5TsgOBOdmqRJlOamsk1FJMsDF4M1QcVF585IhFcj5TD9Ybhx4UFCs4jifQZJJLZIXkesp9Zuxh3%2Fp%2B7vM3NZxsapaVnHCeWyKnVaIeMBKb0IpWwgVu1GCBa3G%2FK2MwFbjV8qw7YiwbPfkLIBVIlqjYDFrc2SPIPG2pw0tlLEFGlkeFy3xrtyzQS1FNjEGp7FQZqBy1blbwzcFUWacdZ8Ub5aPowUAIMitKaPqM7A5aUh4HwKAA6a8N2lQtwNxe4QafHsQL%2FFtuyfqArR7Z8mGb0pj3mF4kyMAH9C8cpBWM27jKljgP27wVDzIa4FWXRjN4XgLyABhNmvfl02zI85CIpk%2B4v6%2BJ1eFy28PZlv7%2B3pdV%2FLY6dBl9m%2BG65Kf%2BLqv5Z3d3um7h28G7SH%2BuyiXs5R%2BuiUkIMQYAAA%3D%3D

SWC Info output

No response

Expected behavior

The code to be output as-is. Constructor is output in console.

const foo = {
  Object: class {
    constructor() { console.log('Constructor') }
  }
}
with (foo) {
    console.log(new Object())
}

Actual behavior

new Object() is replaced with {}. Constructor is not output in console.

let foo = {
    Object: class {
        constructor(){
            console.log('Constructor');
        }
    }
};
with (foo)console.log({});

Version

1.10.6

Additional context

This is not something I encountered with a real world code so it's probably fine to deprioritize this issue.

@kdy1
Copy link
Member

kdy1 commented Jan 9, 2025

I'm not sure if it's already the case, but I think we want to assume there's no with in the input.

@sapphi-red
Copy link
Author

That would be fine as well.

@kdy1 kdy1 added this to the Planned milestone Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants