Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce withOffset #297

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

zhiyuanzmj
Copy link

@zhiyuanzmj zhiyuanzmj commented Dec 12, 2024

close #296

Returns a proxy instance that modifies the incoming position by adding an offset.
Intercepted API: slice, update, overwrite, appendLeft, prependLeft, appendRight, prependRight, move, clone, snip, reset, remove.

import { MagicStringOffset } from 'magic-string'

const s = new MagicString('hello world').withOffset(6)
s.slice() === 'world'

const s1 = new MagicStringOffset('hello world', { offset: 6 })
s1.slice() === 'world'
s1.offset = 8
s1.slice() === 'rld'

src/MagicStringOffset.js Outdated Show resolved Hide resolved
@antfu
Copy link
Collaborator

antfu commented Dec 13, 2024

This makes me wonder, if we should have the offset function directly in the MagicString class and make it immutable. But yeah it's also tricky as it's different from .clone this would write back to the original instance

s = s.withOffset(9);
assert.strictEqual(s.slice(), 'ld');

s.offset = 10;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am also not sure about this usage, it feels like we should either make it always immutable (always call .withOffset to create a new instance) or always mutable (that brings us back to the original issue).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, after a second thought, I think the original design might be better. We shouldn't think too much about immutability here, as the MagicString itself is designed to be mutable. Let's check back to a single class with the mutable offset property then. Sorry for the back and forth

Copy link
Author

@zhiyuanzmj zhiyuanzmj Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No matter, I will add an offset field in the afternoon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feat: add offfset option to support Vue SFC
2 participants