Skip to content

Commit 010ff2a

Browse files
authoredFeb 28, 2025··
fix(ts/fast-strip): Throw object consistently (#10122)
**Description:** This PR adds an early return to code paths to make them throw an object on error. **Related issue:** - Closes #10087
1 parent f019d53 commit 010ff2a

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed
 

‎.changeset/brave-fishes-mate.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_fast_ts_strip: patch
4+
---
5+
6+
fix(ts/fast-strip): Throw object consistently

‎bindings/binding_typescript_wasm/__tests__/__snapshots__/transform.js.snap

+10-4
Original file line numberDiff line numberDiff line change
@@ -166,21 +166,27 @@ exports[`transform in strip-only mode should throw an error when it encounters a
166166
`;
167167
168168
exports[`transform in transform mode should throw an error when it encounters a declared module 1`] = `
169-
" x \`module\` keyword is not supported. Use \`namespace\` instead.
169+
{
170+
"code": "UnsupportedSyntax",
171+
"message": " x \`module\` keyword is not supported. Use \`namespace\` instead.
170172
,----
171173
1 | declare module foo { }
172174
: ^^^^^^
173175
\`----
174-
"
176+
",
177+
}
175178
`;
176179
177180
exports[`transform in transform mode should throw an error when it encounters a module 1`] = `
178-
" x \`module\` keyword is not supported. Use \`namespace\` instead.
181+
{
182+
"code": "UnsupportedSyntax",
183+
"message": " x \`module\` keyword is not supported. Use \`namespace\` instead.
179184
,----
180185
1 | module foo { }
181186
: ^^^^^^
182187
\`----
183-
"
188+
",
189+
}
184190
`;
185191
186192
exports[`transform should strip types 1`] = `

‎bindings/binding_typescript_wasm/__tests__/transform.js

+11
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,16 @@ describe("transform", () => {
189189
}),
190190
).rejects.toMatchSnapshot();
191191
});
192+
193+
it('shoud throw an object even with deprecatedTsModuleAsError = true', async () => {
194+
await expect(
195+
swc.transform("module F { export type x = number }", {
196+
mode: "transform",
197+
deprecatedTsModuleAsError: true,
198+
}),
199+
).rejects.toMatchObject({
200+
code: "UnsupportedSyntax",
201+
});
202+
})
192203
});
193204
});

‎crates/swc_fast_ts_strip/src/lib.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ pub fn operate(
255255
src: &fm.src,
256256
tokens: &tokens,
257257
});
258+
if handler.has_errors() {
259+
return Err(TsError {
260+
message: "Unsupported syntax".to_string(),
261+
code: ErrorCode::UnsupportedSyntax,
262+
});
263+
}
258264
}
259265

260266
// Strip typescript types
@@ -365,6 +371,12 @@ pub fn operate(
365371
src: &fm.src,
366372
tokens: &tokens,
367373
});
374+
if handler.has_errors() {
375+
return Err(TsError {
376+
message: "Unsupported syntax".to_string(),
377+
code: ErrorCode::UnsupportedSyntax,
378+
});
379+
}
368380
}
369381

370382
program.mutate(&mut typescript::typescript(
@@ -378,7 +390,9 @@ pub fn operate(
378390
program.mutate(&mut hygiene());
379391

380392
program.mutate(&mut fixer(Some(&comments)));
381-
});
393+
394+
Ok(())
395+
})?;
382396

383397
let mut src = std::vec::Vec::new();
384398
let mut src_map_buf = if options.source_map {

‎crates/swc_fast_ts_strip/tests/errors/ts-module.swc-stderr

-7
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,3 @@
1818
8 | declare module Bar { }
1919
: ^^^^^^
2020
`----
21-
x TypeScript namespace declaration is not supported in strip-only mode
22-
,-[3:1]
23-
2 |
24-
3 | ,-> module Foo {
25-
4 | | export const foo = 1;
26-
5 | `-> }
27-
`----

0 commit comments

Comments
 (0)
Please sign in to comment.