Skip to content

Conversation

@skirtles-code
Copy link
Contributor

@skirtles-code skirtles-code commented Dec 16, 2022

This PR is closely related to my other PR, #7360. There is significant overlap between them and merging one will cause conflicts for the other.

This PR introduces a compiler warning when it encounters :v- at the start of an attribute name. e.g. :v-if, :v-for or :v-model. This is almost invariably an error, and a common mistake made by beginners.

Only the : shorthand is affected. If someone really wants to have a DOM attribute beginning with v- then they can use v-bind:v- instead. Beginners are unlikely to make the mistake of using v-bind:v-for, so I see no reason to prevent that.

The compiler currently doesn't use warnings, only errors. However, I felt an error would be too severe for the initial introduction of this check as it would prevent some applications from building. It could be seen as a breaking change, as there's nothing in the docs to suggest that this isn't allowed. In future, once people have had chance to fix the warnings, we could maybe increase the severity to make it an error.

The warnings should be visible in the console for anyone using the SFC Playground or a browser-based compiler. For a standard Vite setup, the warning won't be shown in the browser, it'll only appear in the terminal running Vite.

SFC Playground example

Screenshot of Vite:

image

Summary by CodeRabbit

  • New Features

    • Added compiler warning for directives with colon-prefixed configurations to help developers identify and correct common directive usage errors.
  • Tests

    • Expanded test infrastructure to validate warning detection, reporting, and location information alongside existing error handling.
  • Chores

    • Updated internal error code numbering for consistency across compiler packages.

✏️ Tip: You can customize this high-level summary in your review settings.

@netlify
Copy link

netlify bot commented Dec 16, 2022

Deploy Preview for vuejs-coverage failed.

Name Link
🔨 Latest commit 02d0bd5
🔍 Latest deploy log https://app.netlify.com/sites/vuejs-coverage/deploys/639caf5f35b08200089373bd

@github-actions
Copy link

github-actions bot commented Oct 20, 2023

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 103 kB 38.9 kB 35 kB
vue.global.prod.js 161 kB 58.8 kB (+1 B) 52.3 kB (+25 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.8 kB 18.3 kB 16.8 kB
createApp 55 kB 21.4 kB 19.6 kB
createSSRApp 59.3 kB 23.1 kB 21.1 kB
defineCustomElement 60.6 kB 23.1 kB 21.1 kB
overall 69.3 kB 26.6 kB 24.2 kB

# Conflicts:
#	packages/compiler-core/src/parse.ts
@codspeed-hq
Copy link

codspeed-hq bot commented Dec 13, 2023

CodSpeed Performance Report

Merging #7359 will not alter performance

Comparing skirtles-code:colon-before-directive (949f7bf) with main (a6503e3)

Summary

✅ 53 untouched benchmarks

# Conflicts:
#	packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap
#	packages/compiler-core/__tests__/parse.spec.ts
#	packages/compiler-core/src/errors.ts
#	packages/compiler-core/src/parse.ts
#	packages/compiler-dom/src/errors.ts
#	packages/compiler-ssr/src/errors.ts
@edison1105 edison1105 added scope: compiler 🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. ready for review This PR requires more reviews labels Aug 20, 2024
@coderabbitai
Copy link

coderabbitai bot commented Nov 24, 2025

Walkthrough

A new warning detection mechanism is added to flag colon-prefixed directives with arguments starting with "v-", alongside corresponding error code enhancements and test infrastructure updates. Error codes are renumbered in DOM and SSR modules for alignment.

Changes

Cohort / File(s) Summary
Warning Detection & Error Code
packages/compiler-core/src/errors.ts, packages/compiler-core/src/parser.ts
Introduces X_COLON_BEFORE_DIRECTIVE error code and message; adds emitWarning() helper function and detection logic to warn when directive arguments start with "v-" in colon-prefixed directives during development.
Test Infrastructure Updates
packages/compiler-core/__tests__/parse.spec.ts
Extends test harness with optional warnings field in test patterns; adds warning spies and assertion logic to verify warnings alongside errors; includes new test case for X_COLON_BEFORE_DIRECTIVE.
Error Code Realignment
packages/compiler-dom/src/errors.ts, packages/compiler-ssr/src/errors.ts
Updates numeric enum values: X_V_HTML_NO_EXPRESSION (53 → 54) and X_SSR_UNSAFE_ATTR_NAME (65 → 66) for consistency.

Sequence Diagram

sequenceDiagram
    participant Parser
    participant Directive Handler
    participant Warning Handler
    participant onWarn Callback

    Parser->>Directive Handler: Parse directive argument
    Directive Handler->>Directive Handler: Check if colon-prefixed<br/>and arg starts with 'v-'
    alt Warning Condition Met
        Directive Handler->>Warning Handler: emitWarning(X_COLON_BEFORE_DIRECTIVE)
        Warning Handler->>onWarn Callback: Invoke with warning details
        onWarn Callback->>onWarn Callback: Collect warning
    end
    Directive Handler->>Parser: Continue parsing
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • Verify enum value changes: Confirm that renumbered error codes (53→54, 65→66) don't conflict with existing or reserved codes and are properly propagated
  • Test pattern validation: Review new test case for X_COLON_BEFORE_DIRECTIVE to ensure warning detection logic is correctly exercised
  • Warning emission logic: Examine the directive argument parsing path in parser.ts to confirm the "v-" prefix detection is correctly positioned and doesn't affect normal parsing

Poem

🐰 A colon walks into a directive, looking quite mean,
But we spot its mischief with warnings pristine,
"v-" prefixed tricks won't hide anymore,
The parser now whispers of what came before!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding a compiler warning for the :v- directive shorthand pattern, which is the primary objective across all modified files.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 24, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-sfc

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

@vue/compiler-ssr

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: 8415f76

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-core/__tests__/parse.spec.ts (1)

2429-2439: Expand test coverage for common mistake patterns.

The test correctly verifies the warning is emitted for :v-foo, but consider adding tests for the most common real-world mistakes that motivated this PR:

  1. :v-if, :v-for, :v-model, :v-show (common beginner mistakes mentioned in PR description)
  2. Verify that v-bind:v-foo does NOT emit a warning (per PR description: "using v-bind:v- is still allowed")
  3. Test location accuracy (currently tests offset 5, which should be the colon position)
  4. Edge cases like :v- with no directive name

Apply this diff to add comprehensive test coverage:

       X_COLON_BEFORE_DIRECTIVE: [
         {
           code: `<div :v-foo="obj" />`,
           warnings: [
             {
               type: ErrorCodes.X_COLON_BEFORE_DIRECTIVE,
               loc: { offset: 5, line: 1, column: 6 },
             },
           ],
         },
+        {
+          code: `<div :v-if="show" />`,
+          warnings: [
+            {
+              type: ErrorCodes.X_COLON_BEFORE_DIRECTIVE,
+              loc: { offset: 5, line: 1, column: 6 },
+            },
+          ],
+        },
+        {
+          code: `<div :v-for="item in items" />`,
+          warnings: [
+            {
+              type: ErrorCodes.X_COLON_BEFORE_DIRECTIVE,
+              loc: { offset: 5, line: 1, column: 6 },
+            },
+          ],
+        },
+        {
+          code: `<div :v-model="value" />`,
+          warnings: [
+            {
+              type: ErrorCodes.X_COLON_BEFORE_DIRECTIVE,
+              loc: { offset: 5, line: 1, column: 6 },
+            },
+          ],
+        },
+        {
+          code: `<div v-bind:v-foo="obj" />`,
+          warnings: [],
+        },
       ],
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5af3dd9 and 8415f76.

⛔ Files ignored due to path filters (1)
  • packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap is excluded by !**/*.snap
📒 Files selected for processing (5)
  • packages/compiler-core/__tests__/parse.spec.ts (2 hunks)
  • packages/compiler-core/src/errors.ts (2 hunks)
  • packages/compiler-core/src/parser.ts (2 hunks)
  • packages/compiler-dom/src/errors.ts (1 hunks)
  • packages/compiler-ssr/src/errors.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
packages/compiler-core/src/errors.ts (1)
packages/runtime-core/src/index.ts (1)
  • ErrorCodes (141-141)
packages/compiler-core/src/parser.ts (1)
packages/compiler-core/src/ast.ts (1)
  • DirectiveNode (193-211)
packages/compiler-core/__tests__/parse.spec.ts (2)
packages/compiler-core/src/ast.ts (1)
  • Position (83-87)
packages/compiler-core/src/parser.ts (1)
  • baseParse (1045-1096)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: test / e2e-test
🔇 Additional comments (7)
packages/compiler-dom/src/errors.ts (1)

24-24: LGTM! Mechanical renumbering to accommodate new core error code.

The change from 53 to 54 aligns with the new X_COLON_BEFORE_DIRECTIVE error code added to the core ErrorCodes enum. The runtime check at line 42 ensures this remains in sync with the core extension point.

packages/compiler-ssr/src/errors.ts (1)

20-20: LGTM! Cascading renumbering for SSR error codes.

The change from 65 to 66 accounts for the DOM error code renumbering, maintaining proper alignment with DOMErrorCodes.__EXTEND_POINT__. The runtime check at line 29 prevents collisions.

packages/compiler-core/src/errors.ts (1)

93-93: LGTM! New error code properly integrated.

The X_COLON_BEFORE_DIRECTIVE error code is well-positioned in the enum and has a clear message. The trailing colon in the message at line 183 correctly anticipates that the parser will append the specific attribute name when emitting the warning.

Also applies to: 183-183

packages/compiler-core/src/parser.ts (2)

1029-1033: LGTM! Helper function follows existing pattern.

The emitWarning function correctly mirrors the structure of emitError at line 1023, forwarding to currentOptions.onWarn with a properly constructed compiler error.


262-272: Clarify the dynamic argument edge case—it is already handled correctly.

The review's concern about dynamic arguments (point 1) is based on a misunderstanding. The code does catch cases like :[v-model]:

  1. The parser extracts the argument from brackets via arg.slice(1, -1), resulting in v-model (without brackets)
  2. The check arg.startsWith('v-') evaluates to true, and since rawName === ':', the warning is correctly emitted

The other shorthand suggestions (.v- and @v-) are not found in the codebase and fall outside the PR's scope—the warning intentionally targets only the : shorthand by checking rawName === ':'.

The message clarity suggestion (point 3) is valid but minor; rewording to "':${arg}' is probably a mistake. Did you mean '${arg}' instead?" would improve readability if desired.

Likely an incorrect or invalid review comment.

packages/compiler-core/__tests__/parse.spec.ts (2)

2558-2559: LGTM! Clean extension of test pattern structure.

The addition of the warnings? field alongside errors? maintains consistency with the existing pattern structure and allows tests to verify both error and warning behavior independently.


2444-2446: LGTM! Test infrastructure properly separates errors and warnings.

The implementation correctly:

  • Uses separate errorSpy and warnSpy instances
  • Passes both to baseParse via onError and onWarn options
  • Destructures with default empty arrays for backward compatibility
  • Asserts both errors and warnings independently

This allows existing tests to continue working while new tests can verify warning behavior.

Also applies to: 2453-2454, 2460-2461, 2470-2475

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 for review This PR requires more reviews scope: compiler

Projects

Status: Needs Review

Development

Successfully merging this pull request may close these issues.

2 participants