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

fix: wrap :id, :where:not and :has with :global during migration #13850

Merged
merged 10 commits into from
Oct 31, 2024

Conversation

paoloricciuti
Copy link
Member

@paoloricciuti paoloricciuti commented Oct 23, 2024

Before submitting the PR, please make sure you do the following

Closes #13765...if you can think of more weird case to test please let me know.

EDIT: i've added a slightly better test and realised that we could have a problem if the user has something like this

{`
<style>
	.this :is(a .problem){}
</style>
`}

in the template...should we care?

Also weirdly replaceAll from MagicString did not got the job done, might be worth opening an issue there.

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Oct 23, 2024

🦋 Changeset detected

Latest commit: 60fb2b5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

.changeset/sour-feet-carry.md Outdated Show resolved Hide resolved
div:has(:is(span)){}
div > :not(:is(span)){}
div > :is(:is(span)){}
div > :where(:is(span)){}
Copy link
Member

Choose a reason for hiding this comment

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

just for safety: can we add more tests around nested braces? Like :has(.x:not(...))

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure...if you can think of any other case please let me know

Copy link
Member

Choose a reason for hiding this comment

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

div :is(.class:is(span:is(:hover)), .x){} -> the .x isn't properly wrapped

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh I know why...let me fix

@paoloricciuti paoloricciuti changed the title feat: migrate css :id, :where:not and :has fix: wrap :id, :where:not and :has with :global during migration Oct 24, 2024
@dummdidumm
Copy link
Member

should we care?

I don't think this will ever come up in the wild, so I'm good with this approach, assuming we can deal with :is(.x, .y) cases.

@paoloricciuti
Copy link
Member Author

ouch i borked the fix...i need a bit more thinkering to fix this

@paoloricciuti
Copy link
Member Author

I don't think this is doable with regex only...we might need to interveen with some JS. Let me see what i can do

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

nice an simple

packages/svelte/src/compiler/migrate/index.js Outdated Show resolved Hide resolved
@Conduitry
Copy link
Member

I'd still like to argue for providing a way to run only this migration (and other future migrations we write that are for upgrading Svelte 4 code to non-runes Svelte 5 code).

I'm also worried about idempotency and this sort of change. When we're migrating a component, how do we know whether the user already made the appropriate changes for non-runes mode? Adding :global()s might cause them to lose scoping that they intended to have and were already accounting for from the v5 changes.

@dummdidumm
Copy link
Member

I'm not as worried about that since it's probably very rare that you'd escape scoping with one of these selectors. But yes, maybe we should do that change only when we detect it's not in runes mode yet? This is definitely tricky 😅

@Rich-Harris
Copy link
Member

Surely the issue is that you write a scoped selector, run the migration (on your entire codebase, which happens to include this component that doesn't need migration), and now you have an unscoped selector?

I'd previously suggested skipping the migration altogether for components that are already in runes mode for basically this reason, though it wouldn't help in the case of an ambiguous

@dummdidumm
Copy link
Member

skipping it entirely is a bit too harsh potentially because you might have done runes but not event attributes yet, which are harmless to do.

Co-authored-by: Simon H <[email protected]>
Copy link

pkg-pr-new bot commented Oct 24, 2024

pnpm add https://pkg.pr.new/svelte@13850

commit: 60fb2b5

@paoloricciuti
Copy link
Member Author

I'd still like to argue for providing a way to run only this migration (and other future migrations we write that are for upgrading Svelte 4 code to non-runes Svelte 5 code).

I'm also worried about idempotency and this sort of change. When we're migrating a component, how do we know whether the user already made the appropriate changes for non-runes mode? Adding :global()s might cause them to lose scoping that they intended to have and were already accounting for from the v5 changes.

Oh this also made me remember...we probably want to skip this if it's already global

image

@paoloricciuti
Copy link
Member Author

Considering that this migration is literally it's own separate function we could split it in a separate command (and maybe ask in the cli if you want to migrate css too)

@dummdidumm
Copy link
Member

This feels like too much overhead - most people will not know how to answer such questions. People who run the migration on a component (or project) do so with the intention of getting it transformed in a way that mimicks Svelte 4 as closely as possible while upgrading the syntax. It's impossible to know whether or not the intention of something being run on a component already in runes mode is to automatically convert the rest (like @paoloricciuti would probably do on his project after this lands; if he didn't already do it by hand) or leave it alone. I vote for erroring on the side of "wants more" and always do it.

@paoloricciuti
Copy link
Member Author

Yeah there's also to say that you still need to check the files and it's pretty easy to revert this changes with a click if you watch in diff mode in vscode

@dummdidumm dummdidumm merged commit e47ee22 into main Oct 31, 2024
9 checks passed
@dummdidumm dummdidumm deleted the migrate-css-is-where-not-had branch October 31, 2024 10:26
@github-actions github-actions bot mentioned this pull request Oct 31, 2024
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.

Migration: Wrap :has/not/where/is(...) selectors in :global(...) if necessary
4 participants