Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thirty-balloons-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
swc_ecma_compiler: major
---

feat(es/compiler): Merge `export_namespace_from` and `nullish_coalescing`
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions crates/swc_ecma_compat_es2020/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ version = "23.0.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { workspace = true, features = ["derive"] }
swc_atoms = { version = "7.0.0", path = "../swc_atoms" }
serde = { workspace = true, features = ["derive"] }
tracing = { workspace = true }

swc_common = { version = "14.0.1", path = "../swc_common" }
swc_ecma_ast = { version = "14.0.0", path = "../swc_ecma_ast" }
swc_ecma_compat_es2022 = { version = "23.0.1", path = "../swc_ecma_compat_es2022" }
swc_ecma_compiler = { version = "0.1.2", path = "../swc_ecma_compiler" }
swc_ecma_transforms_base = { version = "22.0.1", path = "../swc_ecma_transforms_base" }
swc_ecma_utils = { version = "19.0.0", path = "../swc_ecma_utils" }
swc_ecma_visit = { version = "14.0.0", path = "../swc_ecma_visit" }
swc_trace_macro = { version = "2.0.2", path = "../swc_trace_macro" }
tracing = { workspace = true }

[dev-dependencies]
131 changes: 6 additions & 125 deletions crates/swc_ecma_compat_es2020/src/export_namespace_from.rs
Original file line number Diff line number Diff line change
@@ -1,128 +1,9 @@
use swc_atoms::Atom;
use swc_ecma_ast::*;
use swc_ecma_utils::private_ident;
use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut};
use swc_trace_macro::swc_trace;
use swc_ecma_ast::Pass;
use swc_ecma_compiler::{Compiler, Features};

pub fn export_namespace_from() -> impl Pass {
visit_mut_pass(ExportNamespaceFrom)
}

struct ExportNamespaceFrom;

#[swc_trace]
impl VisitMut for ExportNamespaceFrom {
noop_visit_mut_type!(fail);

fn visit_mut_module_items(&mut self, items: &mut Vec<ModuleItem>) {
let count = items
.iter()
.filter(|m| {
matches!(m, ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(NamedExport {
specifiers,
src: Some(..),
type_only: false,
..
})) if specifiers.iter().any(|s| s.is_namespace()))
})
.count();

if count == 0 {
return;
}

let mut stmts = Vec::<ModuleItem>::with_capacity(items.len() + count);

for item in items.drain(..) {
match item {
ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(NamedExport {
span,
specifiers,
src: Some(src),
type_only: false,
with,
})) if specifiers.iter().any(|s| s.is_namespace()) => {
let mut origin_specifiers = Vec::new();

let mut import_specifiers = Vec::new();
let mut export_specifiers = Vec::new();

for s in specifiers.into_iter() {
match s {
ExportSpecifier::Namespace(ExportNamespaceSpecifier { span, name }) => {
let local_bridge =
private_ident!(format!("_{}", normalize_name(&name)));

import_specifiers.push(ImportSpecifier::Namespace(
ImportStarAsSpecifier {
span,
local: local_bridge.clone(),
},
));
export_specifiers.push(ExportSpecifier::Named(
ExportNamedSpecifier {
span,
orig: local_bridge.into(),
exported: Some(name),
is_type_only: false,
},
))
}
ExportSpecifier::Default(..) | ExportSpecifier::Named(..) => {
origin_specifiers.push(s);
}
}
}

stmts.push(
ImportDecl {
span,
specifiers: import_specifiers,
src: src.clone(),
type_only: false,
with: with.clone(),
phase: Default::default(),
}
.into(),
);

stmts.push(
NamedExport {
span,
specifiers: export_specifiers,
src: None,
type_only: false,
with: None,
}
.into(),
);

if !origin_specifiers.is_empty() {
stmts.push(
NamedExport {
span,
specifiers: origin_specifiers,
src: Some(src),
type_only: false,
with,
}
.into(),
);
}
}
_ => {
stmts.push(item);
}
}
}

*items = stmts;
}
}

fn normalize_name(module_export_name: &ModuleExportName) -> &Atom {
match module_export_name {
ModuleExportName::Ident(Ident { sym: name, .. })
| ModuleExportName::Str(Str { value: name, .. }) => name,
}
Compiler::new(swc_ecma_compiler::Config {
includes: Features::EXPORT_NAMESPACE_FROM,
..Default::default()
})
}
11 changes: 10 additions & 1 deletion crates/swc_ecma_compat_es2020/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::Deserialize;
use swc_common::Mark;
use swc_ecma_ast::Pass;
use swc_ecma_compiler::{Compiler, Features};
use swc_ecma_transforms_base::assumptions::Assumptions;

pub use self::{
export_namespace_from::export_namespace_from, nullish_coalescing::nullish_coalescing,
Expand All @@ -12,10 +14,17 @@ pub mod nullish_coalescing;
pub mod optional_chaining;

pub fn es2020(config: Config, unresolved_mark: Mark) -> impl Pass {
let mut assumptions = Assumptions::default();
assumptions.no_document_all = config.nullish_coalescing.no_document_all;

(
nullish_coalescing(config.nullish_coalescing),
optional_chaining(config.optional_chaining, unresolved_mark),
export_namespace_from(),
Compiler::new(swc_ecma_compiler::Config {
includes: Features::EXPORT_NAMESPACE_FROM,
assumptions,
..Default::default()
}),
)
}

Expand Down
3 changes: 0 additions & 3 deletions crates/swc_ecma_compat_es2020/src/nullish_coalescing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use swc_common::{util::take::Take, Span, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{alias_ident_for_simple_assign_tatget, alias_if_required, StmtLike};
use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith};
use swc_trace_macro::swc_trace;

pub fn nullish_coalescing(c: Config) -> impl Pass + 'static {
visit_mut_pass(NullishCoalescing {
Expand All @@ -27,7 +26,6 @@ pub struct Config {
pub no_document_all: bool,
}

#[swc_trace]
impl NullishCoalescing {
fn visit_mut_stmt_like<T>(&mut self, stmts: &mut Vec<T>)
where
Expand Down Expand Up @@ -58,7 +56,6 @@ impl NullishCoalescing {
}
}

#[swc_trace]
impl VisitMut for NullishCoalescing {
noop_visit_mut_type!(fail);

Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_compat_es2021/src/logical_assignments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ use swc_ecma_compiler::{Compiler, Features};
pub fn logical_assignments() -> impl Pass {
Compiler::new(swc_ecma_compiler::Config {
includes: Features::LOGICAL_ASSIGNMENTS,
excludes: Features::empty(),
..Default::default()
})
}
2 changes: 1 addition & 1 deletion crates/swc_ecma_compat_es2022/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn es2022(config: Config, unresolved_mark: Mark) -> impl Pass {
}),
Compiler::new(swc_ecma_compiler::Config {
includes: Features::STATIC_BLOCKS | Features::PRIVATE_IN_OBJECT,
excludes: Features::empty(),
..Default::default()
}),
class_properties(config.class_properties, unresolved_mark),
)
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_compat_es2022/src/private_in_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ use swc_ecma_compiler::{Compiler, Features};
pub fn private_in_object() -> impl Pass {
Compiler::new(swc_ecma_compiler::Config {
includes: Features::PRIVATE_IN_OBJECT,
excludes: Features::empty(),
..Default::default()
})
}
2 changes: 1 addition & 1 deletion crates/swc_ecma_compat_es2022/src/static_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ use swc_ecma_compiler::{Compiler, Features};
pub fn static_blocks() -> impl Pass {
Compiler::new(swc_ecma_compiler::Config {
includes: Features::STATIC_BLOCKS,
excludes: Features::empty(),
..Default::default()
})
}
1 change: 1 addition & 0 deletions crates/swc_ecma_compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ swc_ecma_ast = { version = "14.0.0", path = "../swc_ecma_ast" }
swc_ecma_utils = { version = "19.0.0", path = "../swc_ecma_utils" }
swc_ecma_visit = { version = "14.0.0", path = "../swc_ecma_visit" }
swc_trace_macro = { version = "2.0.2", path = "../swc_trace_macro" }
swc_ecma_transforms_base = { version = "22.0.1", path = "../swc_ecma_transforms_base" }
119 changes: 119 additions & 0 deletions crates/swc_ecma_compiler/src/es2020/export_namespace_from.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use swc_atoms::Atom;
use swc_ecma_ast::*;
use swc_ecma_utils::private_ident;

use crate::CompilerImpl;

impl<'a> CompilerImpl<'a> {
pub(crate) fn transform_export_namespace_from(&mut self, items: &mut Vec<ModuleItem>) {
let count = items
.iter()
.filter(|m| {
matches!(m, ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(NamedExport {
specifiers,
src: Some(..),
type_only: false,
..
})) if specifiers.iter().any(|s| s.is_namespace()))
})
.count();

if count == 0 {
return;
}

let mut stmts = Vec::<ModuleItem>::with_capacity(items.len() + count);

for item in items.drain(..) {
match item {
ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(NamedExport {
span,
specifiers,
src: Some(src),
type_only: false,
with,
})) if specifiers.iter().any(|s| s.is_namespace()) => {
let mut origin_specifiers = Vec::new();

let mut import_specifiers = Vec::new();
let mut export_specifiers = Vec::new();

for s in specifiers.into_iter() {
match s {
ExportSpecifier::Namespace(ExportNamespaceSpecifier { span, name }) => {
let local_bridge =
private_ident!(format!("_{}", normalize_name(&name)));

import_specifiers.push(ImportSpecifier::Namespace(
ImportStarAsSpecifier {
span,
local: local_bridge.clone(),
},
));
export_specifiers.push(ExportSpecifier::Named(
ExportNamedSpecifier {
span,
orig: local_bridge.into(),
exported: Some(name),
is_type_only: false,
},
))
}
ExportSpecifier::Default(..) | ExportSpecifier::Named(..) => {
origin_specifiers.push(s);
}
}
}

stmts.push(
ImportDecl {
span,
specifiers: import_specifiers,
src: src.clone(),
type_only: false,
with: with.clone(),
phase: Default::default(),
}
.into(),
);

stmts.push(
NamedExport {
span,
specifiers: export_specifiers,
src: None,
type_only: false,
with: None,
}
.into(),
);

if !origin_specifiers.is_empty() {
stmts.push(
NamedExport {
span,
specifiers: origin_specifiers,
src: Some(src),
type_only: false,
with,
}
.into(),
);
}
}
_ => {
stmts.push(item);
}
}
}

*items = stmts;
}
}

fn normalize_name(module_export_name: &ModuleExportName) -> &Atom {
match module_export_name {
ModuleExportName::Ident(Ident { sym: name, .. })
| ModuleExportName::Str(Str { value: name, .. }) => name,
}
}
1 change: 1 addition & 0 deletions crates/swc_ecma_compiler/src/es2020/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) mod export_namespace_from;
Loading
Loading