Skip to content

Conversation

edison1105
Copy link
Member

@edison1105 edison1105 commented Sep 26, 2025

close #13930

Summary by CodeRabbit

  • Bug Fixes

    • Treats effectively empty shorthand bindings as missing expressions in browser in-DOM scenarios, avoiding confusing results and improving robustness without changing public APIs.
    • Limits a development-only decode/entity runtime check so it no longer triggers in test environments, reducing false errors during testing.
  • Tests

    • Adds a test covering shorthand no-expression behavior for in-DOM templates in browser-like conditions.

Copy link

coderabbitai bot commented Sep 26, 2025

Walkthrough

Adds a browser-only branch treating a present SIMPLE_EXPRESSION with empty content as equivalent to a missing v-bind expression for shorthand in in‑DOM templates; narrows a parser runtime decodeEntities check to skip test environments; adds a test for the in‑DOM shorthand case. No public API changes.

Changes

Cohort / File(s) Summary of changes
v-bind shorthand transform
packages/compiler-core/src/transforms/transformVBindShorthand.ts
Add a browser-only branch that treats a SIMPLE_EXPRESSION with empty content as absent for shorthand v-bind (:foo="" normalized by browser), routing it into the existing missing/invalid-argument handling.
Parser runtime check
packages/compiler-core/src/parser.ts
Restrict the browser-only decodeEntities runtime check to skip test environments by adding a !__TEST__ guard.
Tests
packages/compiler-core/__tests__/transforms/vBind.spec.ts
Add "no expression (shorthand) in-DOM template" test that sets __BROWSER__ and verifies shorthand v-bind shape for :id with an empty expression.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant BrowserDOM as In‑DOM template (browser)
  participant Parser as baseParse
  participant Transform as transformVBindShorthand
  participant Reporter as Diagnostic Reporter

  BrowserDOM->>Parser: parse attributes (shorthand v-bind)
  Parser->>Transform: directive node (argument + expression)
  alt missing expression OR (SIMPLE_EXPRESSION && empty content && __BROWSER__ && !__TEST__)
    Transform->>Reporter: emit missing/invalid-argument diagnostic
    Reporter-->>Transform: diagnostic recorded
    Transform-->>Parser: treat as no-expression shorthand (error path)
  else valid expression
    Transform-->>Parser: normal shorthand transform -> ObjectExpression properties
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

scope: compiler, :hammer: p3-minor-bug

Suggested reviewers

  • baiwusanyu-c

Poem

I nibble parser roots beneath the sod,
Found empty binds where browsers trod,
A tiny hop, a gentle seam—
Now shorthand binds no longer scream,
Hooray — I fixed the in‑DOM dream. 🐰✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Out of Scope Changes Check ⚠️ Warning The modification in packages/compiler-core/src/parser.ts alters the decodeEntities runtime check by excluding test environments via TEST, which does not relate to fixing the v-bind shorthand crash in in-DOM templates and introduces behavior outside the stated objectives of issue #13930. To keep the PR focused on the v-bind shorthand fix, extract the parser.ts change into a separate pull request or clearly document its necessity for test compatibility; this will maintain a clear separation of concerns and ensure the bug fix remains isolated.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title “fix(compiler-core): fix v-bind shorthand handling for in-DOM templates” succinctly describes the primary change, namely correcting how the compiler handles empty v-bind shorthands in browser‐parsed templates, and it directly aligns with the bug being addressed without extraneous detail.
Linked Issues Check ✅ Passed The transformVBindShorthand change adds a browser‐only branch to treat a SimpleExpression with empty content as absent, preventing the TypeError in in-DOM templates as described in issue #13930, and the new test in vBind.spec.ts confirms the expected behavior under BROWSER, fully satisfying the linked issue’s objective to restore the v-bind shorthand functionality without a crash.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch edison/fix/13930

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

Copy link

github-actions bot commented Sep 26, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 102 kB 38.6 kB 34.8 kB
vue.global.prod.js 160 kB (+41 B) 58.7 kB (+13 B) 52.2 kB (+1 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.7 kB 18.3 kB 16.7 kB
createApp 54.7 kB 21.3 kB 19.5 kB
createSSRApp 58.9 kB 23 kB 21 kB
defineCustomElement 60 kB 23 kB 21 kB
overall 68.8 kB 26.5 kB 24.2 kB

Copy link

pkg-pr-new bot commented Sep 26, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-sfc

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

@vue/compiler-ssr

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: 338b236

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/src/transforms/transformVBindShorthand.ts (1)

18-20: Also treat whitespace-only values as empty

In some in-DOM parses, attribute values can be whitespace-only. Trim before checking emptiness to make the fix more robust.

Apply this diff:

-        (!prop.exp ||
-          (prop.exp.type === NodeTypes.SIMPLE_EXPRESSION && !prop.exp.content))
+        (!prop.exp ||
+          (prop.exp.type === NodeTypes.SIMPLE_EXPRESSION &&
+            prop.exp.content.trim().length === 0))
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5a8aa0b and 1d35971.

📒 Files selected for processing (1)
  • packages/compiler-core/src/transforms/transformVBindShorthand.ts (1 hunks)
⏰ 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). (3)
  • GitHub Check: Redirect rules
  • GitHub Check: Header rules
  • GitHub Check: Pages changed
🔇 Additional comments (1)
packages/compiler-core/src/transforms/transformVBindShorthand.ts (1)

18-20: Good fix: handles empty SIMPLE_EXPRESSION to avoid in-DOM crash

Extending the condition to treat an empty simple expression as absent prevents the TypeError in in-DOM templates. Short‑circuiting keeps .type access safe.

Please add a regression test for in-DOM templates covering:

to ensure both parse to same-name shorthand without crashing.

@edison1105 edison1105 changed the title fix(compiler-core): correctly handle empty expression checks for v-bind shorthand fix(compiler-core): fix v-bind shorthand handling in in-DOM templates Sep 26, 2025
@edison1105 edison1105 marked this pull request as draft September 26, 2025 14:05
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__/transforms/vBind.spec.ts (1)

115-134: Restore the previous __BROWSER__ flag value

Because __BROWSER__ is a shared global across the suite, forcing it back to false could break any test that intentionally left it true. Cache the old value and restore it in finally to keep the suite isolated.

-    try {
-      __BROWSER__ = true
+    const prevBrowser = __BROWSER__
+    try {
+      __BROWSER__ = true
       // in-DOM templates will be parsed by the browser into :id=""
       const node = parseWithVBind(`<div :id="" />`)
       const props = (node.codegenNode as VNodeCall).props as ObjectExpression
       expect(props.properties[0]).toMatchObject({
         key: {
           content: `id`,
           isStatic: true,
         },
         value: {
           content: `id`,
           isStatic: false,
         },
       })
     } finally {
-      __BROWSER__ = false
+      __BROWSER__ = prevBrowser
     }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4685dac and 8536f89.

📒 Files selected for processing (2)
  • packages/compiler-core/__tests__/transforms/vBind.spec.ts (1 hunks)
  • packages/compiler-core/src/parser.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/compiler-core/__tests__/transforms/vBind.spec.ts (1)
packages/compiler-core/src/ast.ts (2)
  • VNodeCall (323-341)
  • ObjectExpression (374-377)
⏰ 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). (3)
  • GitHub Check: Redirect rules
  • GitHub Check: Header rules
  • GitHub Check: Pages changed
🔇 Additional comments (1)
packages/compiler-core/src/parser.ts (1)

1057-1060: Decode-entities guard still enforces runtime expectations

Thanks for tightening the condition to skip only under __TEST__; this keeps real browser builds protected while letting tests flip __BROWSER__ without tripping the check. Looks good.

@edison1105 edison1105 marked this pull request as ready for review September 26, 2025 14:09
@edison1105 edison1105 changed the title fix(compiler-core): fix v-bind shorthand handling in in-DOM templates fix(compiler-core): fix v-bind shorthand handling for in-DOM templates Sep 28, 2025
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__/transforms/vBind.spec.ts (1)

115-134: Restore __BROWSER__ to its original value

We shouldn’t assume the ambient __BROWSER__ flag starts as false. If another spec toggles it to true, this test will force it back to false and leak state across suites. Capture the current value and restore it in the finally block to keep the test isolated.

-  test('no expression (shorthand) in-DOM templates', () => {
-    try {
-      __BROWSER__ = true
+  test('no expression (shorthand) in-DOM templates', () => {
+    const prevBrowser = __BROWSER__
+    try {
+      __BROWSER__ = true
@@
-    } finally {
-      __BROWSER__ = false
+    } finally {
+      __BROWSER__ = prevBrowser
     }
   })
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8536f89 and 338b236.

📒 Files selected for processing (2)
  • packages/compiler-core/__tests__/transforms/vBind.spec.ts (1 hunks)
  • packages/compiler-core/src/transforms/transformVBindShorthand.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/compiler-core/__tests__/transforms/vBind.spec.ts (1)
packages/compiler-core/src/ast.ts (2)
  • VNodeCall (323-341)
  • ObjectExpression (374-377)
⏰ 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). (3)
  • GitHub Check: Redirect rules
  • GitHub Check: Header rules
  • GitHub Check: Pages changed

@edison1105 edison1105 added ready to merge The PR is ready to be merged. 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. regression and removed regression labels Sep 28, 2025
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

v-bind's same-name shorthand fails (causes crash) in in-DOM templates
1 participant