Skip to content

Commit

Permalink
feat(napi/parser): add .hasChanged() to MagicString (#8586)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 18, 2025
1 parent 1bef911 commit ee8ee55
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ sha1 = "0.10.6"
simdutf8 = { version = "0.1.5", features = ["aarch64_neon"] }
similar = "2.6.0"
similar-asserts = "1.6.0"
string_wizard = "0.0.25"
string_wizard = "0.0.26"
tempfile = "3.14.0"
tokio = "1.42.0"
tower-lsp = "0.20.0"
Expand Down
1 change: 1 addition & 0 deletions napi/parser/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export declare class MagicString {
getUtf16ByteOffset(offset: number): number
length(): number
toString(): string
hasChanged(): boolean
append(input: string): this
appendLeft(index: number, input: string): this
appendRight(index: number, input: string): this
Expand Down
5 changes: 5 additions & 0 deletions napi/parser/src/magic_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ impl MagicString {
self.cell.borrow_dependent().to_string()
}

#[napi]
pub fn has_changed(&self) -> bool {
self.cell.borrow_dependent().has_changed()
}

#[napi]
pub fn append(&mut self, input: String) -> &Self {
self.cell.with_dependent_mut(|_, ms| {
Expand Down
3 changes: 2 additions & 1 deletion napi/parser/test/magic_string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ describe('simple', () => {
expect(code.substring(ms.getUtf16ByteOffset(start), ms.getUtf16ByteOffset(end))).toEqual('测试');

// Magic string manipulation.
expect(ms.hasChanged()).toBe(false);
ms.remove(start, end).append(';');
expect(ms.hasChanged()).toBe(true);
expect(ms.toString()).toEqual('const s: String = /* 🤨 */ "";');
});

Expand All @@ -43,7 +45,6 @@ describe('simple', () => {
});
expect(map.toUrl()).toBeTypeOf('string');
expect(map.toString()).toBeTypeOf('string');
console.log(map.toMap());
expect(map.toMap()).toEqual({
mappings:
'CAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC',
Expand Down

0 comments on commit ee8ee55

Please sign in to comment.