Skip to content

Commit 40e216d

Browse files
authoredMar 23, 2025··
feat(ts/fast-strip): Remove line numbers (#10254)
**Description:** We use string search to replace line numbers
1 parent 254bcf9 commit 40e216d

File tree

4 files changed

+120
-32
lines changed

4 files changed

+120
-32
lines changed
 

‎bindings/Cargo.lock

+15-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎bindings/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ resolver = "2"
3636
swc_atoms = "5.0.0"
3737
swc_cached = "2.0.0"
3838
swc_cli_impl = "16.0.0"
39-
swc_common = "8.0.0"
39+
swc_common = "8.0.1"
4040
swc_compiler_base = "13.0.1"
4141
swc_config = "2.0.0"
42-
swc_core = "16.8.0"
42+
swc_core = "16.9.1"
4343
swc_css_ast = "8.0.0"
4444
swc_css_codegen = "8.0.0"
4545
swc_css_minifier = "8.0.0"
4646
swc_css_parser = "8.0.0"
47-
swc_error_reporters = "9.1.0"
47+
swc_error_reporters = "9.1.1"
4848
swc_fast_ts_strip = "12.1.0"
4949
swc_html = "12.0.0"
5050
swc_html_ast = "8.0.0"

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

+13-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ exports[`transform in strip-only mode should not emit 'Caused by: failed to pars
99
"message": "await isn't allowed in non-async function",
1010
"snippet": "
1111
12-
1 function foo() { await Promise.resolve(1); }
12+
function foo() { await Promise.resolve(1); }
1313
^^^^^
1414
1515
",
@@ -53,7 +53,7 @@ exports[`transform in strip-only mode should report correct error for syntax err
5353
"message": "Expected ';', '}' or <eof>",
5454
"snippet": "
5555
56-
1 function foo() { invalid syntax }
56+
function foo() { invalid syntax }
5757
^^^^^^
5858
5959
",
@@ -70,10 +70,10 @@ exports[`transform in strip-only mode should report correct error for unsupporte
7070
"filename": "test.ts",
7171
"message": "TypeScript enum is not supported in strip-only mode",
7272
"snippet": "
73-
[1:1]
74-
1 > enum Foo {
75-
2 a, b
76-
3 > }
73+
74+
> enum Foo {
75+
a, b
76+
> }
7777
7878
",
7979
"startColumn": 0,
@@ -143,7 +143,7 @@ exports[`transform in strip-only mode should throw an error when it encounters a
143143
"message": "\`module\` keyword is not supported. Use \`namespace\` instead.",
144144
"snippet": "
145145
146-
1 module foo { }
146+
module foo { }
147147
^^^^^^^^^^
148148
149149
",
@@ -161,7 +161,7 @@ exports[`transform in strip-only mode should throw an error when it encounters a
161161
"message": "\`module\` keyword is not supported. Use \`namespace\` instead.",
162162
"snippet": "
163163
164-
1 declare module foo { }
164+
declare module foo { }
165165
^^^^^^^^^^
166166
167167
",
@@ -179,7 +179,7 @@ exports[`transform in strip-only mode should throw an error when it encounters a
179179
"message": "TypeScript namespace declaration is not supported in strip-only mode",
180180
"snippet": "
181181
182-
1 namespace Foo { export const m = 1; }
182+
namespace Foo { export const m = 1; }
183183
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
184184
185185
",
@@ -197,7 +197,7 @@ exports[`transform in strip-only mode should throw an error when it encounters a
197197
"message": "TypeScript enum is not supported in strip-only mode",
198198
"snippet": "
199199
200-
1 enum Foo {}
200+
enum Foo {}
201201
^^^^^^^^^^^
202202
203203
",
@@ -215,7 +215,7 @@ exports[`transform in transform mode shoud throw an object even with deprecatedT
215215
"message": "\`module\` keyword is not supported. Use \`namespace\` instead.",
216216
"snippet": "
217217
218-
1 module F { export type x = number }
218+
module F { export type x = number }
219219
^^^^^^^^
220220
221221
",
@@ -233,7 +233,7 @@ exports[`transform in transform mode should throw an error when it encounters a
233233
"message": "\`module\` keyword is not supported. Use \`namespace\` instead.",
234234
"snippet": "
235235
236-
1 declare module foo { }
236+
declare module foo { }
237237
^^^^^^^^^^
238238
239239
",
@@ -251,7 +251,7 @@ exports[`transform in transform mode should throw an error when it encounters a
251251
"message": "\`module\` keyword is not supported. Use \`namespace\` instead.",
252252
"snippet": "
253253
254-
1 module foo { }
254+
module foo { }
255255
^^^^^^^^^^
256256
257257
",

‎bindings/binding_typescript_wasm/src/lib.rs

+89-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ impl Emitter for JsonErrorWriter {
139139
span,
140140
},
141141
) {
142-
Ok(()) => Some(snippet),
142+
Ok(()) => Some({
143+
remove_line_number(&mut snippet);
144+
snippet
145+
}),
143146
Err(_) => None,
144147
}
145148
});
@@ -261,3 +264,88 @@ impl std::fmt::Debug for Snippet<'_> {
261264
Ok(())
262265
}
263266
}
267+
268+
/// Finds for `[\d+\s+]` and `\[\d+:\d+\]` and remove them.
269+
fn remove_line_number(snippet: &mut String) {
270+
let mut result = String::with_capacity(snippet.len());
271+
let lines = snippet.lines();
272+
273+
for line in lines {
274+
let mut new_line = String::with_capacity(line.len());
275+
276+
// Check for [n:m] pattern
277+
if let Some(pos) = line.find('[') {
278+
if let Some(end_pos) = line[pos..].find(']') {
279+
let bracket_content = &line[pos + 1..pos + end_pos];
280+
if bracket_content
281+
.chars()
282+
.all(|c| c.is_ascii_digit() || c == ':')
283+
{
284+
// Replace [n:m] with spaces
285+
for _ in 0..pos {
286+
new_line.push(' ');
287+
}
288+
for _ in 0..(end_pos + 1) {
289+
new_line.push(' ');
290+
}
291+
new_line.push_str(&line[pos + end_pos + 1..]);
292+
result.push_str(&new_line);
293+
result.push('\n');
294+
continue;
295+
}
296+
}
297+
}
298+
299+
// Check for digit-space pattern (e.g., " 1 ")
300+
let trimmed = line.trim_start();
301+
302+
if !trimmed.is_empty() && trimmed.chars().next().unwrap().is_ascii_digit() {
303+
let leading_space_count = line.len() - trimmed.len();
304+
let mut digit_end = 0;
305+
306+
// Find where the digits end
307+
for (i, c) in trimmed.char_indices() {
308+
if !c.is_ascii_digit() {
309+
digit_end = i;
310+
break;
311+
}
312+
}
313+
314+
if digit_end == 0 && trimmed.chars().all(|c| c.is_ascii_digit()) {
315+
digit_end = trimmed.len();
316+
}
317+
318+
// Check if digits are followed by whitespace
319+
if digit_end < trimmed.len()
320+
&& trimmed[digit_end..].starts_with(|c: char| c.is_whitespace())
321+
{
322+
// Count trailing whitespace
323+
let trailing_space_end = trimmed[digit_end..]
324+
.chars()
325+
.take_while(|&c| c.is_whitespace())
326+
.count();
327+
328+
// Replace number and surrounding spaces with spaces
329+
for _ in 0..(leading_space_count + digit_end + trailing_space_end) {
330+
new_line.push(' ');
331+
}
332+
333+
new_line.push_str(&trimmed[digit_end + trailing_space_end..]);
334+
result.push_str(&new_line);
335+
result.push('\n');
336+
continue;
337+
}
338+
}
339+
340+
// If no pattern matched, keep the line as is
341+
result.push_str(line);
342+
result.push('\n');
343+
}
344+
345+
// Remove the trailing newline if the original didn't have one
346+
if !snippet.ends_with('\n') && !result.is_empty() {
347+
result.pop();
348+
}
349+
350+
*snippet = result;
351+
}

0 commit comments

Comments
 (0)
Please sign in to comment.