8000 fix(compiler-sfc): fix scope handling for props destructure in function parameters and catch clauses by edison1105 · Pull Request #12792 · vuejs/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(compiler-sfc): fix scope handling for props destructure in function parameters and catch clauses #12792

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 8000 related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 13, 2025

Conversation

edison1105
Copy link
Member
@edison1105 edison1105 commented Jan 30, 2025

close #12790

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of variable naming conflicts between destructured props and function parameters to prevent incorrect references.
    • Enhanced scope management to correctly handle variables declared within catch clauses.
  • Tests

    • Added a new test case to verify correct behavior when function parameters share names with destructured props.

Copy link
github-actions bot commented Jan 30, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB 38.2 kB 34.4 kB
vue.global.prod.js 159 kB 58.4 kB 51.9 kB

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.5 kB 18.2 kB 16.6 kB
createApp 54.5 kB 21.2 kB 19.4 kB
createSSRApp 58.7 kB 22.9 kB 20.9 kB
defineCustomElement 59.3 kB 22.8 kB 20.8 kB
overall 68.6 kB 26.4 kB 24 kB

Copy link
netlify bot commented Jan 30, 2025

Deploy Preview for vue-sfc-playground failed. Why did it fail? →

Name Link
🔨 Latest commit 3e2b34a
🔍 Latest deploy log https://app.netlify.com/sites/vue-sfc-playground/deploys/679b8efe9d5f1000080fdd25

Copy link
netlify bot commented Jan 30, 2025

Deploy Preview for vue-next-template-explorer failed. Why did it fail? →

Name Link
🔨 Latest commit 3e2b34a
🔍 Latest deploy log https://app.netlify.com/sites/vue-next-template-explorer/deploys/679b8efe523fcb0008f78bb5

Copy link
pkg-pr-new bot commented Jan 30, 2025

Open in StackBlitz

@vue/compiler-core

npm i https://pkg.pr.new/@vue/compiler-core@12792

@vue/compiler-dom

npm i https://pkg.pr.new/@vue/compiler-dom@12792

@vue/compiler-ssr

npm i https://pkg.pr.new/@vue/compiler-ssr@12792

@vue/compiler-sfc

npm i https://pkg.pr.new/@vue/compiler-sfc@12792

@vue/reactivity

npm i https://pkg.pr.new/@vue/reactivity@12792

@vue/runtime-core

npm i https://pkg.pr.new/@vue/runtime-core@12792

@vue/runtime-dom

npm i https://pkg.pr.new/@vue/runtime-dom@12792

@vue/server-renderer

npm i https://pkg.pr.new/@vue/server-renderer@12792

@vue/shared

npm i https://pkg.pr.new/@vue/shared@12792

vue

npm i https://pkg.pr.new/vue@12792

@vue/compat

npm i https://pkg.pr.new/@vue/compat@12792

commit: 4b6e904

8000

@edison1105 edison1105 changed the title fix(compiler-sfc): handling function parameters with same name as destructured props fix(compiler-sfc): fix scope handling for props destructure in function parameters and catch clauses Jan 30, 2025
@edison1105 edison1105 added ready to merge The PR is ready to be merged. scope: sfc 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels Feb 1, 2025
Copy link
coderabbitai bot commented May 13, 2025

Walkthrough

The changes add a new test to verify proper handling of variable shadowing between destructured props and function parameters in Vue's <script setup>. Additionally, the scope management logic in the compiler's AST traversal is updated to correctly handle scope exit for CatchClause nodes, ensuring accurate variable resolution.

Changes

File(s) Change Summary
packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts Added a test case to verify correct prop access when a function parameter shadows a destructured prop.
packages/compiler-sfc/src/script/definePropsDestructure.ts Updated scope popping logic to include CatchClause nodes during AST traversal.

Sequence Diagram(s)

sequenceDiagram
    participant UserScript as User's <script setup>
    participant Compiler as SFC Compiler
    participant AST as AST Walker

    UserScript->>Compiler: Contains destructured prop and function with same param name
    Compiler->>AST: Traverse AST nodes
    AST->>AST: Enter function, block, and catch clause scopes
    AST->>AST: Pop scope on leaving function, block, or catch clause
    AST->>Compiler: Correctly resolve prop vs. parameter references
    Compiler->>UserScript: Outputs compiled code with correct variable references
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure destructured prop access is not shadowed by function parameter of the same name (#12790)
Correctly manage scope exit for blocks, functions, and catch clauses to avoid variable resolution bugs (#12790)

Poem

In burrows deep, where shadows play,
A function met a prop one day—
Both named "value," what a pair!
But now the scopes are handled fair.
No more mix-ups, all is bright,
The code hops on, the tests delight!
🐇✨

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 258f78b and 4b6e904.

⛔ Files ignored due to path filters (1)
  • packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap is excluded by !**/*.snap
📒 Files selected for processing (2)
  • packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts (1 hunks)
  • packages/compiler-sfc/src/script/definePropsDestructure.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
packages/compiler-sfc/src/script/definePropsDestructure.ts (1)
packages/compiler-core/src/babelUtils.ts (1)
  • isFunctionType (307-309)
packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts (1)
packages/compiler-sfc/__tests__/utils.ts (1)
  • assertCode (27-42)
🔇 Additional comments (2)
packages/compiler-sfc/src/script/definePropsDestructure.ts (1)

294-296: Bug fix: Proper scope handling for catch clauses.

The PR correctly adds CatchClause to the list of node types that should trigger a scope pop in the leave visitor. This ensures that variable declarations within catch clauses don't leak into the outer scope, which is particularly important for maintaining correct prop references when the same variable name appears in both props and catch clause parameters.

packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts (1)

361-375: Good test case for variable shadowing in function parameters.

This test properly verifies that the compiler correctly handles variable shadowing between props and function parameters. The test ensures that outside the function scope, value correctly refers to the prop (__props.value), while inside the function it refers to the parameter. The test also includes a try-catch block, which connects it directly to the scope handling fix in the main code.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Doctor-wu
Copy link
Member

/ecosystem-ci run

@vue-bot
Copy link
Contributor 8000
vue-bot commented May 13, 2025

📝 Ran ecosystem CI: Open

suite result latest scheduled
language-tools success success
nuxt success success
vite-plugin-vue success success
primevue success success
pinia success success
quasar success success
router success success
radix-vue success success
vant success success
vue-i18n success success
vuetify success success
vitepress success success
vueuse success success
test-utils success success
vue-simple-compiler success success
vue-macros success success

@edison1105 edison1105 merged commit 8e34357 into main May 13, 2025
16 checks passed
@edison1105 edison1105 deleted the edison/fix/12790 branch May 13, 2025 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. ready to merge The PR is ready to be merged. scope: sfc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@vue/compiler-sfc can't compile this code correct
4 participants
0