core icon indicating copy to clipboard operation
core copied to clipboard

dx(runtime-core): check current and parent components in formatComponentName

Open skirtles-code opened this issue 3 years ago β€’ 5 comments

formatComponentName() already checks the components option for both the current component and the parent component, searching for the component name. However, the current logic will only search the parent if the current component doesn't have a components option.

The following example intentionally logs a warning, but in the warning message the name Anonymous is used, rather than MyChild. If the redundant components: {} option is removed, the warning successfully identifies the name of the component.

https://jsfiddle.net/skirtle/abnvzju0/

This PR changes the logic in formatComponentName() so that the parent will still be checked even if the child has a components option of its own, assuming the component's name hasn't already been found.

The PR also includes:

  • Tests for formatComponentName().
  • A tweak to use formatComponentName() for a warning elsewhere in the same file.
  • The instance.parent check has been moved so that it is only applied in the code branch that needs instance.parent.

skirtles-code avatar Nov 27 '22 02:11 skirtles-code

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 52.3 kB

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

github-actions[bot] avatar Oct 20 '23 11:10 github-actions[bot]

Open in StackBlitz

@vue/compiler-core

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

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

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

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

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

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

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

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

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

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

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

commit: 3230e0d

pkg-pr-new[bot] avatar Aug 17 '24 18:08 pkg-pr-new[bot]

/ecosystem-ci run

edison1105 avatar Aug 20 '24 01:08 edison1105

πŸ“ Ran ecosystem CI: Open

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

vue-bot avatar Aug 20 '24 01:08 vue-bot

The failure of ecosystem-ci has nothing to do with this PR

edison1105 avatar Aug 20 '24 02:08 edison1105

Walkthrough

This PR enhances component name inference in Vue's runtime-core by introducing comprehensive tests for the formatComponentName function and improving its implementation to handle name resolution more robustly, including relaxing null-checks and safely handling registry lookups.

Changes

Cohort / File(s) Summary
Component Name Formatting Tests
packages/runtime-core/__tests__/component.spec.ts
Adds comprehensive test suite for formatComponentName covering default names, name options, recursive components, parent inference, functional components, global components, and name precedence across various configurations
Component Runtime Implementation
packages/runtime-core/src/component.ts
Improves formatComponentName calls in async setup warnings, relaxes null-checks on instance parameter to only require instance (not parent), and safely handles registry lookups by allowing null values

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Test file: Comprehensive coverage with multiple scenarios, but tests follow consistent patterns for a single function
  • Implementation changes: Small focused edits, but require careful understanding of name resolution logic and null-check implications
  • Areas requiring attention:
    • Registry lookup null-safety changes and their interaction with inferFromRegistry
    • Null-check relaxation on instance parameter and implications for parent-dependent name inference
    • Test coverage adequacy for new null-handling behavior

Poem

🐰 A rabbit hops with glee so bright,
Component names now shine just right!
From null-checks loose to registries deep,
Identity inferenceβ€”a promise we keep!
Tests bloom in rows, a garden of care,
Naming precision floats through the air. πŸŽͺ

Pre-merge checks and finishing touches

βœ… Passed checks (3 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The title accurately describes the main change: checking current and parent components in formatComponentName function to improve component name inference.
Docstring Coverage βœ… Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • [ ] πŸ“ Generate docstrings
πŸ§ͺ Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

πŸ“œ Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 5af3dd9b4546252222bb2e4d80499ba37cead03b and 3230e0d96061a3784038d95f439b660f50fbdcda.

πŸ“’ Files selected for processing (2)
  • packages/runtime-core/__tests__/component.spec.ts (1 hunks)
  • packages/runtime-core/src/component.ts (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/runtime-core/__tests__/component.spec.ts (1)
packages/runtime-core/src/component.ts (1)
  • formatComponentName (1219-1253)
⏰ 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 (3)
packages/runtime-core/src/component.ts (2)

900-900: LGTM! Good consistency improvement.

Using formatComponentName here ensures consistent component name resolution across all warning messages, including inference from parent registries and file-based names.


1232-1249: LGTM! This correctly fixes the parent registry lookup issue.

The refactored logic properly searches all three registries in order (child components β†’ parent components β†’ appContext components) rather than selecting just one registry. The key improvements are:

  1. Relaxed null-check (line 1232): Enables name inference even without a parent instance.
  2. Safe registry helper (lines 1234-1236): The inferFromRegistry helper correctly accepts undefined | null and handles them gracefully via the for...in loop.
  3. Sequential search (lines 1244-1249): Each registry is searched independently, so a child with an empty components: {} no longer prevents checking the parent's registry.

This resolves the "Anonymous" display issue described in the PR objectives.

packages/runtime-core/__tests__/component.spec.ts (1)

1-157: LGTM! Excellent test coverage for formatComponentName.

This comprehensive test suite validates all the key scenarios:

  • Parent registry lookup (lines 57-76) directly tests the fix for the "Anonymous" issue when a child has components: {}
  • Name precedence (lines 117-156) confirms the correct resolution order: name option β†’ __name β†’ __file β†’ child components β†’ parent components β†’ appContext components
  • Edge cases like self-recursive components and functional components are well-covered

The tests properly exercise the refactored logic in component.ts and provide strong regression protection.


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.

coderabbitai[bot] avatar Nov 24 '25 06:11 coderabbitai[bot]