From d325b17c288b2b1143324bdf5758ec53dd0d6796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 7 Jan 2025 07:41:09 +0900 Subject: [PATCH 01/22] Invalid Syntax --- crates/swc_fast_ts_strip/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index 3ffe10527e61..11daca6ecb9e 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -171,10 +171,10 @@ pub fn operate( let mut program = match program { Ok(program) => program, Err(err) => { - err.into_diagnostic(handler).emit(); + err.into_diagnostic(handler).note("INVALID_SYNTAX").emit(); for e in errors { - e.into_diagnostic(handler).emit(); + e.into_diagnostic(handler).note("INVALID_SYNTAX").emit(); } return Err(anyhow::anyhow!("failed to parse")); @@ -183,7 +183,7 @@ pub fn operate( if !errors.is_empty() { for e in errors { - e.into_diagnostic(handler).emit(); + e.into_diagnostic(handler).note("INVALID_SYNTAX").emit(); } return Err(anyhow::anyhow!("failed to parse")); From 605fe6655e1badf7295479136f56a02239fb033d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 7 Jan 2025 07:50:14 +0900 Subject: [PATCH 02/22] invalid syntax --- crates/swc_fast_ts_strip/src/lib.rs | 83 ++++++++++++++++++----------- 1 file changed, 52 insertions(+), 31 deletions(-) diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index 11daca6ecb9e..3645f6a7d059 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -174,7 +174,7 @@ pub fn operate( err.into_diagnostic(handler).note("INVALID_SYNTAX").emit(); for e in errors { - e.into_diagnostic(handler).note("INVALID_SYNTAX").emit(); + e.into_diagnostic(handler).note("Invalid syntax").emit(); } return Err(anyhow::anyhow!("failed to parse")); @@ -183,7 +183,7 @@ pub fn operate( if !errors.is_empty() { for e in errors { - e.into_diagnostic(handler).note("INVALID_SYNTAX").emit(); + e.into_diagnostic(handler).note("Invalid syntax").emit(); } return Err(anyhow::anyhow!("failed to parse")); @@ -1028,10 +1028,13 @@ 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", - ); + handler + .struct_span_err( + n.span, + "TypeScript export assignment is not supported in strip-only mode", + ) + .note("Unsupported syntax") + .emit(); }); } @@ -1043,10 +1046,13 @@ impl Visit for TsStrip { } HANDLER.with(|handler| { - handler.span_err( - n.span, - "TypeScript import equals declaration is not supported in strip-only mode", - ); + handler + .struct_span_err( + n.span, + "TypeScript import equals declaration is not supported in strip-only mode", + ) + .note("Unsupported syntax") + .emit(); }); } @@ -1062,28 +1068,37 @@ impl Visit for TsStrip { fn visit_ts_enum_decl(&mut self, e: &TsEnumDecl) { HANDLER.with(|handler| { - handler.span_err( - e.span, - "TypeScript enum is not supported in strip-only mode", - ); + handler + .struct_span_err( + e.span, + "TypeScript enum is not supported in strip-only mode", + ) + .note("Unsupported syntax") + .emit(); }); } fn visit_ts_module_decl(&mut self, n: &TsModuleDecl) { HANDLER.with(|handler| { - handler.span_err( - n.span(), - "TypeScript namespace declaration is not supported in strip-only mode", - ); + handler + .struct_span_err( + n.span(), + "TypeScript namespace declaration is not supported in strip-only mode", + ) + .note("Unsupported syntax") + .emit(); }); } fn visit_ts_namespace_decl(&mut self, n: &TsNamespaceDecl) { HANDLER.with(|handler| { - handler.span_err( - n.span(), - "TypeScript module declaration is not supported in strip-only mode", - ); + handler + .struct_span_err( + n.span(), + "TypeScript module declaration is not supported in strip-only mode", + ) + .note("Unsupported syntax") + .emit(); }); } @@ -1095,10 +1110,13 @@ impl Visit for TsStrip { fn visit_ts_param_prop_param(&mut self, n: &TsParamPropParam) { HANDLER.with(|handler| { - handler.span_err( - n.span(), - "TypeScript parameter property is not supported in strip-only mode", - ); + handler + .struct_span_err( + n.span(), + "TypeScript parameter property is not supported in strip-only mode", + ) + .note("Unsupported syntax") + .emit(); }); } @@ -1133,11 +1151,14 @@ impl Visit for TsStrip { /// See https://github.com/swc-project/swc/issues/9295 fn visit_ts_type_assertion(&mut self, n: &TsTypeAssertion) { HANDLER.with(|handler| { - handler.span_err( - n.span, - "The angle-bracket syntax for type assertions, `expr`, is not supported in \ - type strip mode. Instead, use the 'as' syntax: `expr as T`.", - ); + handler + .struct_span_err( + n.span, + "The angle-bracket syntax for type assertions, `expr`, is not supported in \ + type strip mode. Instead, use the 'as' syntax: `expr as T`.", + ) + .note("Unsupported syntax") + .emit(); }); n.expr.visit_children_with(self); From cb369c3a94f5e20f0c290678930beab9b6d149ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 7 Jan 2025 07:51:54 +0900 Subject: [PATCH 03/22] Update test refs --- crates/swc_fast_ts_strip/tests/errors/enums.swc-stderr | 2 ++ crates/swc_fast_ts_strip/tests/errors/modules.swc-stderr | 2 ++ crates/swc_fast_ts_strip/tests/errors/namespaces.swc-stderr | 2 ++ .../swc_fast_ts_strip/tests/fixture/class-properties.swc-stderr | 1 + crates/swc_fast_ts_strip/tests/fixture/test-case-1.swc-stderr | 1 + crates/swc_fast_ts_strip/tests/fixture/unicode.swc-stderr | 1 + 6 files changed, 9 insertions(+) diff --git a/crates/swc_fast_ts_strip/tests/errors/enums.swc-stderr b/crates/swc_fast_ts_strip/tests/errors/enums.swc-stderr index a7bcfdc15e70..6ab53756cc90 100644 --- a/crates/swc_fast_ts_strip/tests/errors/enums.swc-stderr +++ b/crates/swc_fast_ts_strip/tests/errors/enums.swc-stderr @@ -3,9 +3,11 @@ 1 | enum Foo { } : ^^^^^^^^^^^^ `---- + help: Unsupported syntax x TypeScript enum is not supported in strip-only mode ,-[3:1] 2 | 3 | export enum Bar { } : ^^^^^^^^^^^^ `---- + help: Unsupported syntax diff --git a/crates/swc_fast_ts_strip/tests/errors/modules.swc-stderr b/crates/swc_fast_ts_strip/tests/errors/modules.swc-stderr index 6400d96291e3..5a44fa75c6c9 100644 --- a/crates/swc_fast_ts_strip/tests/errors/modules.swc-stderr +++ b/crates/swc_fast_ts_strip/tests/errors/modules.swc-stderr @@ -3,9 +3,11 @@ 1 | module aModuleKeywordNamespace { } : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `---- + help: Unsupported syntax x TypeScript namespace declaration is not supported in strip-only mode ,-[3:1] 2 | 3 | export module aModuleKeywordExportedNamespace { } : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `---- + help: Unsupported syntax diff --git a/crates/swc_fast_ts_strip/tests/errors/namespaces.swc-stderr b/crates/swc_fast_ts_strip/tests/errors/namespaces.swc-stderr index 1de76bce5e24..c15235d0f4a6 100644 --- a/crates/swc_fast_ts_strip/tests/errors/namespaces.swc-stderr +++ b/crates/swc_fast_ts_strip/tests/errors/namespaces.swc-stderr @@ -3,9 +3,11 @@ 1 | namespace Foo { } : ^^^^^^^^^^^^^^^^^ `---- + help: Unsupported syntax x TypeScript namespace declaration is not supported in strip-only mode ,-[3:1] 2 | 3 | export namespace Bar { } : ^^^^^^^^^^^^^^^^^ `---- + help: Unsupported syntax diff --git a/crates/swc_fast_ts_strip/tests/fixture/class-properties.swc-stderr b/crates/swc_fast_ts_strip/tests/fixture/class-properties.swc-stderr index 82c4bf1c5f6c..fc66a1f2128e 100644 --- a/crates/swc_fast_ts_strip/tests/fixture/class-properties.swc-stderr +++ b/crates/swc_fast_ts_strip/tests/fixture/class-properties.swc-stderr @@ -5,3 +5,4 @@ : ^^^^^^^^^^^^^^^^^^ 4 | console.log(3) `---- + help: Unsupported syntax diff --git a/crates/swc_fast_ts_strip/tests/fixture/test-case-1.swc-stderr b/crates/swc_fast_ts_strip/tests/fixture/test-case-1.swc-stderr index a3f700a18cb7..0e07b08f3473 100644 --- a/crates/swc_fast_ts_strip/tests/fixture/test-case-1.swc-stderr +++ b/crates/swc_fast_ts_strip/tests/fixture/test-case-1.swc-stderr @@ -5,3 +5,4 @@ : ^^^^^^^^^^^^^^ 8 | //^^^^^^^^ `---- + help: Unsupported syntax diff --git a/crates/swc_fast_ts_strip/tests/fixture/unicode.swc-stderr b/crates/swc_fast_ts_strip/tests/fixture/unicode.swc-stderr index f57722b500e6..afefa7331df3 100644 --- a/crates/swc_fast_ts_strip/tests/fixture/unicode.swc-stderr +++ b/crates/swc_fast_ts_strip/tests/fixture/unicode.swc-stderr @@ -5,3 +5,4 @@ : ^^^^^^^^^^^^^^ 5 | } `---- + help: Unsupported syntax From f763e001e583c22fa5311885ecb734441034344f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 7 Jan 2025 07:51:59 +0900 Subject: [PATCH 04/22] Use help --- crates/swc_fast_ts_strip/src/lib.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index 3645f6a7d059..68908e0e34c1 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -171,10 +171,10 @@ pub fn operate( let mut program = match program { Ok(program) => program, Err(err) => { - err.into_diagnostic(handler).note("INVALID_SYNTAX").emit(); + err.into_diagnostic(handler).help("INVALID_SYNTAX").emit(); for e in errors { - e.into_diagnostic(handler).note("Invalid syntax").emit(); + e.into_diagnostic(handler).help("Invalid syntax").emit(); } return Err(anyhow::anyhow!("failed to parse")); @@ -183,7 +183,7 @@ pub fn operate( if !errors.is_empty() { for e in errors { - e.into_diagnostic(handler).note("Invalid syntax").emit(); + e.into_diagnostic(handler).help("Invalid syntax").emit(); } return Err(anyhow::anyhow!("failed to parse")); @@ -1033,7 +1033,7 @@ impl Visit for TsStrip { n.span, "TypeScript export assignment is not supported in strip-only mode", ) - .note("Unsupported syntax") + .help("Unsupported syntax") .emit(); }); } @@ -1051,7 +1051,7 @@ impl Visit for TsStrip { n.span, "TypeScript import equals declaration is not supported in strip-only mode", ) - .note("Unsupported syntax") + .help("Unsupported syntax") .emit(); }); } @@ -1073,7 +1073,7 @@ impl Visit for TsStrip { e.span, "TypeScript enum is not supported in strip-only mode", ) - .note("Unsupported syntax") + .help("Unsupported syntax") .emit(); }); } @@ -1085,7 +1085,7 @@ impl Visit for TsStrip { n.span(), "TypeScript namespace declaration is not supported in strip-only mode", ) - .note("Unsupported syntax") + .help("Unsupported syntax") .emit(); }); } @@ -1097,7 +1097,7 @@ impl Visit for TsStrip { n.span(), "TypeScript module declaration is not supported in strip-only mode", ) - .note("Unsupported syntax") + .help("Unsupported syntax") .emit(); }); } @@ -1115,7 +1115,7 @@ impl Visit for TsStrip { n.span(), "TypeScript parameter property is not supported in strip-only mode", ) - .note("Unsupported syntax") + .help("Unsupported syntax") .emit(); }); } @@ -1157,7 +1157,7 @@ impl Visit for TsStrip { "The angle-bracket syntax for type assertions, `expr`, is not supported in \ type strip mode. Instead, use the 'as' syntax: `expr as T`.", ) - .note("Unsupported syntax") + .help("Unsupported syntax") .emit(); }); From 79aaecb915f6bef39f697d217dd9d6de991ae617 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 7 Jan 2025 07:53:38 +0900 Subject: [PATCH 05/22] Update test refs --- .../__tests__/__snapshots__/transform.js.snap | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap b/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap index 23cb962051ca..16522b6686ac 100644 --- a/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap +++ b/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap @@ -1,11 +1,13 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`transform in strip-only mode should not emit 'Caused by: failed to parse' 1`] = ` -" x await isn't allowed in non-async function +" + x await isn't allowed in non-async function ,---- 1 | function foo() { await Promise.resolve(1); } : ^^^^^^^ \`---- + help: Invalid syntax " `; @@ -89,29 +91,35 @@ exports[`transform in strip-only mode should strip type declarations 1`] = ` `; exports[`transform in strip-only mode should throw an error when it encounters a module 1`] = ` -" x TypeScript namespace declaration is not supported in strip-only mode +" + x TypeScript namespace declaration is not supported in strip-only mode ,---- 1 | module foo {} : ^^^^^^^^^^^^^ \`---- + help: Unsupported syntax " `; exports[`transform in strip-only mode should throw an error when it encounters a namespace 1`] = ` -" x TypeScript namespace declaration is not supported in strip-only mode +" + x TypeScript namespace declaration is not supported in strip-only mode ,---- 1 | namespace Foo {} : ^^^^^^^^^^^^^^^^ \`---- + help: Unsupported syntax " `; exports[`transform in strip-only mode should throw an error when it encounters an enum 1`] = ` -" x TypeScript enum is not supported in strip-only mode +" + x TypeScript enum is not supported in strip-only mode ,---- 1 | enum Foo {} : ^^^^^^^^^^^ \`---- + help: Unsupported syntax " `; From 3b1dd2b31c650630820cb325477664c7aca00a21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 8 Jan 2025 15:22:48 +0900 Subject: [PATCH 06/22] TsError --- crates/swc_fast_ts_strip/src/lib.rs | 56 ++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index 68908e0e34c1..91ab818d1827 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -1,6 +1,6 @@ use std::{cell::RefCell, rc::Rc}; -use anyhow::{Context, Error}; +use anyhow::Context; use serde::{Deserialize, Serialize}; use swc_allocator::maybe::vec::Vec; use swc_common::{ @@ -134,12 +134,35 @@ interface TransformOutput { } "#; +#[derive(Debug, Serialize)] +pub struct TsError { + pub message: String, + pub code: ErrorCode, +} + +#[non_exhaustive] +#[derive(Debug, Serialize)] +pub enum ErrorCode { + Invalid, + Unsupported, + Unknown, +} + +impl From for TsError { + fn from(err: anyhow::Error) -> Self { + TsError { + message: err.to_string(), + code: ErrorCode::Unknown, + } + } +} + pub fn operate( cm: &Lrc, handler: &Handler, input: String, options: Options, -) -> Result { +) -> Result { let filename = options .filename .map_or(FileName::Anon, |f| FileName::Real(f.into())); @@ -171,22 +194,28 @@ pub fn operate( let mut program = match program { Ok(program) => program, Err(err) => { - err.into_diagnostic(handler).help("INVALID_SYNTAX").emit(); + err.into_diagnostic(handler).emit(); for e in errors { - e.into_diagnostic(handler).help("Invalid syntax").emit(); + e.into_diagnostic(handler).emit(); } - return Err(anyhow::anyhow!("failed to parse")); + return Err(TsError { + message: "Syntax error".to_string(), + code: ErrorCode::Invalid, + }); } }; if !errors.is_empty() { for e in errors { - e.into_diagnostic(handler).help("Invalid syntax").emit(); + e.into_diagnostic(handler).emit(); } - return Err(anyhow::anyhow!("failed to parse")); + return Err(TsError { + message: "Syntax error".to_string(), + code: ErrorCode::Invalid, + }); } drop(parser); @@ -266,8 +295,10 @@ pub fn operate( } let code = if cfg!(debug_assertions) { - String::from_utf8(code) - .map_err(|_| anyhow::anyhow!("failed to convert to utf-8"))? + String::from_utf8(code).map_err(|err| TsError { + message: format!("failed to convert to utf-8: {}", err), + code: ErrorCode::Unknown, + })? } else { // SAFETY: We've already validated that the source is valid utf-8 // and our operations are limited to character-level string replacements. @@ -1033,7 +1064,6 @@ impl Visit for TsStrip { n.span, "TypeScript export assignment is not supported in strip-only mode", ) - .help("Unsupported syntax") .emit(); }); } @@ -1051,7 +1081,6 @@ impl Visit for TsStrip { n.span, "TypeScript import equals declaration is not supported in strip-only mode", ) - .help("Unsupported syntax") .emit(); }); } @@ -1073,7 +1102,6 @@ impl Visit for TsStrip { e.span, "TypeScript enum is not supported in strip-only mode", ) - .help("Unsupported syntax") .emit(); }); } @@ -1085,7 +1113,6 @@ impl Visit for TsStrip { n.span(), "TypeScript namespace declaration is not supported in strip-only mode", ) - .help("Unsupported syntax") .emit(); }); } @@ -1097,7 +1124,6 @@ impl Visit for TsStrip { n.span(), "TypeScript module declaration is not supported in strip-only mode", ) - .help("Unsupported syntax") .emit(); }); } @@ -1115,7 +1141,6 @@ impl Visit for TsStrip { n.span(), "TypeScript parameter property is not supported in strip-only mode", ) - .help("Unsupported syntax") .emit(); }); } @@ -1157,7 +1182,6 @@ impl Visit for TsStrip { "The angle-bracket syntax for type assertions, `expr`, is not supported in \ type strip mode. Instead, use the 'as' syntax: `expr as T`.", ) - .help("Unsupported syntax") .emit(); }); From 10adcd3a20e1e575da0c912e4780e6e68939991f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 8 Jan 2025 15:50:09 +0900 Subject: [PATCH 07/22] Fix `@swc/minifier` --- bindings/binding_minifier_node/src/minify.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bindings/binding_minifier_node/src/minify.rs b/bindings/binding_minifier_node/src/minify.rs index 7cb2181bbdbe..4bac8d46e51f 100644 --- a/bindings/binding_minifier_node/src/minify.rs +++ b/bindings/binding_minifier_node/src/minify.rs @@ -212,7 +212,11 @@ fn do_work( .clone() .into_inner() .unwrap_or(BoolOr::Data(JsMinifyCommentOption::PreserveSomeComments)); - minify_file_comments(&comments, preserve_comments); + minify_file_comments( + &comments, + preserve_comments, + options.format.preserve_annotations, + ); swc_compiler_base::print( cm.clone(), From 7446c257571c82c8b3bcb77c4f823d89341f2184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 8 Jan 2025 15:58:39 +0900 Subject: [PATCH 08/22] stderror for tserror --- crates/swc_fast_ts_strip/src/lib.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index 91ab818d1827..19d931b29021 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -1,4 +1,4 @@ -use std::{cell::RefCell, rc::Rc}; +use std::{cell::RefCell, fmt::Display, rc::Rc}; use anyhow::Context; use serde::{Deserialize, Serialize}; @@ -140,14 +140,32 @@ pub struct TsError { pub code: ErrorCode, } +impl std::fmt::Display for TsError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "[{}] {}", self.code, self.message) + } +} + +impl std::error::Error for TsError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + None + } +} + #[non_exhaustive] #[derive(Debug, Serialize)] pub enum ErrorCode { - Invalid, - Unsupported, + InvalidSyntax, + UnsupportedSyntax, Unknown, } +impl Display for ErrorCode { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}", self) + } +} + impl From for TsError { fn from(err: anyhow::Error) -> Self { TsError { @@ -202,7 +220,7 @@ pub fn operate( return Err(TsError { message: "Syntax error".to_string(), - code: ErrorCode::Invalid, + code: ErrorCode::InvalidSyntax, }); } }; @@ -214,7 +232,7 @@ pub fn operate( return Err(TsError { message: "Syntax error".to_string(), - code: ErrorCode::Invalid, + code: ErrorCode::InvalidSyntax, }); } From c4f595fe3898c48494304b3f45de555fdec59b63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 8 Jan 2025 16:02:28 +0900 Subject: [PATCH 09/22] WIP --- bindings/binding_typescript_wasm/src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/bindings/binding_typescript_wasm/src/lib.rs b/bindings/binding_typescript_wasm/src/lib.rs index 13c751adc6e3..e20f95f5058a 100644 --- a/bindings/binding_typescript_wasm/src/lib.rs +++ b/bindings/binding_typescript_wasm/src/lib.rs @@ -1,7 +1,7 @@ use anyhow::Error; use swc_common::{errors::ColorConfig, sync::Lrc, SourceMap, GLOBALS}; use swc_error_reporters::handler::{try_with_handler, HandlerOpts}; -use swc_fast_ts_strip::{Options, TransformOutput}; +use swc_fast_ts_strip::{Options, TransformOutput, TsError}; use wasm_bindgen::prelude::*; use wasm_bindgen_futures::{ future_to_promise, @@ -48,10 +48,16 @@ fn operate(input: String, options: Options) -> Result { color: ColorConfig::Never, skip_filename: true, }, - |handler| swc_fast_ts_strip::operate(&cm, handler, input, options), + |handler| { + swc_fast_ts_strip::operate(&cm, handler, input, options).map_err(anyhow::Error::new) + }, ) } pub fn convert_err(err: Error) -> wasm_bindgen::prelude::JsValue { + if let Some(ts_error) = err.downcast_ref::() { + return Err(anyhow::Error::new(ts_error)); + } + format!("{}", err).into() } From 153f08e3e2fb45e7d6687a597410cf368b01fa75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 8 Jan 2025 16:06:29 +0900 Subject: [PATCH 10/22] CLone copy --- crates/swc_fast_ts_strip/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index 19d931b29021..1841e50d75f3 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -153,7 +153,7 @@ impl std::error::Error for TsError { } #[non_exhaustive] -#[derive(Debug, Serialize)] +#[derive(Debug, Clone, Copy, Serialize)] pub enum ErrorCode { InvalidSyntax, UnsupportedSyntax, From fe74453627f0fe4b9d423603b608752d0ab41431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 8 Jan 2025 16:07:43 +0900 Subject: [PATCH 11/22] Error object --- bindings/binding_typescript_wasm/src/lib.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/bindings/binding_typescript_wasm/src/lib.rs b/bindings/binding_typescript_wasm/src/lib.rs index e20f95f5058a..3eb1be2e1033 100644 --- a/bindings/binding_typescript_wasm/src/lib.rs +++ b/bindings/binding_typescript_wasm/src/lib.rs @@ -1,7 +1,8 @@ use anyhow::Error; +use serde::Serialize; use swc_common::{errors::ColorConfig, sync::Lrc, SourceMap, GLOBALS}; use swc_error_reporters::handler::{try_with_handler, HandlerOpts}; -use swc_fast_ts_strip::{Options, TransformOutput, TsError}; +use swc_fast_ts_strip::{ErrorCode, Options, TransformOutput, TsError}; use wasm_bindgen::prelude::*; use wasm_bindgen_futures::{ future_to_promise, @@ -54,9 +55,19 @@ fn operate(input: String, options: Options) -> Result { ) } +#[derive(Debug, Serialize)] +struct ErrorObject { + code: ErrorCode, + message: String, +} + pub fn convert_err(err: Error) -> wasm_bindgen::prelude::JsValue { if let Some(ts_error) = err.downcast_ref::() { - return Err(anyhow::Error::new(ts_error)); + return serde_wasm_bindgen::to_value(&ErrorObject { + code: ts_error.code, + message: err.to_string(), + }) + .unwrap(); } format!("{}", err).into() From a3245da0ae85af4a6c57c0b5e3fa682776563166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 8 Jan 2025 16:07:54 +0900 Subject: [PATCH 12/22] code --- .../__tests__/__snapshots__/transform.js.snap | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap b/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap index 16522b6686ac..334808868f18 100644 --- a/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap +++ b/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap @@ -1,14 +1,16 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`transform in strip-only mode should not emit 'Caused by: failed to parse' 1`] = ` -" +{ + "code": "InvalidSyntax", + "message": " x await isn't allowed in non-async function ,---- 1 | function foo() { await Promise.resolve(1); } : ^^^^^^^ \`---- - help: Invalid syntax -" +", +} `; exports[`transform in strip-only mode should remove declare enum 1`] = ` @@ -97,7 +99,6 @@ exports[`transform in strip-only mode should throw an error when it encounters a 1 | module foo {} : ^^^^^^^^^^^^^ \`---- - help: Unsupported syntax " `; @@ -108,7 +109,6 @@ exports[`transform in strip-only mode should throw an error when it encounters a 1 | namespace Foo {} : ^^^^^^^^^^^^^^^^ \`---- - help: Unsupported syntax " `; @@ -119,7 +119,6 @@ exports[`transform in strip-only mode should throw an error when it encounters a 1 | enum Foo {} : ^^^^^^^^^^^ \`---- - help: Unsupported syntax " `; From 49e6b64259018ab9b355359d7ab29acdf9cb166a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 8 Jan 2025 17:36:42 +0900 Subject: [PATCH 13/22] ErrorCode::UnsupportedSyntax --- crates/swc_fast_ts_strip/src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index 1841e50d75f3..7c2f37aa3e50 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -247,6 +247,12 @@ pub fn operate( // Strip typescript types let mut ts_strip = TsStrip::new(fm.src.clone(), tokens); program.visit_with(&mut ts_strip); + if handler.has_errors() { + return Err(TsError { + message: "Unsupported syntax".to_string(), + code: ErrorCode::UnsupportedSyntax, + }); + } let replacements = ts_strip.replacements; let overwrites = ts_strip.overwrites; From f035557e0dec8650fc979d5cc2d65bd849d33df9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 8 Jan 2025 17:42:22 +0900 Subject: [PATCH 14/22] Add test --- .../__tests__/transform.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/bindings/binding_typescript_wasm/__tests__/transform.js b/bindings/binding_typescript_wasm/__tests__/transform.js index 8a1e491db566..dbaed85e381c 100644 --- a/bindings/binding_typescript_wasm/__tests__/transform.js +++ b/bindings/binding_typescript_wasm/__tests__/transform.js @@ -138,5 +138,24 @@ describe("transform", () => { }) ).rejects.toMatchSnapshot(); }); + + + it("should report correct error for syntax error", async () => { + await expect( + swc.transform("function foo() { invalid syntax }", { + mode: "strip-only", + }) + ).rejects.toMatchSnapshot(); + }); + + it("should report correct error for unsupported syntax", async () => { + await expect( + swc.transform(`enum Foo { + a, b + }`, { + mode: "strip-only", + }) + ).rejects.toMatchSnapshot(); + }); }); }); From c9163ffde737bb8b08500ef54eadf5fb6d297bd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 8 Jan 2025 17:43:37 +0900 Subject: [PATCH 15/22] Update test refs --- .../__tests__/__snapshots__/transform.js.snap | 53 +++++++++++++++---- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap b/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap index 334808868f18..e6903c8993c9 100644 --- a/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap +++ b/bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap @@ -3,8 +3,7 @@ exports[`transform in strip-only mode should not emit 'Caused by: failed to parse' 1`] = ` { "code": "InvalidSyntax", - "message": " - x await isn't allowed in non-async function + "message": " x await isn't allowed in non-async function ,---- 1 | function foo() { await Promise.resolve(1); } : ^^^^^^^ @@ -39,6 +38,32 @@ exports[`transform in strip-only mode should remove declare enum 3`] = ` } `; +exports[`transform in strip-only mode should report correct error for syntax error 1`] = ` +{ + "code": "InvalidSyntax", + "message": " x Expected ';', '}' or + ,---- + 1 | function foo() { invalid syntax } + : ^^^|^^^ ^^^^^^ + : \`-- This is the expression part of an expression statement + \`---- +", +} +`; + +exports[`transform in strip-only mode should report correct error for unsupported syntax 1`] = ` +{ + "code": "UnsupportedSyntax", + "message": " x TypeScript enum is not supported in strip-only mode + ,-[1:1] + 1 | ,-> enum Foo { + 2 | | a, b + 3 | \`-> } + \`---- +", +} +`; + exports[`transform in strip-only mode should strip complex expressions 1`] = ` { "code": "const foo = { @@ -93,33 +118,39 @@ exports[`transform in strip-only mode should strip type declarations 1`] = ` `; exports[`transform in strip-only mode should throw an error when it encounters a module 1`] = ` -" - x TypeScript namespace declaration is not supported in strip-only mode +{ + "code": "UnsupportedSyntax", + "message": " x TypeScript namespace declaration is not supported in strip-only mode ,---- 1 | module foo {} : ^^^^^^^^^^^^^ \`---- -" +", +} `; exports[`transform in strip-only mode should throw an error when it encounters a namespace 1`] = ` -" - x TypeScript namespace declaration is not supported in strip-only mode +{ + "code": "UnsupportedSyntax", + "message": " x TypeScript namespace declaration is not supported in strip-only mode ,---- 1 | namespace Foo {} : ^^^^^^^^^^^^^^^^ \`---- -" +", +} `; exports[`transform in strip-only mode should throw an error when it encounters an enum 1`] = ` -" - x TypeScript enum is not supported in strip-only mode +{ + "code": "UnsupportedSyntax", + "message": " x TypeScript enum is not supported in strip-only mode ,---- 1 | enum Foo {} : ^^^^^^^^^^^ \`---- -" +", +} `; exports[`transform should strip types 1`] = ` From 4254a654548e750fd6dc4bcd2aea3b0740ffa8f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 9 Jan 2025 20:40:27 +0900 Subject: [PATCH 16/22] Add tests for error reporting --- .../swc_fast_ts_strip/tests/errors/export-equals.ts | 2 ++ .../swc_fast_ts_strip/tests/errors/import-equals.ts | 1 + .../tests/errors/parameter-property.ts | 11 +++++++++++ 3 files changed, 14 insertions(+) create mode 100644 crates/swc_fast_ts_strip/tests/errors/export-equals.ts create mode 100644 crates/swc_fast_ts_strip/tests/errors/import-equals.ts create mode 100644 crates/swc_fast_ts_strip/tests/errors/parameter-property.ts diff --git a/crates/swc_fast_ts_strip/tests/errors/export-equals.ts b/crates/swc_fast_ts_strip/tests/errors/export-equals.ts new file mode 100644 index 000000000000..2f4528443607 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/errors/export-equals.ts @@ -0,0 +1,2 @@ + +export = 1; diff --git a/crates/swc_fast_ts_strip/tests/errors/import-equals.ts b/crates/swc_fast_ts_strip/tests/errors/import-equals.ts new file mode 100644 index 000000000000..11fb569cd7c2 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/errors/import-equals.ts @@ -0,0 +1 @@ +import foo = require("foo"); \ No newline at end of file diff --git a/crates/swc_fast_ts_strip/tests/errors/parameter-property.ts b/crates/swc_fast_ts_strip/tests/errors/parameter-property.ts new file mode 100644 index 000000000000..1ab53dc0f75b --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/errors/parameter-property.ts @@ -0,0 +1,11 @@ + + + +class Foo { + constructor(public id: string) { } +} + + +class Bar { + constructor(private id: string) { } +} \ No newline at end of file From d5357626c87f4366fbc59f44baae757c23c68434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 9 Jan 2025 20:41:37 +0900 Subject: [PATCH 17/22] fix fixture testing --- crates/swc_fast_ts_strip/tests/fixture.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_fast_ts_strip/tests/fixture.rs b/crates/swc_fast_ts_strip/tests/fixture.rs index 9f537e35c98e..ea8722fe8a0c 100644 --- a/crates/swc_fast_ts_strip/tests/fixture.rs +++ b/crates/swc_fast_ts_strip/tests/fixture.rs @@ -102,7 +102,7 @@ fn error(input: PathBuf) { let output_file = input.with_extension("swc-stderr"); testing::run_test(false, |cm, handler| { - operate(&cm, handler, input_code, opts(Mode::StripOnly)).expect("should not return Err()"); + operate(&cm, handler, input_code, opts(Mode::StripOnly)).expect_err("should return Err()"); Err::<(), _>(()) }) From fbc93330e8e2d7774f8435d915a9b8f1ef627eda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 9 Jan 2025 20:43:14 +0900 Subject: [PATCH 18/22] Add tests --- crates/swc_fast_ts_strip/tests/errors/ts-module.ts | 5 +++++ crates/swc_fast_ts_strip/tests/errors/ts-namespace.ts | 5 +++++ 2 files changed, 10 insertions(+) create mode 100644 crates/swc_fast_ts_strip/tests/errors/ts-module.ts create mode 100644 crates/swc_fast_ts_strip/tests/errors/ts-namespace.ts diff --git a/crates/swc_fast_ts_strip/tests/errors/ts-module.ts b/crates/swc_fast_ts_strip/tests/errors/ts-module.ts new file mode 100644 index 000000000000..43e1c67d88e4 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/errors/ts-module.ts @@ -0,0 +1,5 @@ + + +module Foo { + export const foo = 1; +} \ No newline at end of file diff --git a/crates/swc_fast_ts_strip/tests/errors/ts-namespace.ts b/crates/swc_fast_ts_strip/tests/errors/ts-namespace.ts new file mode 100644 index 000000000000..c55f6f9776c8 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/errors/ts-namespace.ts @@ -0,0 +1,5 @@ + + +namespace Foo { + export const foo = 1; +} \ No newline at end of file From 2eef3b9d9ca9634fb761df89a4b5b449d627b51c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 9 Jan 2025 20:43:19 +0900 Subject: [PATCH 19/22] Update test refs --- .../tests/errors/enums.swc-stderr | 2 -- .../tests/errors/export-equals.swc-stderr | 6 ++++++ .../tests/errors/import-equals.swc-stderr | 5 +++++ .../tests/errors/modules.swc-stderr | 2 -- .../tests/errors/namespaces.swc-stderr | 2 -- .../tests/errors/parameter-property.swc-stderr | 14 ++++++++++++++ .../tests/errors/ts-module.swc-stderr | 7 +++++++ .../tests/errors/ts-namespace.swc-stderr | 7 +++++++ 8 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 crates/swc_fast_ts_strip/tests/errors/export-equals.swc-stderr create mode 100644 crates/swc_fast_ts_strip/tests/errors/import-equals.swc-stderr create mode 100644 crates/swc_fast_ts_strip/tests/errors/parameter-property.swc-stderr create mode 100644 crates/swc_fast_ts_strip/tests/errors/ts-module.swc-stderr create mode 100644 crates/swc_fast_ts_strip/tests/errors/ts-namespace.swc-stderr diff --git a/crates/swc_fast_ts_strip/tests/errors/enums.swc-stderr b/crates/swc_fast_ts_strip/tests/errors/enums.swc-stderr index 6ab53756cc90..a7bcfdc15e70 100644 --- a/crates/swc_fast_ts_strip/tests/errors/enums.swc-stderr +++ b/crates/swc_fast_ts_strip/tests/errors/enums.swc-stderr @@ -3,11 +3,9 @@ 1 | enum Foo { } : ^^^^^^^^^^^^ `---- - help: Unsupported syntax x TypeScript enum is not supported in strip-only mode ,-[3:1] 2 | 3 | export enum Bar { } : ^^^^^^^^^^^^ `---- - help: Unsupported syntax diff --git a/crates/swc_fast_ts_strip/tests/errors/export-equals.swc-stderr b/crates/swc_fast_ts_strip/tests/errors/export-equals.swc-stderr new file mode 100644 index 000000000000..4f7863b5a20d --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/errors/export-equals.swc-stderr @@ -0,0 +1,6 @@ + x TypeScript export assignment is not supported in strip-only mode + ,-[2:1] + 1 | + 2 | export = 1; + : ^^^^^^^^^^^ + `---- diff --git a/crates/swc_fast_ts_strip/tests/errors/import-equals.swc-stderr b/crates/swc_fast_ts_strip/tests/errors/import-equals.swc-stderr new file mode 100644 index 000000000000..f960b56486e5 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/errors/import-equals.swc-stderr @@ -0,0 +1,5 @@ + x TypeScript import equals declaration is not supported in strip-only mode + ,---- + 1 | import foo = require("foo"); + : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + `---- diff --git a/crates/swc_fast_ts_strip/tests/errors/modules.swc-stderr b/crates/swc_fast_ts_strip/tests/errors/modules.swc-stderr index 5a44fa75c6c9..6400d96291e3 100644 --- a/crates/swc_fast_ts_strip/tests/errors/modules.swc-stderr +++ b/crates/swc_fast_ts_strip/tests/errors/modules.swc-stderr @@ -3,11 +3,9 @@ 1 | module aModuleKeywordNamespace { } : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `---- - help: Unsupported syntax x TypeScript namespace declaration is not supported in strip-only mode ,-[3:1] 2 | 3 | export module aModuleKeywordExportedNamespace { } : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `---- - help: Unsupported syntax diff --git a/crates/swc_fast_ts_strip/tests/errors/namespaces.swc-stderr b/crates/swc_fast_ts_strip/tests/errors/namespaces.swc-stderr index c15235d0f4a6..1de76bce5e24 100644 --- a/crates/swc_fast_ts_strip/tests/errors/namespaces.swc-stderr +++ b/crates/swc_fast_ts_strip/tests/errors/namespaces.swc-stderr @@ -3,11 +3,9 @@ 1 | namespace Foo { } : ^^^^^^^^^^^^^^^^^ `---- - help: Unsupported syntax x TypeScript namespace declaration is not supported in strip-only mode ,-[3:1] 2 | 3 | export namespace Bar { } : ^^^^^^^^^^^^^^^^^ `---- - help: Unsupported syntax diff --git a/crates/swc_fast_ts_strip/tests/errors/parameter-property.swc-stderr b/crates/swc_fast_ts_strip/tests/errors/parameter-property.swc-stderr new file mode 100644 index 000000000000..82f78c87780b --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/errors/parameter-property.swc-stderr @@ -0,0 +1,14 @@ + x TypeScript parameter property is not supported in strip-only mode + ,-[5:1] + 4 | class Foo { + 5 | constructor(public id: string) { } + : ^^^^^^^^^^ + 6 | } + `---- + x TypeScript parameter property is not supported in strip-only mode + ,-[10:1] + 9 | class Bar { + 10 | constructor(private id: string) { } + : ^^^^^^^^^^ + 11 | } + `---- diff --git a/crates/swc_fast_ts_strip/tests/errors/ts-module.swc-stderr b/crates/swc_fast_ts_strip/tests/errors/ts-module.swc-stderr new file mode 100644 index 000000000000..a9f37bc16ae1 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/errors/ts-module.swc-stderr @@ -0,0 +1,7 @@ + x TypeScript namespace declaration is not supported in strip-only mode + ,-[3:1] + 2 | + 3 | ,-> module Foo { + 4 | | export const foo = 1; + 5 | `-> } + `---- diff --git a/crates/swc_fast_ts_strip/tests/errors/ts-namespace.swc-stderr b/crates/swc_fast_ts_strip/tests/errors/ts-namespace.swc-stderr new file mode 100644 index 000000000000..2774b4dfa8e6 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/errors/ts-namespace.swc-stderr @@ -0,0 +1,7 @@ + x TypeScript namespace declaration is not supported in strip-only mode + ,-[3:1] + 2 | + 3 | ,-> namespace Foo { + 4 | | export const foo = 1; + 5 | `-> } + `---- From 2b65aae3da4691d60c8a5eb7a3753a6c640af37f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 9 Jan 2025 20:44:25 +0900 Subject: [PATCH 20/22] Update test refs --- .../tests/errors/type-assertions.swc-stderr | 6 ++++++ crates/swc_fast_ts_strip/tests/errors/type-assertions.ts | 3 +++ 2 files changed, 9 insertions(+) create mode 100644 crates/swc_fast_ts_strip/tests/errors/type-assertions.swc-stderr create mode 100644 crates/swc_fast_ts_strip/tests/errors/type-assertions.ts diff --git a/crates/swc_fast_ts_strip/tests/errors/type-assertions.swc-stderr b/crates/swc_fast_ts_strip/tests/errors/type-assertions.swc-stderr new file mode 100644 index 000000000000..d4b0c89a1d57 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/errors/type-assertions.swc-stderr @@ -0,0 +1,6 @@ + x The angle-bracket syntax for type assertions, `expr`, is not supported in type strip mode. Instead, use the 'as' syntax: `expr as T`. + ,-[3:1] + 2 | + 3 | const foo = 1; + : ^^^^^^^^^ + `---- diff --git a/crates/swc_fast_ts_strip/tests/errors/type-assertions.ts b/crates/swc_fast_ts_strip/tests/errors/type-assertions.ts new file mode 100644 index 000000000000..ad9af693cab6 --- /dev/null +++ b/crates/swc_fast_ts_strip/tests/errors/type-assertions.ts @@ -0,0 +1,3 @@ + + +const foo = 1; \ No newline at end of file From c677abf8c8180d8bbbccee508352d284a5e98f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Fri, 10 Jan 2025 11:33:54 +0900 Subject: [PATCH 21/22] fix --- crates/swc_fast_ts_strip/benches/assets.rs | 5 +- crates/swc_fast_ts_strip/tests/fixture.rs | 12 +- .../tests/fixture/class-properties.js | 6 - .../tests/fixture/class-properties.swc-stderr | 1 - .../tests/fixture/test-case-1.js | 198 ------------------ .../tests/fixture/test-case-1.swc-stderr | 1 - .../tests/fixture/unicode.js | 7 - .../tests/fixture/unicode.swc-stderr | 1 - 8 files changed, 8 insertions(+), 223 deletions(-) delete mode 100644 crates/swc_fast_ts_strip/tests/fixture/class-properties.js delete mode 100644 crates/swc_fast_ts_strip/tests/fixture/test-case-1.js delete mode 100644 crates/swc_fast_ts_strip/tests/fixture/unicode.js diff --git a/crates/swc_fast_ts_strip/benches/assets.rs b/crates/swc_fast_ts_strip/benches/assets.rs index 789e91b0856f..45f0e1d12e9e 100644 --- a/crates/swc_fast_ts_strip/benches/assets.rs +++ b/crates/swc_fast_ts_strip/benches/assets.rs @@ -9,15 +9,14 @@ fn fast_ts(c: &mut Criterion) { fn fast_typescript(b: &mut Bencher) { b.iter(|| { ::testing::run_test(false, |cm, handler| { - black_box(operate( + let _result = black_box(operate( &cm, handler, black_box(SOURCE.to_string()), Options { ..Default::default() }, - )) - .unwrap(); + )); Ok(()) }) diff --git a/crates/swc_fast_ts_strip/tests/fixture.rs b/crates/swc_fast_ts_strip/tests/fixture.rs index ea8722fe8a0c..0fc384a7794d 100644 --- a/crates/swc_fast_ts_strip/tests/fixture.rs +++ b/crates/swc_fast_ts_strip/tests/fixture.rs @@ -16,13 +16,13 @@ fn test(input: PathBuf) { let transform_output_file = input.with_extension("transform.js"); let err = testing::run_test(false, |cm, handler| { - let code = operate(&cm, handler, input_code.clone(), opts(Mode::StripOnly)) - .expect("should not return Err()") - .code; + if let Ok(code) = operate(&cm, handler, input_code.clone(), opts(Mode::StripOnly)) { + let code = code.code; - NormalizedOutput::new_raw(code) - .compare_to_file(output_file) - .unwrap(); + NormalizedOutput::new_raw(code) + .compare_to_file(output_file) + .unwrap(); + } if handler.has_errors() { return Err(()); diff --git a/crates/swc_fast_ts_strip/tests/fixture/class-properties.js b/crates/swc_fast_ts_strip/tests/fixture/class-properties.js deleted file mode 100644 index 3713bda1fc69..000000000000 --- a/crates/swc_fast_ts_strip/tests/fixture/class-properties.js +++ /dev/null @@ -1,6 +0,0 @@ -class Foo { - x = console.log(1) - constructor(public y = console.log(2)) { - console.log(3) - } -} \ No newline at end of file diff --git a/crates/swc_fast_ts_strip/tests/fixture/class-properties.swc-stderr b/crates/swc_fast_ts_strip/tests/fixture/class-properties.swc-stderr index fc66a1f2128e..82c4bf1c5f6c 100644 --- a/crates/swc_fast_ts_strip/tests/fixture/class-properties.swc-stderr +++ b/crates/swc_fast_ts_strip/tests/fixture/class-properties.swc-stderr @@ -5,4 +5,3 @@ : ^^^^^^^^^^^^^^^^^^ 4 | console.log(3) `---- - help: Unsupported syntax diff --git a/crates/swc_fast_ts_strip/tests/fixture/test-case-1.js b/crates/swc_fast_ts_strip/tests/fixture/test-case-1.js deleted file mode 100644 index 62454e9ef522..000000000000 --- a/crates/swc_fast_ts_strip/tests/fixture/test-case-1.js +++ /dev/null @@ -1,198 +0,0 @@ -let x /**/ /**/ = 1 ; -// ^^^^^^^^ ^ - -[] ; -// ^^^^^^^^^^^^^^^^^^ - -("test"); -//^^^^^^^^ - -class C /**/ /*︎*/ extends Array/**/ /*︎*/ /*︎*/ { - // ^^^^^ ^^^ ^^^^^^^^^^^^^^ - field/**/ /**/ = ""; - // ^^^^^^^^ ^^^^^^^^ - static accessor f1; - f2/**/ /**/ /*︎*/; - // ^^^^^^^ ^ ^^^^^^^^ - - // ^^^^^^^^^^^^^^^^ declared property - - method/**/ /*︎*/(/*︎*/ /**/ a /*︎*/ /**/)/*︎*/ /*︎*/ { - // ^^^^^^ ^^^ ^^^^^^^^ ^ ^^^^^^^^ ^^^^^^ - } - - - // ^^^^^^^^^^^^^^^^^^^ index signature - - get g() { return 1 }; - // ^^^^^ - set g(v ) { }; - // ^^^^^ -} - -class D extends C { - // ^^^^^ - method(...args) { } - // ^^^^^^^^ ^^^^^ -} - - class A { - // ^^^^^^^^ - - // ^^^^^^^^^^^ abstract property - b; - - // ^^^^^^^^^^^^^^^^^^ abstract method -} - -{ - let m = new (Map ) ([] ); - // ^ ^^^^^^^^^^^^^^^^ ^ -} - -{ - let a = (foo ) ; - // ^ ^^^^^ -} - -{ - let a = (foo ) ([] ); - // ^ ^^^^^ ^ -} - -{ - let f = function (p ) { } - // ^^^^^ -} - -{ - - // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overload - function overload() { } - // ^^^^^ -} - -/** @doc */ - -// ^^^^^^^^^^^ interface - -void 0; - -/** @doc */ - -// ^^^^^^^^ type alias - -/**/ -// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `import type` - -/**/ -// ^^^^^^^^^^^^^^^^^^ `export type` - -/**/ -// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `export type *` - -import { deepEqual } from "node:assert"; -// ^^^^^^^^^^^^^^^^^^^^^^^^^ - -export { - C, - - // ^^^^^^ -} - -/**/ -// ^^^^^^^^^^^^^^^^^^^ - -function foo (p = () => 1) { - // ^^^ ^^^^^ ^^^^^ ^^^^^ - return p ; - // ^^^^^^ -} - -/**/ -// ^^^^^^^^^^^^^^^^^^ `declare enum` - -void 0; - -/**/ -// ^^^^^^^^^^^^^^^^^^^^^^ `declare namespace` - -void 0; - -/**/ -// ^^^^^^^^^^^^^^^^^^^ `declare module` - -void 0; - -/**/ -// ^^^^^^^^^^^^^^ `declare let` - -void 0; - -/**/ -// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `declare class` - -void 0; - -/**/ -// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `declare function` - -void 0; - -// `=>` spanning line cases: -{ - ( - ) => - 1 -}; -{ - ( - ) => - 1 -}; -{ - ( - - ) => - 1 -}; -{ - ( - - - ) => - 1 -}; -{ - ( - - - ) => - 1 -}; -{ - (a, b, c = [] /*comment-1*/ /*comment-2*/ - ) => - 1 -}; - - - ( - )=> - 1; - -{ - (a, b, c = [] /*comment-1*/ /*comment-2*/ - )/*comment-4*/=> - 1 -}; - -      - -( -   ) => -1; - -( /*comment-1*/ -    ) /*comment-4*/=> -1; \ No newline at end of file diff --git a/crates/swc_fast_ts_strip/tests/fixture/test-case-1.swc-stderr b/crates/swc_fast_ts_strip/tests/fixture/test-case-1.swc-stderr index 0e07b08f3473..a3f700a18cb7 100644 --- a/crates/swc_fast_ts_strip/tests/fixture/test-case-1.swc-stderr +++ b/crates/swc_fast_ts_strip/tests/fixture/test-case-1.swc-stderr @@ -5,4 +5,3 @@ : ^^^^^^^^^^^^^^ 8 | //^^^^^^^^ `---- - help: Unsupported syntax diff --git a/crates/swc_fast_ts_strip/tests/fixture/unicode.js b/crates/swc_fast_ts_strip/tests/fixture/unicode.js deleted file mode 100644 index 62378ffc84dd..000000000000 --- a/crates/swc_fast_ts_strip/tests/fixture/unicode.js +++ /dev/null @@ -1,7 +0,0 @@ -    - -function foo() { - <任意>(void 1); throw new Error('foo'); -} - -foo(); \ No newline at end of file diff --git a/crates/swc_fast_ts_strip/tests/fixture/unicode.swc-stderr b/crates/swc_fast_ts_strip/tests/fixture/unicode.swc-stderr index afefa7331df3..f57722b500e6 100644 --- a/crates/swc_fast_ts_strip/tests/fixture/unicode.swc-stderr +++ b/crates/swc_fast_ts_strip/tests/fixture/unicode.swc-stderr @@ -5,4 +5,3 @@ : ^^^^^^^^^^^^^^ 5 | } `---- - help: Unsupported syntax From 050b9aead9163bf80bb1146a72ed4bafb1314c9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 10 Jan 2025 11:35:13 +0900 Subject: [PATCH 22/22] Create metal-penguins-travel.md --- .changeset/metal-penguins-travel.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/metal-penguins-travel.md diff --git a/.changeset/metal-penguins-travel.md b/.changeset/metal-penguins-travel.md new file mode 100644 index 000000000000..00c9f45b9a6a --- /dev/null +++ b/.changeset/metal-penguins-travel.md @@ -0,0 +1,6 @@ +--- +swc_core: minor +swc_fast_ts_strip: minor +--- + +feat(ts/fast-strip): Distinguish invalid vs unsupported