Skip to content

Commit

Permalink
fix(ts/fast-strip): Throw object consistently (#10122)
Browse files Browse the repository at this point in the history
**Description:**

This PR adds an early return to code paths to make them throw an object on error.

**Related issue:**

 - Closes #10087
  • Loading branch information
kdy1 authored Feb 28, 2025
1 parent f019d53 commit 010ff2a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changeset/brave-fishes-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_fast_ts_strip: patch
---

fix(ts/fast-strip): Throw object consistently
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,27 @@ exports[`transform in strip-only mode should throw an error when it encounters a
`;
exports[`transform in transform mode should throw an error when it encounters a declared module 1`] = `
" x \`module\` keyword is not supported. Use \`namespace\` instead.
{
"code": "UnsupportedSyntax",
"message": " x \`module\` keyword is not supported. Use \`namespace\` instead.
,----
1 | declare module foo { }
: ^^^^^^
\`----
"
",
}
`;
exports[`transform in transform mode should throw an error when it encounters a module 1`] = `
" x \`module\` keyword is not supported. Use \`namespace\` instead.
{
"code": "UnsupportedSyntax",
"message": " x \`module\` keyword is not supported. Use \`namespace\` instead.
,----
1 | module foo { }
: ^^^^^^
\`----
"
",
}
`;
exports[`transform should strip types 1`] = `
Expand Down
11 changes: 11 additions & 0 deletions bindings/binding_typescript_wasm/__tests__/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,16 @@ describe("transform", () => {
}),
).rejects.toMatchSnapshot();
});

it('shoud throw an object even with deprecatedTsModuleAsError = true', async () => {
await expect(
swc.transform("module F { export type x = number }", {
mode: "transform",
deprecatedTsModuleAsError: true,
}),
).rejects.toMatchObject({
code: "UnsupportedSyntax",
});
})
});
});
16 changes: 15 additions & 1 deletion crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ pub fn operate(
src: &fm.src,
tokens: &tokens,
});
if handler.has_errors() {
return Err(TsError {
message: "Unsupported syntax".to_string(),
code: ErrorCode::UnsupportedSyntax,
});
}
}

// Strip typescript types
Expand Down Expand Up @@ -365,6 +371,12 @@ pub fn operate(
src: &fm.src,
tokens: &tokens,
});
if handler.has_errors() {
return Err(TsError {
message: "Unsupported syntax".to_string(),
code: ErrorCode::UnsupportedSyntax,
});
}
}

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

program.mutate(&mut fixer(Some(&comments)));
});

Ok(())
})?;

let mut src = std::vec::Vec::new();
let mut src_map_buf = if options.source_map {
Expand Down
7 changes: 0 additions & 7 deletions crates/swc_fast_ts_strip/tests/errors/ts-module.swc-stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,3 @@
8 | declare module Bar { }
: ^^^^^^
`----
x TypeScript namespace declaration is not supported in strip-only mode
,-[3:1]
2 |
3 | ,-> module Foo {
4 | | export const foo = 1;
5 | `-> }
`----

0 comments on commit 010ff2a

Please sign in to comment.