core icon indicating copy to clipboard operation
core copied to clipboard

refactor(compiler): add dedicated transform for vbind shorthand

Open edison1105 opened this issue 6 months ago β€’ 5 comments

close #13169 close #13170 close #11321 close #12298 close #12828

use tests from #13170 and #12298 and #12828

The current handing logic for v-bind shorthand is rather scattered, which results in many edge cases where transformBindShorthand needs to be called additionally. these cases occur because vbind shorthand has not been processed when reading properties from props.

This PR introduces a dedicated transform specifically for handling v-bind shorthand, and makes it the first transform to be called. This ensures that vbind shorthand has already been processed before any subsequent logic read from props


Special handling is required for double bind in vue-macros. PR https://github.com/vue-macros/vue-macros/pull/960

Summary by CodeRabbit

  • New Features

    • Added support for shorthand v-bind syntax (e.g., :prop) across the template compiler, including server-side rendering.
    • Exposed the shorthand v-bind transform for public use.
  • Bug Fixes

    • Correctly handles shorthand v-bind directives in combination with other directives like v-if, v-for, and v-model.
  • Tests

    • Added and updated test cases to ensure proper handling of shorthand v-bind syntax in various scenarios, including dynamic tags and v-model.
  • Documentation

    • No end-user documentation changes included.
  • Chores

    • Minor internal refactoring and public export of a utility regular expression.

edison1105 avatar Jun 05 '25 06:06 edison1105

Walkthrough

Added a new NodeTransform transformVBindShorthand that expands same-name shorthand v-bind directives and integrated it into compiler-core, compiler-dom tests, and SSR compile pipelines; removed inline same-name shorthand handling from vBind/vFor; updated tests and exported a utility regex used by the new transform.

Changes

Cohort / File(s) Change Summary
New transform
packages/compiler-core/src/transforms/transformVBindShorthand.ts
Added transformVBindShorthand NodeTransform to expand same-name shorthand v-bind (e.g. :type) into explicit expressions, camelize args, and report invalid-arg errors.
Core integration & export
packages/compiler-core/src/compile.ts, packages/compiler-core/src/index.ts
Imported and added transformVBindShorthand as the first node transform in the base preset; exported it from core index.
vBind refactor
packages/compiler-core/src/transforms/vBind.ts
Removed previous same-name shorthand helper/logic (transformBindShorthand); adjusted empty-expression handling and emission paths for v-bind.
vFor adjustments
packages/compiler-core/src/transforms/vFor.ts
Removed import/use of removed transformBindShorthand for resolving shorthand :key.
Utilities export
packages/compiler-core/src/utils.ts
Exported validFirstIdentCharRE with explicit RegExp type annotation.
SSR integration
packages/compiler-ssr/src/index.ts
Imported and added transformVBindShorthand to SSR compile nodeTransforms (first).
Tests β€” include transform
packages/compiler-core/__tests__/transforms/vBind.spec.ts, packages/compiler-core/__tests__/transforms/vFor.spec.ts, packages/compiler-core/__tests__/transforms/vIf.spec.ts, packages/compiler-dom/__tests__/transforms/vModel.spec.ts
Test helpers updated to include transformVBindShorthand in nodeTransforms; new/updated tests for shorthand :key, v-model + shorthand :type, and related cases.
Tests β€” SSR
packages/compiler-ssr/__tests__/ssrTransitionGroup.spec.ts
Added test "with dynamic tag shorthand" asserting SSR output for shorthand dynamic :tag.

Sequence Diagram(s)

sequenceDiagram
    participant Parser
    participant AST
    participant transformVBindShorthand as ShorthandTransform
    participant OtherTransforms

    Parser->>AST: parse template β†’ AST
    AST->>ShorthandTransform: apply same-name v-bind expansion (first)
    ShorthandTransform-->>AST: attach expression / report errors
    AST->>OtherTransforms: continue with v-if, v-for, v-model, element transforms
    OtherTransforms-->>AST: further node mutations (userKey, key resolution, helpers)
    note right of ShorthandTransform #dff0d8: Shorthand handled before other transforms

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Suggested labels

ready to merge, :broom: p1-chore

Suggested reviewers

  • yyx990803

Poem

"I’m a rabbit with a tiny hop, turning :type into a line,
I camel-case the whispered name and make the bindings fine.
Tests clap paws, SSR sings, v-if finds its keyβ€”
A nimble change, a tidy leap, bindings happy as can be. πŸ₯•"

Pre-merge checks and finishing touches

βœ… Passed checks (5 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues Check βœ… Passed The PR centralizes same-name v-bind shorthand processing by adding transformVBindShorthand, adding it to the base transform preset and SSR pipeline, removing the old inline shorthand handling, and updating tests in compiler-core, compiler-dom, and compiler-ssr; the changes introduce the shorthand expansion, validation, and camelization logic and add tests for v-model/type, :key on v-if, and SSR dynamic tag cases, which correspond to the linked issues for radio/v-model type fixes [#13169,#13170], v-if :key parsing [#11321,#12298], and SSR shorthand handling [#12828].
Out of Scope Changes Check βœ… Passed All modified files and additions are directly related to centralizing v-bind shorthand handling: new transform implementation, integrating it into core/dom/ssr pipelines, removing the old helper usage, and updating tests; the only ancillary change is exporting a regex utility used for argument validation, which supports the new transform and is in-scope.
Docstring Coverage βœ… Passed No functions found in the changes. Docstring coverage check skipped.
Title Check βœ… Passed The title succinctly and accurately summarizes the primary change: adding a dedicated v-bind shorthand transform to the compiler, which matches the PR's additions of transformVBindShorthand and related test/compile updates. It is concise, scoped with a conventional-commit prefix, and specific enough for reviewers to understand the main change at a glance.
✨ 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/refactor/vBindShorthand

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

coderabbitai[bot] avatar Jun 05 '25 06:06 coderabbitai[bot]

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 102 kB 38.6 kB 34.7 kB
vue.global.prod.js 160 kB (+66 B) 58.7 kB (+14 B) 52.2 kB (-51 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 20.9 kB
overall 68.8 kB 26.5 kB 24.2 kB

github-actions[bot] avatar Jun 05 '25 06:06 github-actions[bot]

Open in StackBlitz

@vue/compiler-core

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

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

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

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

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

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

npm i https://pkg.pr.new/@vue/runtime-dom@13438
@vue/server-renderer

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

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

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

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

commit: ecd1d8b

pkg-pr-new[bot] avatar Jun 05 '25 06:06 pkg-pr-new[bot]

/ecosystem-ci run

edison1105 avatar Sep 24 '25 13:09 edison1105

πŸ“ Ran ecosystem CI: Open

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

vue-bot avatar Sep 24 '25 13:09 vue-bot