Skip to content

Commit

Permalink
fix blank string
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Jul 5, 2024
1 parent f2c5d06 commit 4ea8b34
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 28 deletions.
16 changes: 6 additions & 10 deletions crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,21 @@ pub fn operate(
let mut ts_strip = TsStrip::default();
program.visit_with(&mut ts_strip);

let mut replacements = ts_strip.replacements;
let replacements = ts_strip.replacements;

if replacements.is_empty() {
return Ok(fm.src.to_string());
}

replacements.sort();

let mut code = String::with_capacity(fm.src.len());
let mut index = 0;
let mut code = <std::string::String as Clone>::clone(&fm.src).into_bytes();

for r in replacements {
code.push_str(&fm.src[index..(r.0 .0 - 1) as usize]);
index = (r.1 .0 - 1) as usize;
code[(r.0 .0 - 1) as usize..(r.1 .0 - 1) as usize]
.iter_mut()
.for_each(|b| *b = b' ');
}

code.push_str(&fm.src[index..]);

Ok(code)
String::from_utf8(code).map_err(|_| anyhow::anyhow!("failed to convert to utf-8"))
}

#[derive(Default)]
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_fast_ts_strip/tests/fixture/const-assertion.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@


const foo = [1, 3, 5];
const foo = [1, 3, 5] ;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@


const foo = call
const foo = call
8 changes: 4 additions & 4 deletions crates/swc_fast_ts_strip/tests/fixture/mixed/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const foo = {
foo: 1,
bar: "bar",
baz: foo,
};
foo: 1 ,
bar: "bar" ,
baz: foo ,
} ;
const bar = "bar";
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const foo = 1;
const foo = 1 ;
const bar = "bar";
4 changes: 2 additions & 2 deletions crates/swc_fast_ts_strip/tests/fixture/pat-optional.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@


export function typeAnn({ a, b, c }) {
export function typeAnn({ a, b, c } ) {
console.log(a, b, c);
}

export function optional(a) {
export function optional(a ) {
console.log(a, b, c);
}
2 changes: 1 addition & 1 deletion crates/swc_fast_ts_strip/tests/fixture/satisfies/1.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const foo = 1;
const foo = 1 ;
const bar = "bar";
4 changes: 2 additions & 2 deletions crates/swc_fast_ts_strip/tests/fixture/this-param.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export function foo() {}
export function bar(x) {}
export function foo( ) {}
export function bar( x ) {}
3 changes: 2 additions & 1 deletion crates/swc_fast_ts_strip/tests/fixture/type-alias/1.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const foo = 1;
export const foo = 1;

6 changes: 3 additions & 3 deletions crates/swc_fast_ts_strip/tests/fixture/type-alias/2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const foo = 1;


const bar = "bar";
const bar = "bar";
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const foo = 1;
const bar = "bar";
const bar = "bar";
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const foo = 1;
const foo = 1 ;
const bar = "bar";

0 comments on commit 4ea8b34

Please sign in to comment.