Visitar URL original
feat: nativeMagicString clone · rolldown/rolldown@4100cfa · GitHub
Skip to content

Commit 4100cfa

Browse files
feat: nativeMagicString clone
1 parent 92e1d20 commit 4100cfa

4 files changed

Lines changed: 19 additions & 4 deletions

File tree

‎crates/rolldown_binding/src/types/binding_magic_string.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use napi::bindgen_prelude::This;
33
use napi_derive::napi;
44
use string_wizard::MagicString;
55

6+
#[derive(Clone)]
67
struct CharToByteMapper {
78
char_to_byte: Vec<usize>,
89
}
@@ -236,6 +237,12 @@ impl BindingMagicString<'_> {
236237
))
237238
}
238239

240+
/// Returns a clone of the MagicString instance.
241+
#[napi]
242+
pub fn clone(&self) -> Self {
243+
Self { inner: self.inner.clone(), char_to_byte_mapper: self.char_to_byte_mapper.clone() }
244+
}
245+
239246
/// Returns the content between the specified original character positions.
240247
/// Supports negative indices (counting from the end).
241248
#[napi]

‎packages/rolldown/src/binding.d.cts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,8 @@ export declare class BindingMagicString {
14581458
* This matches the original magic-string API which deprecated this method.
14591459
*/
14601460
insert(index: number, content: string): void
1461+
/** Returns a clone of the MagicString instance. */
1462+
clone(): BindingMagicString
14611463
/**
14621464
* Returns the content between the specified original character positions.
14631465
* Supports negative indices (counting from the end).

‎packages/rolldown/tests/magic-string/MagicString.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ describe('MagicString', () => {
134134
});
135135
});
136136

137-
describe.skip('clone', () => {
137+
describe('clone', () => {
138138
it('should clone a magic string', () => {
139139
const s = new MagicString('abcdefghijkl');
140140

@@ -145,7 +145,7 @@ describe('MagicString', () => {
145145
assert.equal(c.toString(), 'abcXYZjkl');
146146
});
147147

148-
it('should clone filename info', () => {
148+
it.skip('should clone filename info', () => {
149149
const s = new MagicString('abcdefghijkl', { filename: 'foo.js' });
150150
const c = s.clone();
151151

‎packages/rolldown/tests/magic-string/download-tests.mjs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@
3232
* - indent(indentor?: string | undefined | null): this
3333
* - slice(start?: number, end?: number): string
3434
* - insert(index: number, content: string): throws Error (deprecated)
35+
* - clone(): BindingMagicString
3536
*
3637
* NOT supported (will be skipped):
3738
* - constructor options (filename, ignoreList, indentExclusionRanges)
38-
* - snip, clone, reset
39+
* - snip, reset
3940
* - generateMap, generateDecodedMap, addSourcemapLocation
4041
* - lastChar, lastLine
4142
* - original property
@@ -56,7 +57,6 @@ const BASE_URL = 'https://raw.githubusercontent.com/Rich-Harris/magic-string/mas
5657
const SKIP_DESCRIBE_BLOCKS = [
5758
'options', // constructor options not supported
5859
'addSourcemapLocation',
59-
'clone',
6060
'generateDecodedMap',
6161
'generateMap',
6262
'getIndentString', // not supported
@@ -67,6 +67,7 @@ const SKIP_DESCRIBE_BLOCKS = [
6767
'snip',
6868
// Note: 'insert' is now supported (throws deprecated error as expected)
6969
// Note: 'slice' is now supported
70+
// Note: 'clone' is now supported (some individual tests skipped)
7071
// Note: hasChanged, replace, replaceAll, isEmpty, length, remove, update, overwrite
7172
// are now enabled with individual test skips for problematic cases
7273
];
@@ -117,6 +118,11 @@ const SKIP_TESTS = [
117118
'should provide a useful error', // expects throw but gets panic
118119
// slice-specific skips
119120
'supports characters moved', // complex move + slice interaction
121+
// clone-specific skips (tests that use unsupported constructor options)
122+
'should clone filename info', // uses filename constructor option
123+
'should clone indentExclusionRanges', // uses indentExclusionRanges constructor option
124+
'should clone complex indentExclusionRanges', // uses indentExclusionRanges constructor option
125+
'should clone sourcemapLocations', // uses sourcemapLocations
120126
// hasChanged tests that use clone
121127
'should not report change if content is identical', // uses clone
122128
'should works', // uses clone

0 commit comments

Comments
 (0)