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

Conversation

runyasak
Copy link

@runyasak runyasak commented May 18, 2025

Overview

This PR is related to #13280.
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

  • New Features

    • Added a clear error message when defineModel() is not assigned to a variable, guiding users to use correct syntax (e.g., const model = defineModel()).
  • Bug Fixes

    • Improved error handling for incorrect usage of defineModel(), ensuring users receive informative feedback.
  • Tests

    • Added a test case to verify that an error is thrown if defineModel() is not assigned to a variable.
    • Updated existing tests to assign defineModel() results to variables for clarity and accuracy.

Copy link

coderabbitai bot commented May 18, 2025

Walkthrough

A new error code and message were introduced to enforce that defineModel() must be assigned to a variable. The compiler logic was updated to check for this condition, and a corresponding error is reported if violated. Test coverage was added to verify this behavior, ensuring proper error handling during compilation. Additionally, a numeric value of an existing DOM error code was incremented.

Changes

File(s) Change Summary
packages/compiler-core/src/errors.ts Added X_DEFINE_MODEL_NO_ASSIGNMENT to ErrorCodes and its message to errorMessages.
packages/compiler-sfc/src/script/defineModel.ts Added error reporting if defineModel() is not assigned to a variable using new error code.
packages/compiler-sfc/tests/compileScript/defineModel.spec.ts Added test case to verify error is thrown when defineModel() lacks variable assignment.
packages/compiler-sfc/tests/compileScript.spec.ts Modified tests to assign defineModel() to a variable in error-related cases.
packages/compiler-dom/src/errors.ts Updated enum member value: DOMErrorCodes.X_V_HTML_NO_EXPRESSION changed from 53 to 54.

Poem

In the warren of code, a new rule appears,
"Assign your model," the compiler now cheers.
If you forget and let it float free,
An error will hop out—quite rapidly!
With tests in the meadow and checks in the den,
Our models are safe—again and again!
🐇✨


📜 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 46083c1 and 2b8ab9f.

📒 Files selected for processing (1)
  • packages/compiler-sfc/__tests__/compileScript.spec.ts (3 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/compiler-sfc/tests/compileScript.spec.ts
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: test / e2e-test
  • GitHub Check: test / unit-test-windows
  • 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 (+1 B) 52.1 kB (+77 B)

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-sfc

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

@vue/compiler-ssr

npm i https://pkg.pr.new/@vue/compiler-ssr@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/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

@vue/server-renderer

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

commit: 2b8ab9f

@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. 😁

@@ -179,6 +179,7 @@ export const errorMessages: Record<ErrorCodes, string> = {
[ErrorCodes.X_INVALID_EXPRESSION]: `Error parsing JavaScript expression: `,
[ErrorCodes.X_KEEP_ALIVE_INVALID_CHILDREN]: `<KeepAlive> expects exactly one child component.`,
[ErrorCodes.X_VNODE_HOOKS]: `@vnode-* hooks in templates are no longer supported. Use the vue: prefix instead. For example, @vnode-mounted should be changed to @vue:mounted. @vnode-* hooks support has been removed in 3.4.`,
[ErrorCodes.X_DEFINE_MODEL_NO_ASSIGNMENT]: `defineModel() must be assigned to a variable. For example: const model = defineModel()`,
Copy link
Member

Choose a reason for hiding this comment

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

ErrorCodes are used in compiler-core/dom, but in compiler-sfc, we can inline error messages directly to reduce bundle size.

@@ -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

@@ -22,6 +27,10 @@ export function processDefineModel(
return false
}

if (!declId) {
ctx.error(errorMessages[ErrorCodes.X_DEFINE_MODEL_NO_ASSIGNMENT], node)
Copy link
Member

Choose a reason for hiding this comment

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

Just inline message

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.

4 participants