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

"private should be removed by class_properties" error when use preset_env #7956

Closed
zhangpanweb opened this issue Sep 15, 2023 · 1 comment · Fixed by #7958
Closed

"private should be removed by class_properties" error when use preset_env #7956

zhangpanweb opened this issue Sep 15, 2023 · 1 comment · Fixed by #7958
Labels
Milestone

Comments

@zhangpanweb
Copy link

zhangpanweb commented Sep 15, 2023

Describe the bug

When use preset_env to fold ast of using this.# and ||=, for examplethis.#a ||= 1, swc will panic at private should be removed by class_properties.

Discovery these after read the logic of swc:

  • preset_env will use folder according to inner plugin.json data and target config.
  • In Chrome 74, class_property have been natively implemented , see plugin.json . Therefore, it will not be used
  • but logical_assignments depends on class_property. So it will run into the unreachable!("private should be removed by class_properties")

If change the target to chrome 73 or include class_property when use preset_env, the code will run as normal

Input code

fn main() {
    let source = r#"
    class A {
        b() {
            this.#a ||= 1;
        }
    }
"#;

    let cm: Lrc<SourceMap> = Default::default();
    let fm = cm.new_source_file(FileName::Real(Path::new("").to_owned()), source.to_owned());

    let lexer = Lexer::new(
        Syntax::Es(EsConfig {
            ..Default::default()
        }),
        swc_ecma_ast::EsVersion::default(),
        StringInput::from(&*fm),
        None,
    );

    let mut parser = Parser::new_from(lexer);

    let ast = parser.parse_module().unwrap();

    let globals = Globals::default();
    let targets: Targets = serde_json::from_str(r#"{"chrome":74}"#).unwrap();
    GLOBALS.set(&globals, || {
        let _ = try_with_handler(cm.clone(), Default::default(), |handler| {
            HELPERS.set(&Helpers::new(true), || {
                HANDLER.set(handler, || {
                    let mut preset_env = swc_ecma_preset_env::preset_env(
                        Mark::new(),
                        Some(NoopComments),
                        swc_ecma_preset_env::Config {
                            targets: Some(targets),
                            mode: Some(swc_ecma_preset_env::Mode::Entry),
                            ..Default::default()
                        },
                        Assumptions::default(),
                        &mut FeatureFlag::default(),
                    );
                    preset_env.fold_module(ast.clone());
                });
            });

            Ok(())
        });
    });
}

Config

No response

Playground link

https://github.com/zhangpanweb/swc-private-assign-error

SWC Info output

No response

Expected behavior

preset_env will work as normal, and not panic

Actual behavior

No response

Version

swc_common: 0.31.17

Additional context

No response

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 19, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants