fix(codegen): print space before with clause in import (#2278)

This commit is contained in:
Wenzhe Wang 2024-02-02 22:52:32 +08:00 committed by GitHub
parent 3569e42475
commit 0c225a49aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 0 deletions

View file

@ -654,6 +654,9 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ImportDeclaration<'a> {
p.print(b'\'');
p.print_str(self.source.value.as_bytes());
p.print(b'\'');
if self.with_clause.is_some() {
p.print_hard_space();
}
self.with_clause.gen(p, ctx);
p.print_semicolon_after_statement();
return;
@ -718,6 +721,9 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ImportDeclaration<'a> {
p.print_str(b" from ");
}
self.source.gen(p, ctx);
if self.with_clause.is_some() {
p.print_hard_space();
}
self.with_clause.gen(p, ctx);
p.print_semicolon_after_statement();
}
@ -734,6 +740,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for Option<WithClause<'a>> {
impl<'a, const MINIFY: bool> Gen<MINIFY> for WithClause<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
self.attributes_keyword.gen(p, ctx);
p.print_soft_space();
p.print_block(&self.with_entries, Separator::Comma, ctx);
}
}
@ -815,6 +822,9 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ExportAllDeclaration<'a> {
p.print_str(b" from ");
self.source.gen(p, ctx);
if self.with_clause.is_some() {
p.print_hard_space();
}
self.with_clause.gen(p, ctx);
p.print_semicolon_after_statement();

View file

@ -104,4 +104,7 @@ fn template() {
#[test]
fn module_decl() {
test("export * as foo from 'foo'", "export * as foo from 'foo';\n");
test("import x from './foo.js' with {}", "import x from './foo.js' with {\n};\n");
test("import {} from './foo.js' with {}", "import './foo.js' with {\n};\n");
test("export * from './foo.js' with {}", "export * from './foo.js' with {\n};\n");
}