-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
fix(nuxt): assign default name to component without setup #29869
Conversation
Run & review this pull request in StackBlitz Codeflow. |
WalkthroughThe pull request modifies the Changes
Assessment against linked issues
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (1)
packages/nuxt/src/components/plugins/component-names.ts (1)
35-47
: Consider adding tests for the new AST-based name injectionThe changes introduce a new code path for handling components without
__name
. This should be covered by tests.Would you like me to help create test cases for:
- Components without setup/
__name
- Error handling scenarios
- Multiple export default edge case
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
packages/nuxt/src/components/plugins/component-names.ts
(2 hunks)
🔇 Additional comments (3)
packages/nuxt/src/components/plugins/component-names.ts (3)
4-4
: LGTM: Type import for AST handling
The addition of the Program
type import from acorn is appropriate for type-safe AST manipulation.
36-40
: LGTM: Existing name replacement logic
The handling of components with __name
is maintained and works correctly, using a precise regex replacement.
35-47
: Verify the impact on Vue Devtools display
The changes look good for injecting component names, but we should verify that this actually fixes the Vue Devtools display issue.
Let's check for any similar patterns in the codebase:
const ast = this.parse(code) as Program | ||
const exportDefault = ast.body.find(node => node.type === 'ExportDefaultDeclaration') | ||
if (exportDefault) { | ||
const { start, end } = exportDefault.declaration | ||
s.overwrite(start, end, `Object.assign(${code.slice(start, end)}, { name: ${JSON.stringify(component.pascalName)} })`) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add error handling for AST parsing and edge cases
While the AST-based approach is a good solution, there are some concerns:
- AST parsing could fail and should be handled gracefully
- The code assumes a single export default declaration
- Direct string manipulation with indices could be fragile
Consider applying this safer implementation:
- const ast = this.parse(code) as Program
- const exportDefault = ast.body.find(node => node.type === 'ExportDefaultDeclaration')
- if (exportDefault) {
- const { start, end } = exportDefault.declaration
- s.overwrite(start, end, `Object.assign(${code.slice(start, end)}, { name: ${JSON.stringify(component.pascalName)} })`)
- }
+ try {
+ const ast = this.parse(code) as Program
+ const exportDefaults = ast.body.filter(node => node.type === 'ExportDefaultDeclaration')
+
+ if (exportDefaults.length > 1) {
+ console.warn(`Multiple export defaults found in ${id}, skipping name injection`)
+ return
+ }
+
+ const exportDefault = exportDefaults[0]
+ if (exportDefault?.declaration) {
+ const { start, end } = exportDefault.declaration
+ const originalExport = code.slice(start, end)
+ s.overwrite(
+ start,
+ end,
+ `Object.assign(${originalExport}, { name: ${JSON.stringify(component.pascalName)} })`
+ )
+ }
+ } catch (error) {
+ console.warn(`Failed to parse ${id} for name injection:`, error)
+ return
+ }
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const ast = this.parse(code) as Program | |
const exportDefault = ast.body.find(node => node.type === 'ExportDefaultDeclaration') | |
if (exportDefault) { | |
const { start, end } = exportDefault.declaration | |
s.overwrite(start, end, `Object.assign(${code.slice(start, end)}, { name: ${JSON.stringify(component.pascalName)} })`) | |
} | |
} | |
try { | |
const ast = this.parse(code) as Program | |
const exportDefaults = ast.body.filter(node => node.type === 'ExportDefaultDeclaration') | |
if (exportDefaults.length > 1) { | |
console.warn(`Multiple export defaults found in ${id}, skipping name injection`) | |
return | |
} | |
const exportDefault = exportDefaults[0] | |
if (exportDefault?.declaration) { | |
const { start, end } = exportDefault.declaration | |
const originalExport = code.slice(start, end) | |
s.overwrite( | |
start, | |
end, | |
`Object.assign(${originalExport}, { name: ${JSON.stringify(component.pascalName)} })` | |
) | |
} | |
} catch (error) { | |
console.warn(`Failed to parse ${id} for name injection:`, error) | |
return | |
} |
There was a problem hiding this 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
🧹 Outside diff range and nitpick comments (2)
packages/nuxt/src/components/plugins/component-names.ts (2)
36-39
: Consider making the name replacement more robustWhile the current implementation works, it could be more resilient to different code formatting styles.
Consider this more flexible approach:
- const NAME_RE = new RegExp(`__name:\\s*['"]${filename}['"]`) + const NAME_RE = new RegExp(`__name:\\s*['"](${filename}|[^'"]*?)['"]`)This would handle cases where the existing name might not exactly match the filename.
35-47
: Consider unifying the component name injection strategyThe current implementation uses two different strategies (regex-based and AST-based) depending on the presence of
__name
. Consider using AST parsing consistently for both cases, which would:
- Provide more reliable code manipulation
- Make the code more maintainable
- Allow for better error handling
Would you like me to provide an example of a unified AST-based approach?
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
packages/nuxt/src/components/plugins/component-names.ts
(2 hunks)
🔇 Additional comments (2)
packages/nuxt/src/components/plugins/component-names.ts (2)
4-4
: LGTM: Type import for AST parsing
The Program type import from acorn provides proper type safety for AST operations.
41-46
: Critical: Add error handling and edge case protection
The AST-based transformation needs additional safety measures as highlighted in the previous review.
Let's verify potential edge cases in the codebase:
#!/bin/bash
# Description: Check for components with multiple export defaults or complex export patterns
# that might cause issues with the current implementation
# Look for files with multiple export default declarations
echo "Checking for multiple export defaults..."
rg "export\s+default" -A 2 -B 2
# Look for dynamic exports that might cause issues
echo "Checking for dynamic exports..."
ast-grep --pattern 'export default $$$'
🔗 Linked issue
Fix #29843
📚 Description
Hi 👋 this PR adds __name if the component does not have a setup function.
Summary by CodeRabbit
New Features
Bug Fixes