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

swc_ecma_codegen: missing parenthesis around IIFE results in invalid output #9907

Closed
maxicode2 opened this issue Jan 20, 2025 · 3 comments
Closed
Labels
Milestone

Comments

@maxicode2
Copy link

maxicode2 commented Jan 20, 2025

Describe the bug

When Compiler.print() is called with an AST consisting of an empty IIFE, the function doesn't wrap function(){} in parenthesis, causing invalid javascript to be returned.

Input code

use swc::Compiler;
use swc_ecma_ast::{CallExpr, Callee, Expr, ExprStmt, FnExpr, Function, Module, ModuleItem, Program, Stmt};

pub fn main() {
    let c = Compiler::new(Default::default());

    println!("{}", c.print(
        &Program::Module(Module {
            body: vec![
                ModuleItem::Stmt(Stmt::Expr(ExprStmt {
                    expr: Box::new(Expr::Call(CallExpr {
                        callee: Callee::Expr(Box::new(Expr::Fn(FnExpr {
                            function: Box::new(Function {
                                body: Some(Default::default()),
                                ..Default::default()
                            }),
                            ..Default::default()
                        }))),
                        ..Default::default()
                    })),
                    ..Default::default()
                }))
            ],
            ..Default::default()
        }),
        Default::default()
    ).unwrap().code);
}

Config

Playground link (or link to the minimal reproduction)

https://gist.github.com/maxicode2/35a93dcac5984fa7333af7ab491bb380

SWC Info output

No response

Expected behavior

The AST is printed as (function(){})() and node.js returns no error

Actual behavior

The AST is printed as function() {}() and node.js returns an error:

[stdin]:1
function() {}();
^^^^^^^^

SyntaxError: Function statements require a function name

Version

swc@10.0.0, swc_ecma_ast@5.0.3, swc_ecma_codegen@5.1.0

Additional context

No response

@maxicode2 maxicode2 changed the title codegen: missing parenthesis around IIFE results in invalid output swc_ecma_codegen: missing parenthesis around IIFE results in invalid output Jan 20, 2025
@kdy1
Copy link
Member

kdy1 commented Jan 21, 2025

Use fixer

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 21, 2025
@kdy1 kdy1 added this to the Planned milestone Jan 21, 2025
@kdy1
Copy link
Member

kdy1 commented Jan 21, 2025

@kdy1 kdy1 modified the milestones: Planned, v1.10.9 Jan 21, 2025
@swc-bot
Copy link
Collaborator

swc-bot commented Feb 21, 2025

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 Feb 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants