Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
101 changes: 82 additions & 19 deletions pnpm-lock.yaml

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

6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
}