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

fix(es/typescript): Preserve type assertions #9328

Merged
merged 8 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
77 changes: 39 additions & 38 deletions crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,6 @@ impl Visit for TsStrip {
}
}

fn visit_class_decl(&mut self, n: &ClassDecl) {
if n.declare {
self.add_replacement(n.span());
return;
}

n.visit_children_with(self);
}

fn visit_class(&mut self, n: &Class) {
if n.is_abstract {
let r#abstract = self.get_next_token(n.span_lo());
Expand All @@ -475,6 +466,15 @@ impl Visit for TsStrip {
n.visit_children_with(self);
}

fn visit_class_decl(&mut self, n: &ClassDecl) {
if n.declare {
self.add_replacement(n.span());
return;
}

n.visit_children_with(self);
}

fn visit_class_method(&mut self, n: &ClassMethod) {
if n.function.body.is_none() || n.is_abstract {
self.add_replacement(n.span);
Expand Down Expand Up @@ -552,10 +552,6 @@ impl Visit for TsStrip {
n.visit_children_with(self);
}

fn visit_ts_index_signature(&mut self, n: &TsIndexSignature) {
self.add_replacement(n.span);
}

fn visit_export_all(&mut self, n: &ExportAll) {
if n.type_only {
self.add_replacement(n.span);
Expand Down Expand Up @@ -636,29 +632,6 @@ impl Visit for TsStrip {
}
}

fn visit_ts_import_equals_decl(&mut self, n: &TsImportEqualsDecl) {
if n.is_type_only {
self.add_replacement(n.span);
return;
}

HANDLER.with(|handler| {
handler.span_err(
n.span,
"TypeScript import equals declaration is not supported in strip-only mode",
);
});
}

fn visit_ts_export_assignment(&mut self, n: &TsExportAssignment) {
HANDLER.with(|handler| {
handler.span_err(
n.span,
"TypeScript export assignment is not supported in strip-only mode",
);
});
}

fn visit_params(&mut self, n: &[Param]) {
if let Some(p) = n.first().filter(|param| {
matches!(
Expand Down Expand Up @@ -710,6 +683,33 @@ impl Visit for TsStrip {
});
}

fn visit_ts_export_assignment(&mut self, n: &TsExportAssignment) {
HANDLER.with(|handler| {
handler.span_err(
n.span,
"TypeScript export assignment is not supported in strip-only mode",
);
});
}

fn visit_ts_import_equals_decl(&mut self, n: &TsImportEqualsDecl) {
if n.is_type_only {
self.add_replacement(n.span);
return;
}

HANDLER.with(|handler| {
handler.span_err(
n.span,
"TypeScript import equals declaration is not supported in strip-only mode",
);
});
}

fn visit_ts_index_signature(&mut self, n: &TsIndexSignature) {
self.add_replacement(n.span);
}

fn visit_ts_instantiation(&mut self, n: &TsInstantiation) {
self.add_replacement(span(n.expr.span().hi, n.span.hi));

Expand Down Expand Up @@ -777,9 +777,10 @@ impl Visit for TsStrip {
self.add_replacement(n.span);
}

/// We do not strip type assertion because it's not safe.
///
/// See https://github.com/swc-project/swc/issues/9295
fn visit_ts_type_assertion(&mut self, n: &TsTypeAssertion) {
self.add_replacement(span(n.span.lo, n.expr.span().lo));

kdy1 marked this conversation as resolved.
Show resolved Hide resolved
n.expr.visit_children_with(self);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/swc_fast_ts_strip/tests/fixture/test-case-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let x /**/ /**/ = 1 ;
[] ;
// ^^^^^^^^^^^^^^^^^^

( "test");
(<string>"test");
//^^^^^^^^

class C /**/ /*︎*/ extends Array/**/ /*︎*/ /*︎*/ {
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_fast_ts_strip/tests/fixture/unicode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


function foo() {
   (void 1); throw new Error('foo');
<任意>(void 1); throw new Error('foo');
}

foo();
2 changes: 1 addition & 1 deletion crates/swc_plugin_runner/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use swc_common::{
sync::{Lazy, OnceCell},
};
#[cfg(not(target_arch = "wasm32"))]
use wasmer::{BaseTunables, CpuFeature, Engine, Target, Triple};
use wasmer::{sys::BaseTunables, CpuFeature, Engine, Target, Triple};
use wasmer::{Module, Store};
#[cfg(all(not(target_arch = "wasm32"), feature = "filesystem_cache"))]
use wasmer_cache::{Cache as WasmerCache, FileSystemCache, Hash};
Expand Down
5 changes: 4 additions & 1 deletion crates/swc_plugin_runner/src/wasix_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use wasmer_wasix::Runtime;
static ENGINE: Lazy<Mutex<wasmer::Engine>> = Lazy::new(|| {
// Use empty enumset to disable simd.
use enumset::EnumSet;
use wasmer::{BaseTunables, CompilerConfig, EngineBuilder, Target, Triple};
use wasmer::{
sys::{BaseTunables, EngineBuilder},
CompilerConfig, Target, Triple,
};
let mut set = EnumSet::new();

// [TODO]: Should we use is_x86_feature_detected! macro instead?
Expand Down
Loading