Skip to content

Commit 2da0142

Browse files
authored
feat(ts/fast-strip): Throw js object instead of map (#10186)
**Related issue:** - Closes #10180
1 parent cd777bf commit 2da0142

File tree

2 files changed

+89
-92
lines changed

2 files changed

+89
-92
lines changed

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

+70-70
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`transform in strip-only mode should not emit 'Caused by: failed to parse' 1`] = `
4-
Map {
5-
"code" => "InvalidSyntax",
6-
"column" => 23,
7-
"filename" => "test.ts",
8-
"line" => 1,
9-
"message" => "await isn't allowed in non-async function",
10-
"snippet" => "Promise",
4+
{
5+
"code": "InvalidSyntax",
6+
"column": 23,
7+
"filename": "test.ts",
8+
"line": 1,
9+
"message": "await isn't allowed in non-async function",
10+
"snippet": "Promise",
1111
}
1212
`;
1313

@@ -38,24 +38,24 @@ exports[`transform in strip-only mode should remove declare enum 3`] = `
3838
`;
3939

4040
exports[`transform in strip-only mode should report correct error for syntax error 1`] = `
41-
Map {
42-
"code" => "InvalidSyntax",
43-
"column" => 25,
44-
"filename" => "test.ts",
45-
"line" => 1,
46-
"message" => "Expected ';', '}' or <eof>",
47-
"snippet" => "syntax",
41+
{
42+
"code": "InvalidSyntax",
43+
"column": 25,
44+
"filename": "test.ts",
45+
"line": 1,
46+
"message": "Expected ';', '}' or <eof>",
47+
"snippet": "syntax",
4848
}
4949
`;
5050

5151
exports[`transform in strip-only mode should report correct error for unsupported syntax 1`] = `
52-
Map {
53-
"code" => "UnsupportedSyntax",
54-
"column" => 0,
55-
"filename" => "test.ts",
56-
"line" => 1,
57-
"message" => "TypeScript enum is not supported in strip-only mode",
58-
"snippet" => "enum Foo {
52+
{
53+
"code": "UnsupportedSyntax",
54+
"column": 0,
55+
"filename": "test.ts",
56+
"line": 1,
57+
"message": "TypeScript enum is not supported in strip-only mode",
58+
"snippet": "enum Foo {
5959
a, b
6060
}",
6161
}
@@ -115,79 +115,79 @@ exports[`transform in strip-only mode should strip type declarations 1`] = `
115115
`;
116116

117117
exports[`transform in strip-only mode should throw an error when it encounters a module 1`] = `
118-
Map {
119-
"code" => "UnsupportedSyntax",
120-
"column" => 0,
121-
"filename" => "test.ts",
122-
"line" => 1,
123-
"message" => "\`module\` keyword is not supported. Use \`namespace\` instead.",
124-
"snippet" => "module foo",
118+
{
119+
"code": "UnsupportedSyntax",
120+
"column": 0,
121+
"filename": "test.ts",
122+
"line": 1,
123+
"message": "\`module\` keyword is not supported. Use \`namespace\` instead.",
124+
"snippet": "module foo",
125125
}
126126
`;
127127

128128
exports[`transform in strip-only mode should throw an error when it encounters a module 2`] = `
129-
Map {
130-
"code" => "UnsupportedSyntax",
131-
"column" => 8,
132-
"filename" => "test.ts",
133-
"line" => 1,
134-
"message" => "\`module\` keyword is not supported. Use \`namespace\` instead.",
135-
"snippet" => "module foo",
129+
{
130+
"code": "UnsupportedSyntax",
131+
"column": 8,
132+
"filename": "test.ts",
133+
"line": 1,
134+
"message": "\`module\` keyword is not supported. Use \`namespace\` instead.",
135+
"snippet": "module foo",
136136
}
137137
`;
138138

139139
exports[`transform in strip-only mode should throw an error when it encounters a namespace 1`] = `
140-
Map {
141-
"code" => "UnsupportedSyntax",
142-
"column" => 0,
143-
"filename" => "test.ts",
144-
"line" => 1,
145-
"message" => "TypeScript namespace declaration is not supported in strip-only mode",
146-
"snippet" => "namespace Foo { export const m = 1; }",
140+
{
141+
"code": "UnsupportedSyntax",
142+
"column": 0,
143+
"filename": "test.ts",
144+
"line": 1,
145+
"message": "TypeScript namespace declaration is not supported in strip-only mode",
146+
"snippet": "namespace Foo { export const m = 1; }",
147147
}
148148
`;
149149

150150
exports[`transform in strip-only mode should throw an error when it encounters an enum 1`] = `
151-
Map {
152-
"code" => "UnsupportedSyntax",
153-
"column" => 0,
154-
"filename" => "test.ts",
155-
"line" => 1,
156-
"message" => "TypeScript enum is not supported in strip-only mode",
157-
"snippet" => "enum Foo {}",
151+
{
152+
"code": "UnsupportedSyntax",
153+
"column": 0,
154+
"filename": "test.ts",
155+
"line": 1,
156+
"message": "TypeScript enum is not supported in strip-only mode",
157+
"snippet": "enum Foo {}",
158158
}
159159
`;
160160

161161
exports[`transform in transform mode shoud throw an object even with deprecatedTsModuleAsError = true 1`] = `
162-
Map {
163-
"code" => "UnsupportedSyntax",
164-
"column" => 0,
165-
"filename" => "<anon>",
166-
"line" => 1,
167-
"message" => "\`module\` keyword is not supported. Use \`namespace\` instead.",
168-
"snippet" => "module F",
162+
{
163+
"code": "UnsupportedSyntax",
164+
"column": 0,
165+
"filename": "<anon>",
166+
"line": 1,
167+
"message": "\`module\` keyword is not supported. Use \`namespace\` instead.",
168+
"snippet": "module F",
169169
}
170170
`;
171171

172172
exports[`transform in transform mode should throw an error when it encounters a declared module 1`] = `
173-
Map {
174-
"code" => "UnsupportedSyntax",
175-
"column" => 8,
176-
"filename" => "test.ts",
177-
"line" => 1,
178-
"message" => "\`module\` keyword is not supported. Use \`namespace\` instead.",
179-
"snippet" => "module foo",
173+
{
174+
"code": "UnsupportedSyntax",
175+
"column": 8,
176+
"filename": "test.ts",
177+
"line": 1,
178+
"message": "\`module\` keyword is not supported. Use \`namespace\` instead.",
179+
"snippet": "module foo",
180180
}
181181
`;
182182

183183
exports[`transform in transform mode should throw an error when it encounters a module 1`] = `
184-
Map {
185-
"code" => "UnsupportedSyntax",
186-
"column" => 0,
187-
"filename" => "test.ts",
188-
"line" => 1,
189-
"message" => "\`module\` keyword is not supported. Use \`namespace\` instead.",
190-
"snippet" => "module foo",
184+
{
185+
"code": "UnsupportedSyntax",
186+
"column": 0,
187+
"filename": "test.ts",
188+
"line": 1,
189+
"message": "\`module\` keyword is not supported. Use \`namespace\` instead.",
190+
"snippet": "module foo",
191191
}
192192
`;
193193

bindings/binding_typescript_wasm/src/lib.rs

+19-22
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ pub fn transform_sync(input: JsValue, options: JsValue) -> Result<JsValue, JsVal
5656

5757
match result {
5858
Ok(v) => Ok(serde_wasm_bindgen::to_value(&v)?),
59-
Err(v) => Err(serde_wasm_bindgen::to_value(&v[0])?),
59+
Err(errors) => Err(serde_wasm_bindgen::to_value(&errors[0])?),
6060
}
6161
}
6262

63-
fn operate(input: String, options: Options) -> Result<TransformOutput, Vec<serde_json::Value>> {
63+
fn operate(input: String, options: Options) -> Result<TransformOutput, Vec<JsonDiagnostic>> {
6464
let cm = Lrc::new(SourceMap::default());
6565

6666
try_with_json_handler(cm.clone(), |handler| {
@@ -70,11 +70,11 @@ fn operate(input: String, options: Options) -> Result<TransformOutput, Vec<serde
7070

7171
#[derive(Clone)]
7272
struct LockedWriter {
73-
errors: Arc<Mutex<Vec<serde_json::Value>>>,
73+
errors: Arc<Mutex<Vec<JsonDiagnostic>>>,
7474
cm: Lrc<SourceMap>,
7575
}
7676

77-
fn try_with_json_handler<F, Ret>(cm: Lrc<SourceMap>, op: F) -> Result<Ret, Vec<serde_json::Value>>
77+
fn try_with_json_handler<F, Ret>(cm: Lrc<SourceMap>, op: F) -> Result<Ret, Vec<JsonDiagnostic>>
7878
where
7979
F: FnOnce(&Handler) -> Result<Ret, Error>,
8080
{
@@ -127,19 +127,16 @@ impl Emitter for LockedWriter {
127127
let filename = loc.as_ref().map(|loc| loc.file.name.to_string());
128128

129129
let error = JsonDiagnostic {
130-
code: error_code,
131-
message: &d.message[0].0,
132-
snippet: snippet.as_deref(),
133-
filename: filename.as_deref(),
130+
code: error_code.map(|s| s.to_string()),
131+
message: d.message[0].0.to_string(),
132+
snippet,
133+
filename,
134134
line: loc.as_ref().map(|loc| loc.line),
135135
column: loc.as_ref().map(|loc| loc.col_display),
136136
children,
137137
};
138138

139-
self.errors
140-
.lock()
141-
.unwrap()
142-
.push(serde_json::to_value(&error).unwrap());
139+
self.errors.lock().unwrap().push(error);
143140
}
144141

145142
fn take_diagnostics(&mut self) -> Vec<String> {
@@ -148,31 +145,31 @@ impl Emitter for LockedWriter {
148145
}
149146

150147
#[derive(Serialize)]
151-
struct JsonDiagnostic<'a> {
148+
struct JsonDiagnostic {
152149
/// Error code
153150
#[serde(skip_serializing_if = "Option::is_none")]
154-
code: Option<&'a str>,
155-
message: &'a str,
151+
code: Option<String>,
152+
message: String,
156153

157154
#[serde(skip_serializing_if = "Option::is_none")]
158-
snippet: Option<&'a str>,
155+
snippet: Option<String>,
159156

160157
#[serde(skip_serializing_if = "Option::is_none")]
161-
filename: Option<&'a str>,
158+
filename: Option<String>,
162159

163160
#[serde(skip_serializing_if = "Option::is_none")]
164161
line: Option<usize>,
165162
#[serde(skip_serializing_if = "Option::is_none")]
166163
column: Option<usize>,
167164

168165
#[serde(skip_serializing_if = "Vec::is_empty")]
169-
children: Vec<JsonSubdiagnostic<'a>>,
166+
children: Vec<JsonSubdiagnostic>,
170167
}
171168

172169
#[derive(Serialize)]
173-
struct JsonSubdiagnostic<'a> {
174-
message: &'a str,
175-
snippet: Option<&'a str>,
176-
filename: &'a str,
170+
struct JsonSubdiagnostic {
171+
message: String,
172+
snippet: Option<String>,
173+
filename: String,
177174
line: usize,
178175
}

0 commit comments

Comments
 (0)