Skip to content

fix: avoid shadowing a variable in dynamic components #16185

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

7nik
Copy link
Contributor

@7nik 7nik commented Jun 16, 2025

Fixes #16163 caused by regression in #16118

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

  • 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.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

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

Copy link

changeset-bot bot commented Jun 16, 2025

🦋 Changeset detected

Latest commit: 4d0794a

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

@svelte-docs-bot
Copy link

Copy link
Contributor

Playground

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

// only if it's a valid identifier, otherwise we will use a default name
!node.metadata.dynamic || regex_is_valid_identifier.test(node.name) ? node.name : '$$component',
// avoid shadowing the component variable by a variable used in $.component
node.metadata.dynamic

Choose a reason for hiding this comment

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

Don't we need to keep the regex_is_valid_identifier test here for the node.name branch?

Also IMO we should cut this out and move it into its own variable -- it's getting hard to read

Choose a reason for hiding this comment

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

do we have any tests for invalid identifiers? if not we should add one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The name should be kept as is for non-dynamic components.
In the case of dynamic components, the name just defines the name of an intermediate variable that was shadowing the component's one. And before #15118 it was always $$component. Here, the name only defines a more convenient stack tracing. The regex was there because the component name can be Object.prop, see component-namespace and component-namespaced tests.

I thought about separating the names of the component's source and the intermediate variable, but couldn't come up with a nice one. I can try again tomorrow.

Copy link
Member

Choose a reason for hiding this comment

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

What if instead we just context.scope.generate(node.name)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Whoops, I must have accidentally clicked approve instead of request changes. See prior comment

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.

Rendering a dynamic component within a dynamic component is broken
3 participants