diff --git a/package.json b/package.json index 1bf7bd1..e6c89e1 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ }, "dependencies": { "@ampproject/remapping": "^2.3.0", - "magic-string": "^0.30.10" + "magic-string": "^0.30.17" }, "devDependencies": { "@antfu/eslint-config": "^2.14.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 11e6442..2739270 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -12,8 +12,8 @@ importers: specifier: ^2.3.0 version: 2.3.0 magic-string: - specifier: ^0.30.10 - version: 0.30.10 + specifier: ^0.30.17 + version: 0.30.17 devDependencies: '@antfu/eslint-config': specifier: ^2.14.0 @@ -178,10 +178,18 @@ packages: resolution: {integrity: sha512-mM4COjgZox8U+JcXQwPijIZLElkgEpO5rsERVDJTc2qfCDfERyob6k5WegS14SX18IIjv+XD+GrqNumY5JRCDw==} engines: {node: '>=6.9.0'} + '@babel/helper-string-parser@7.25.9': + resolution: {integrity: sha512-4A/SCr/2KLd5jrtOMFzaKjVtAei3+2r/NChoBNoZ3EyP/+GlhoaEGoWOZUmFmoITP7zOJyHIMm+DYRd8o3PvHA==} + engines: {node: '>=6.9.0'} + '@babel/helper-validator-identifier@7.22.20': resolution: {integrity: sha512-Y4OZ+ytlatR8AI+8KZfKuL5urKp7qey08ha31L8b3BwewJAoJamTzyvxPR/5D+KkdJCGPq/+8TukHBlY10FX9A==} engines: {node: '>=6.9.0'} + '@babel/helper-validator-identifier@7.25.9': + resolution: {integrity: sha512-Ed61U6XJc3CVRfkERJWDz4dJwKe7iLmmJsbOGu9wSloNSFttHV0I8g6UAgb7qnK5ly5bGLPd4oXZlxCdANBOWQ==} + engines: {node: '>=6.9.0'} + '@babel/helper-validator-option@7.22.5': resolution: {integrity: sha512-R3oB6xlIVKUnxNUxbmgq7pKjxpru24zlimpE8WK47fACIlM0II/Hm1RS8IaOI7NgCr6LNS+jl5l75m20npAziw==} engines: {node: '>=6.9.0'} @@ -199,6 +207,11 @@ packages: engines: {node: '>=6.0.0'} hasBin: true + '@babel/parser@7.26.7': + resolution: {integrity: sha512-kEvgGGgEjRUutvdVvZhbn/BxVt+5VSpwXz1j3WYXQbXDo8KzFOPNG2GQbdAiNq8g6wn1yKk7C/qrke03a84V+w==} + engines: {node: '>=6.0.0'} + hasBin: true + '@babel/standalone@7.22.13': resolution: {integrity: sha512-JoI61IOKM8jJv8V4yD0HprU/Lnx3Y29bGGULdIdJgvIUS7oCWcl43gtXoLY7nrYZhZerXYncYfDtmq4wUEofcg==} engines: {node: '>=6.9.0'} @@ -215,6 +228,10 @@ packages: resolution: {integrity: sha512-siazHiGuZRz9aB9NpHy9GOs9xiQPKnMzgdr493iI1M67vRXpnEq8ZOOKzezC5q7zwuQ6sDhdSp4SD9ixKSqKZg==} engines: {node: '>=6.9.0'} + '@babel/types@7.26.7': + resolution: {integrity: sha512-t8kDRGrKXyp6+tjUh7hw2RLyclsW4TRoRvRHtSyAX9Bb5ldlFh+90YAYY6awRXrlB4G5G2izNeGySpATlFzmOg==} + engines: {node: '>=6.9.0'} + '@clack/core@0.3.4': resolution: {integrity: sha512-H4hxZDXgHtWTwV3RAVenqcC4VbJZNegbBjlPvzOzCouXtS2y3sDvlO3IsbrPNWuLWPPlYVYPghQdSF64683Ldw==} @@ -691,6 +708,9 @@ packages: '@jridgewell/sourcemap-codec@1.4.15': resolution: {integrity: sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==} + '@jridgewell/sourcemap-codec@1.5.0': + resolution: {integrity: sha512-gv3ZRaISU3fjPAgNsriBRqGWQL6quFx04YMPW/zD8XMLsU32mhCCbfbO6KZFLjvYpCZ8zyDEgqsgf+PwPaM7GQ==} + '@jridgewell/trace-mapping@0.3.25': resolution: {integrity: sha512-vNk6aEwybGtawWmy/PzwnGDOjCkLWSD2wqvjGGAgOAwCGWySYXfYoxt00IJkTF+8Lb57DwOb3Aa0o9CApepiYQ==} @@ -1951,8 +1971,8 @@ packages: resolution: {integrity: sha512-8UnnX2PeRAPZuN12svgR9j7M1uWMovg/CEnIwIG0LFkXSJJe4PdfUGiTGl8V9bsBHFUtfVINcSyYxd7q+kx9fA==} engines: {node: '>=12'} - magic-string@0.30.10: - resolution: {integrity: sha512-iIRwTIf0QKV3UAnYK4PU8uiEc4SRh5jX0mwpIwETPpHdhVM4f53RSwS/vXvN1JhGX+Cs7B8qIq3d6AH49O5fAQ==} + magic-string@0.30.17: + resolution: {integrity: sha512-sNPKHvyjVf7gyjwS4xGTaW/mCnF8wnjtifKBEhxfZ7E/S8tQ0rssrwGNn6q8JH/ohItJfSQp9mBtQYuTlH5QnA==} mdast-util-from-markdown@0.8.5: resolution: {integrity: sha512-2hkTXtYYnr+NubD/g6KGBS/0mFmBcifAsI0yIWRiRo0PjVs6SSOSOdtzbp6kSGnShDN6G5aWZpKQ2lWRy27mWQ==} @@ -2052,6 +2072,11 @@ packages: engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1} hasBin: true + nanoid@3.3.8: + resolution: {integrity: sha512-WNLf5Sd8oZxOm+TzppcYk8gVOgP+l58xNy58D0nbUnOxOWRWvlcCV4kUF7ltmI6PsrLl/BgKEyS4mqsGChFN0w==} + engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1} + hasBin: true + natural-compare-lite@1.4.0: resolution: {integrity: sha512-Tj+HTDSJJKaZnfiuw+iaF9skdPpTo2GtEly5JHnWV/hfv2Qj/9RKsGISQtLh2ox3l5EAGw487hnBee0sIJ6v2g==} @@ -2187,6 +2212,9 @@ packages: picocolors@1.0.0: resolution: {integrity: sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==} + picocolors@1.1.1: + resolution: {integrity: sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==} + picomatch@2.3.1: resolution: {integrity: sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==} engines: {node: '>=8.6'} @@ -2220,6 +2248,10 @@ packages: resolution: {integrity: sha512-Wglpdk03BSfXkHoQa3b/oulrotAkwrlLDRSOb9D0bN86FdRyE9lppSp33aHNPgBa0JKCoB+drFLZkQoRRYae5A==} engines: {node: ^10 || ^12 || >=14} + postcss@8.5.1: + resolution: {integrity: sha512-6oz2beyjc5VMn/KV1pPw8fliQkhBXrVn1Z3TVyqZxU8kZpzEKhBdmCFqI6ZbmGtamQvQGuU1sgPTk8ZrXDD7jQ==} + engines: {node: ^10 || ^12 || >=14} + prelude-ls@1.2.1: resolution: {integrity: sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==} engines: {node: '>= 0.8.0'} @@ -2384,6 +2416,10 @@ packages: resolution: {integrity: sha512-itJW8lvSA0TXEphiRoawsCksnlf8SyvmFzIhltqAHluXd88pkCd+cXJVHTDwdCr0IzwptSm035IHQktUu1QUMg==} engines: {node: '>=0.10.0'} + source-map-js@1.2.1: + resolution: {integrity: sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==} + engines: {node: '>=0.10.0'} + spdx-correct@3.1.1: resolution: {integrity: sha512-cOYcUWwhCuHCXi49RhFRCyJEK3iPj1Ziz9DpViV3tbZOwXD49QzIN3MpOLJNxh2qwq2lJJZaKMVw9qNi4jTC0w==} @@ -2845,8 +2881,12 @@ snapshots: '@babel/helper-string-parser@7.22.5': {} + '@babel/helper-string-parser@7.25.9': {} + '@babel/helper-validator-identifier@7.22.20': {} + '@babel/helper-validator-identifier@7.25.9': {} + '@babel/helper-validator-option@7.22.5': {} '@babel/helpers@7.22.11': @@ -2867,6 +2907,10 @@ snapshots: dependencies: '@babel/types': 7.22.11 + '@babel/parser@7.26.7': + dependencies: + '@babel/types': 7.26.7 + '@babel/standalone@7.22.13': {} '@babel/template@7.22.5': @@ -2896,6 +2940,11 @@ snapshots: '@babel/helper-validator-identifier': 7.22.20 to-fast-properties: 2.0.0 + '@babel/types@7.26.7': + dependencies: + '@babel/helper-string-parser': 7.25.9 + '@babel/helper-validator-identifier': 7.25.9 + '@clack/core@0.3.4': dependencies: picocolors: 1.0.0 @@ -3191,6 +3240,8 @@ snapshots: '@jridgewell/sourcemap-codec@1.4.15': {} + '@jridgewell/sourcemap-codec@1.5.0': {} + '@jridgewell/trace-mapping@0.3.25': dependencies: '@jridgewell/resolve-uri': 3.1.0 @@ -3531,7 +3582,7 @@ snapshots: '@vitest/snapshot@1.5.0': dependencies: - magic-string: 0.30.10 + magic-string: 0.30.17 pathe: 1.1.2 pretty-format: 29.7.0 @@ -3548,10 +3599,10 @@ snapshots: '@vue/compiler-core@3.3.10': dependencies: - '@babel/parser': 7.23.5 + '@babel/parser': 7.26.7 '@vue/shared': 3.3.10 estree-walker: 2.0.2 - source-map-js: 1.2.0 + source-map-js: 1.2.1 '@vue/compiler-dom@3.3.10': dependencies: @@ -3560,16 +3611,16 @@ snapshots: '@vue/compiler-sfc@3.3.10': dependencies: - '@babel/parser': 7.23.5 + '@babel/parser': 7.26.7 '@vue/compiler-core': 3.3.10 '@vue/compiler-dom': 3.3.10 '@vue/compiler-ssr': 3.3.10 '@vue/reactivity-transform': 3.3.10 '@vue/shared': 3.3.10 estree-walker: 2.0.2 - magic-string: 0.30.10 - postcss: 8.4.38 - source-map-js: 1.2.0 + magic-string: 0.30.17 + postcss: 8.5.1 + source-map-js: 1.2.1 '@vue/compiler-ssr@3.3.10': dependencies: @@ -3578,11 +3629,11 @@ snapshots: '@vue/reactivity-transform@3.3.10': dependencies: - '@babel/parser': 7.23.5 + '@babel/parser': 7.26.7 '@vue/compiler-core': 3.3.10 '@vue/shared': 3.3.10 estree-walker: 2.0.2 - magic-string: 0.30.10 + magic-string: 0.30.17 '@vue/shared@3.3.10': {} @@ -4620,11 +4671,11 @@ snapshots: magic-string@0.27.0: dependencies: - '@jridgewell/sourcemap-codec': 1.4.15 + '@jridgewell/sourcemap-codec': 1.5.0 - magic-string@0.30.10: + magic-string@0.30.17: dependencies: - '@jridgewell/sourcemap-codec': 1.4.15 + '@jridgewell/sourcemap-codec': 1.5.0 mdast-util-from-markdown@0.8.5: dependencies: @@ -4720,6 +4771,8 @@ snapshots: nanoid@3.3.7: {} + nanoid@3.3.8: {} + natural-compare-lite@1.4.0: {} natural-compare@1.4.0: {} @@ -4852,6 +4905,8 @@ snapshots: picocolors@1.0.0: {} + picocolors@1.1.1: {} + picomatch@2.3.1: {} picomatch@4.0.2: {} @@ -4879,6 +4934,12 @@ snapshots: picocolors: 1.0.0 source-map-js: 1.2.0 + postcss@8.5.1: + dependencies: + nanoid: 3.3.8 + picocolors: 1.1.1 + source-map-js: 1.2.1 + prelude-ls@1.2.1: {} pretty-bytes@6.1.1: {} @@ -4956,7 +5017,7 @@ snapshots: rollup-plugin-dts@6.0.0(rollup@3.28.1)(typescript@5.4.5): dependencies: - magic-string: 0.30.10 + magic-string: 0.30.17 rollup: 3.28.1 typescript: 5.4.5 optionalDependencies: @@ -5036,6 +5097,8 @@ snapshots: source-map-js@1.2.0: {} + source-map-js@1.2.1: {} + spdx-correct@3.1.1: dependencies: spdx-expression-parse: 3.0.1 @@ -5189,7 +5252,7 @@ snapshots: globby: 13.2.2 hookable: 5.5.3 jiti: 1.21.0 - magic-string: 0.30.10 + magic-string: 0.30.17 mkdist: 1.3.0(typescript@5.4.5) mlly: 1.6.0 pathe: 1.1.2 @@ -5282,7 +5345,7 @@ snapshots: debug: 4.3.4 execa: 8.0.1 local-pkg: 0.5.0 - magic-string: 0.30.10 + magic-string: 0.30.17 pathe: 1.1.2 picocolors: 1.0.0 std-env: 3.5.0 diff --git a/src/index.ts b/src/index.ts index 438173f..f60e251 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,7 +44,10 @@ export default class MagicStringStack implements MagicStringStackType { return parent }, set: (_, p, value) => { - return Reflect.set(this, p, value, this) + if (Reflect.has(this, p)) + return Reflect.set(this, p, value, this) + + return Reflect.set(this._current, p, value) }, }) as any @@ -58,6 +61,7 @@ export default class MagicStringStack implements MagicStringStackType { */ commit() { const newOne = new MagicString(this._current.toString(), this._options) + newOne.offset = this._current.offset this._current = newOne this._stack.unshift(newOne) return this diff --git a/test/index.test.ts b/test/index.test.ts index 3808803..eb67a7a 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -111,6 +111,17 @@ it('should be chainable', () => { .toMatchInlineSnapshot(`"AAAA,OAAK,CAAC"`) }) +it('should support offset modification', () => { + const s = new MagicStringStack(' problems = 99') + s.offset = 1 + s.update(0, 8, 'answer') + expect(s.toString()).toMatchInlineSnapshot(`" answer = 99"`) + + s.commit() + s.update(0, 6, 'problems') + expect(s.toString()).toMatchInlineSnapshot(`" problems = 99"`) +}) + function removeEmptyKeys(obj: any) { return Object.fromEntries(Object.entries(obj).filter(([_, v]) => !(v == null || (Array.isArray(v) && !v.length)))) }