Skip to content

expr_simplifier replaces ternary expression with sequence (..., 0) #2165

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

Closed
mischnic opened this issue Aug 26, 2021 · 1 comment · Fixed by #2166
Closed

expr_simplifier replaces ternary expression with sequence (..., 0) #2165

mischnic opened this issue Aug 26, 2021 · 1 comment · Fixed by #2166
Assignees
Labels
Milestone

Comments

@mischnic
Copy link
Contributor

Describe the bug

expr_simplifier replaces a ternary expression ... ? 0 : 1 with (..., 0)

Input code

var bit = 0;
var sum = 0;
sum += (bit ^= 1) ? 0 : 1;
sum += (bit ^= 1) ? 0 : 1;
console.log(sum);

Config

Details
use swc_common::{
    chain, comments::SingleThreadedComments, sync::Lrc, FileName, Globals, Mark, SourceMap,
};
use swc_ecma_ast::Module;
use swc_ecma_codegen::{text_writer::JsWriter, Config, Emitter};
use swc_ecma_parser::{lexer::Lexer, EsConfig, PResult, Parser, StringInput, Syntax};
use swc_ecma_preset_env::preset_env;
use swc_ecma_transforms::{
    compat::reserved_words::reserved_words,
    fixer, helpers, hygiene,
    optimization::simplify::{dead_branch_remover, expr_simplifier},
    react,
    resolver::resolver_with_mark,
};
use swc_ecma_visit::FoldWith;

fn main() {
    let cm = Lrc::<SourceMap>::default();
    let src = r#"
var bit = 0;
var sum = 0;
sum += (bit ^= 1) ? 0 : 1;
sum += (bit ^= 1) ? 0 : 1;
console.log(sum);
"#;
    let (module, comments) = parse(src, "test.js", &cm).unwrap();

    let transform = &mut expr_simplifier();
    let module = module.fold_with(transform);
    let module = module.fold_with(&mut chain!(
        reserved_words(),
        hygiene(),
        fixer(Some(&comments))
    ));

    let code = emit(&module, &comments, cm);
    println!("{}", code);
}

fn parse(
    code: &str,
    filename: &str,
    cm: &Lrc<SourceMap>,
) -> PResult<(Module, SingleThreadedComments)> {
    let source_file = cm.new_source_file(FileName::Real(filename.into()), code.into());
    let comments = SingleThreadedComments::default();

    let lexer = Lexer::new(
        Syntax::Es(EsConfig {
            jsx: true,
            ..Default::default()
        }),
        Default::default(),
        StringInput::from(&*source_file),
        Some(&comments),
    );
    let mut parser = Parser::new_from(lexer);
    match parser.parse_module() {
        Err(err) => Err(err),
        Ok(module) => Ok((module, comments)),
    }
}

fn emit(module: &Module, comments: &SingleThreadedComments, cm: Lrc<SourceMap>) -> String {
    let mut buf = vec![];
    {
        let writer = Box::new(JsWriter::new(cm.clone(), "\n", &mut buf, None));
        let config = Config { minify: false };
        let mut emitter = Emitter {
            cfg: config,
            comments: Some(&comments),
            cm,
            wr: writer,
        };
        emitter.emit_module(&module).unwrap();
    }

    String::from_utf8(buf).unwrap()
}

Current behaviour

var bit = 0;
var sum = 0;
sum += (bit ^= 1, 0);
sum += (bit ^= 1, 0);
console.log(sum);

Expected behavior
The behaviour of the code shouldn't change.

Version

d975a19

Context
parcel-bundler/parcel#6766

@mischnic mischnic added the C-bug label Aug 26, 2021
@kdy1 kdy1 added this to the v1.2.83 milestone Aug 26, 2021
@kdy1 kdy1 mentioned this issue Aug 27, 2021
3 tasks
kdy1 added a commit to kdy1/swc that referenced this issue Aug 27, 2021
kdy1 added a commit to kdy1/swc that referenced this issue Aug 27, 2021
@kdy1 kdy1 self-assigned this Aug 27, 2021
kdy1 added a commit that referenced this issue Aug 27, 2021
swc_ecma_parser:
 - Fix parsing of typescript generics in jsx context. (#2161)

swc_ecma_transforms_base:
 - `fixer`: Handle assignment with patterns in arrow body. (#2163)

swc_ecma_utils:
 - `as_bool`: Handle assignment with operator correctly. (#2165)
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 22, 2022

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 22, 2022
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