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

Comments around jsx omitted while printing #9561

Open
oosawy opened this issue Sep 17, 2024 · 0 comments
Open

Comments around jsx omitted while printing #9561

oosawy opened this issue Sep 17, 2024 · 0 comments
Labels

Comments

@oosawy
Copy link

oosawy commented Sep 17, 2024

Describe the bug

Some comments around jsx omitted while printing just parsed program. This issue seems not to happen to the playground that transform jsx.

Input:

function C() {
    return /*#FOO*/<>{/*#BAR*/}hello world</> /*#BAZ*/ ;
}

Printed:

function C() {
    return <>{}hello world</> /*#BAZ*/ ;
}

Reproduction:

use swc::config::SourceMapsConfig;
use swc::{Compiler, PrintArgs};
use swc_common::comments::SingleThreadedComments;
use swc_common::sync::Lrc;
use swc_common::FileName;
use swc_common::{
    errors::{ColorConfig, Handler},
    SourceMap,
};
use swc_ecma_ast::EsVersion;
use swc_ecma_parser::TsSyntax;
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax};

fn main() {
    let cm: Lrc<SourceMap> = Default::default();
    let handler = Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(cm.clone()));

    let fm = cm.new_source_file(
        FileName::Real("input.js".into()).into(),
        // "function C(){ return /*#FOO*/<>{/*#BAR*/}hello world</>/*#BAZ*/; }".into(),
        "function C() {\n    return /*#FOO*/<>{/*#BAR*/}hello world</> /*#BAZ*/ ;\n}".into(),
    );

    let comments = SingleThreadedComments::default();

    let lexer = Lexer::new(
        Syntax::Typescript(TsSyntax {
            tsx: true,
            ..Default::default()
        }),
        EsVersion::latest(),
        StringInput::from(&*fm),
        Some(&comments),
    );

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

    for e in parser.take_errors() {
        e.into_diagnostic(&handler).emit();
    }

    let program = parser.parse_program().unwrap();

    let compiler = Compiler::new(cm);

    let result = compiler
        .print(
            &program,
            PrintArgs {
                comments: Some(&comments),
                source_map: SourceMapsConfig::Bool(false),
                ..Default::default()
            },
        )
        .expect("failed to print program");

    println!("{}", result.code);

    assert_eq!(
        result.code,
        "function C() {\n    return <>{}hello world</> /*#BAZ*/ ;\n}\n"
    );

    println!("{:#?}", comments);

    println!("{:#?}", program);
}

Input code

No response

Config

N/A

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.7.26&code=H4sIAAAAAAAAA0srzUsuyczPU3DW0FSo5uVSAIKi1JLSojwFfS1lN39%2FLX0bu2og08kxSEu%2FNiM1JydfoTy%2FKCfFRt9OASwepaWvYM3LVQsANOpalksAAAA%3D&config=H4sIAAAAAAAAA1WPMQ7CMAxFd05ReWYAJsSGmBg4hBVcFNTEke0gqqp3J4FSyma%2Fl2%2F9DKumgbs6ODRDGcuSUJRk3gvRPho%2BCwHrE6kTnwzWX2talUmmGV3JsaCxaDEtdrpQfcTg3TkkFptybzd%2BnoAJRm1ZwrKBEDpbgIpyNB%2BotsJsHNC8g0mP%2FxdRblTTQLrbbPdTdUhC5aMPOnbdiUOgaPor9A6DchZHF0yzGV%2FQfYRPMQEAAA%3D%3D

SWC Info output

No response

Expected behavior

Comments are printed / preserved.

Actual behavior

Some comments around jsx are omitted.

Version

swc = "0.287.0"

Additional context

versions:

swc = "0.287.0"
swc_common = { version = "0.38.0", features = ["tty-emitter"] }
swc_core = "0.104.2"
swc_ecma_ast = "0.119.0"
swc_ecma_codegen = "0.156.1"
swc_ecma_parser = "0.150.0"
swc_ecma_transforms = "0.241.0"
swc_ecma_visit = "0.105.0"
swc_visit = "0.6.2"
@oosawy oosawy added the C-bug label Sep 17, 2024
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

1 participant