Skip to content

fix(compiler-sfc): add error handling for defineModel() without variable assignment #13352

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 8 commits into
base: main
Choose a base branch
from

Conversation

runyasak
Copy link

@runyasak runyasak commented May 18, 2025

Fixes #13280

Overview

I added validation to ensure defineModel() is always assigned to a variable, throwing an error message if used incorrectly.

Scope of work

add verified error message: defineModel() must be assigned to a variable

Screenshots

image

If you have any feedback or suggestions, please let me know and I will update the code accordingly.

Summary by CodeRabbit

  • Bug Fixes

    • Improved error handling to ensure an error is thrown if defineModel() is not assigned to a variable, with a clear error message.
  • Tests

    • Added a new test to verify that defineModel() must be assigned to a variable.
    • Updated existing tests to assign the result of defineModel() to a variable.
  • Chores

    • Updated an internal error code to maintain consistency.

Copy link

coderabbitai bot commented May 18, 2025

Walkthrough

A validation was added to enforce that defineModel() must be assigned to a variable, with an error thrown if not. Related test cases were updated or added to reflect this rule. Additionally, an error code in the DOM compiler was incremented by one to maintain alignment with core error codes.

Changes

File(s) Change Summary
packages/compiler-sfc/src/script/defineModel.ts Added validation to ensure defineModel() is assigned to a variable; reports an error if not.
packages/compiler-sfc/tests/compileScript/defineModel.spec.ts Added a test verifying that an error is thrown if defineModel() is not assigned to a variable.
packages/compiler-sfc/tests/compileScript.spec.ts Updated test cases to assign the result of defineModel() to a variable, matching new validation.
packages/compiler-dom/src/errors.ts Changed DOMErrorCodes.X_V_HTML_NO_EXPRESSION value from 53 to 54.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Compiler
    participant ErrorReporter

    User->>Compiler: Call defineModel()
    alt Not assigned to variable
        Compiler->>ErrorReporter: Report error ("defineModel() must be assigned...")
    else Assigned to variable
        Compiler->>Compiler: Continue processing defineModel()
    end
Loading

Poem

In the warren of code, a rule now appears,
"Assign your model!" the compiler now cheers.
No more loose models hopping away,
Errors will catch them, with no delay.
A hop, a check, a test anew—
The code is tidy, and rabbits approve! 🐇

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ 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
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 2b8ab9f and 1eb9f28.

📒 Files selected for processing (2)
  • packages/compiler-sfc/__tests__/compileScript/defineModel.spec.ts (1 hunks)
  • packages/compiler-sfc/src/script/defineModel.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/compiler-sfc/tests/compileScript/defineModel.spec.ts
  • packages/compiler-sfc/src/script/defineModel.ts
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Redirect rules
  • GitHub Check: Header rules
  • GitHub Check: Pages changed
✨ 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.

@KazariEX KazariEX added scope: compiler 🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. labels May 18, 2025
Copy link

github-actions bot commented May 19, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB 38.3 kB 34.5 kB
vue.global.prod.js 159 kB 58.5 kB 52 kB

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.6 kB 18.2 kB 16.7 kB
createApp 54.5 kB 21.2 kB 19.4 kB
createSSRApp 58.8 kB 23 kB 20.9 kB
defineCustomElement 59.5 kB 22.8 kB 20.8 kB
overall 68.6 kB 26.4 kB 24.1 kB

Copy link

pkg-pr-new bot commented May 19, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-ssr

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

@vue/compiler-sfc

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: 9d205dc

@edison1105
Copy link
Member

export enum DOMErrorCodes {
  X_V_HTML_NO_EXPRESSION = 53 /* ErrorCodes.__EXTEND_POINT__ */,

X_V_HTML_NO_EXPRESSION should be updated to 54 due to X_DEFINE_MODEL_NO_VARIABLE being added

@runyasak
Copy link
Author

X_V_HTML_NO_EXPRESSION should be updated to 54 due to X_DEFINE_MODEL_NO_VARIABLE being added

@edison1105 I have updated it for testing but forgot to commit. I will commit this change soon. Thank you so much.

@runyasak runyasak requested a review from KazariEX May 19, 2025 01:01
@edison1105 edison1105 added ready to merge The PR is ready to be merged. and removed wait changes labels May 19, 2025
@edison1105
Copy link
Member

edison1105 commented May 19, 2025

@runyasak
https://github.com/vuejs/core/actions/runs/15101991112/job/42444363129?pr=13352
The failed tests should be updated.

@runyasak
Copy link
Author

@runyasak https://github.com/vuejs/core/actions/runs/15101991112/job/42444363129?pr=13352 The failed tests should be updated.

@edison1105 I apologize for not running all the tests.
I updated by adding a variable when using defineModel.
Please feel free to review my code.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/compiler-sfc/__tests__/compileScript.spec.ts (1)

982-984: Missing space after the equals sign

There should be a space between = and defineModel for consistent code style.

-        const model =defineModel({
+        const model = defineModel({
          default: () => bar
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 65b9098 and 46083c1.

📒 Files selected for processing (1)
  • packages/compiler-sfc/__tests__/compileScript.spec.ts (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Redirect rules
  • GitHub Check: Header rules
  • GitHub Check: Pages changed
🔇 Additional comments (3)
packages/compiler-sfc/__tests__/compileScript.spec.ts (3)

982-986: LGTM: Good test case for variable reference restriction

This test correctly validates that defineModel() with locally declared let variables in the default function will throw an error, while ensuring defineModel() is assigned to a variable.


992-996: LGTM: Good test case for const variables

This test correctly verifies that using const variables in defineModel() is allowed, while ensuring proper variable assignment.


1002-1007: LGTM: Good test case for get/set handlers

This test correctly verifies that using let variables in defineModel() is allowed when they're used within get/set handlers, while ensuring proper variable assignment.

@runyasak
Copy link
Author

@edison1105 Oh, I forgot a space. 😂
Thank you for adding it for me.

@edison1105
Copy link
Member

LGTM~
Thanks for your contribution.

@runyasak
Copy link
Author

@edison1105 @KazariEX Thank you so much for helping me. 😁

@@ -21,7 +21,7 @@ export function createDOMCompilerError(
}

export enum DOMErrorCodes {
X_V_HTML_NO_EXPRESSION = 53 /* ErrorCodes.__EXTEND_POINT__ */,
X_V_HTML_NO_EXPRESSION = 54 /* ErrorCodes.__EXTEND_POINT__ */,
Copy link
Member

Choose a reason for hiding this comment

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

Breaking change here

Copy link
Author

Choose a reason for hiding this comment

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

Should I update anything here?

Copy link
Member

Choose a reason for hiding this comment

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

Just revert it.

Copy link
Author

Choose a reason for hiding this comment

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

Done. 😁

@runyasak runyasak requested a review from sxzz May 19, 2025 11:55
@edison1105
Copy link
Member

/ecosystem-ci run

@vue-bot
Copy link
Contributor

vue-bot commented May 19, 2025

📝 Ran ecosystem CI: Open

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. ready to merge The PR is ready to be merged. scope: compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

misleading error message when nesting/passing on v-model
5 participants