feat(napi/parser): add .hasChanged() to MagicString (#8586)

This commit is contained in:
Boshen 2025-01-18 15:29:17 +00:00
parent 1bef911e59
commit ee8ee55cda
5 changed files with 11 additions and 4 deletions

4
Cargo.lock generated
View file

@ -2935,9 +2935,9 @@ checksum = "d08889ec5408683408db66ad89e0e1f93dff55c73a4ccc71c427d5b277ee47e6"
[[package]]
name = "string_wizard"
version = "0.0.25"
version = "0.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6bb58bc508ea1c7f771111be7870d0c8079410c0fe7e026454c7baa7998cf2a5"
checksum = "baa2ddbd6b187e9467902cf1c0f90266969897659ec5e7cefb3433ff5ffcf97d"
dependencies = [
"oxc_index",
"oxc_sourcemap",

View file

@ -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"

View file

@ -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

View file

@ -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| {

View file

@ -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 = /* 🤨 */ "";');
});
@ -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',