-
-
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
feat: add warning if some preset import is overridden #29971
base: main
Are you sure you want to change the base?
Conversation
Run & review this pull request in StackBlitz Codeflow. |
WalkthroughThe changes in this pull request focus on the Changes
Suggested labels
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: 3
🧹 Outside diff range and nitpick comments (1)
packages/nuxt/src/imports/module.ts (1)
135-136
: Use the Nuxt logger instead ofconsole.warn
.For consistency and better control over logging levels, consider using the Nuxt
logger.warn
method instead ofconsole.warn
.Apply this diff to update the logging method:
- console.warn(`[imports] "${i.as}" is already defined and auto imported from "${preset ?? 'unknown preset'}" within nuxt itself. Please don't name your composable the same as a preset, as it will lead to unexpected behavior.`) + logger.warn(`[imports] "${i.as}" is already defined and auto imported from "${preset ?? 'unknown preset'}" within Nuxt itself. Please avoid naming your composable the same as a preset to prevent unexpected behavior.`)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
packages/nuxt/src/imports/module.ts
(1 hunks)
🧰 Additional context used
🪛 GitHub Check: build
packages/nuxt/src/imports/module.ts
[failure] 131-131:
Argument of type 'InlinePreset | PresetImport' is not assignable to parameter of type 'string'.
[failure] 133-133:
Property 'as' does not exist on type 'ImportPresetWithDeprecation'.
[failure] 133-133:
Argument of type 'string' is not assignable to parameter of type 'ImportPresetWithDeprecation[][]'.
[failure] 137-137:
Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
🔇 Additional comments (2)
packages/nuxt/src/imports/module.ts (2)
128-129
:
Correct property access on preset
.
The property as
does not exist on type ImportPresetWithDeprecation
, leading to a TypeScript error. You might need to use an existing property like from
or name
.
Apply this diff to fix the property access:
- presetMap.set(preset.as, preset.from)
+ presetMap.set(preset.from, preset.from)
Likely invalid or redundant comment.
121-121
:
Fix the TypeScript type definition for presetMap
.
The current type definition new Map<string, typeof presets[]>()
is invalid TypeScript syntax. Using typeof presets[]
is incorrect. You should specify the correct type for the map values based on what you're storing.
Apply this diff to correct the type definition:
-const presetMap = new Map<string, typeof presets[]>()
+const presetMap = new Map<string, string>()
Likely invalid or redundant comment.
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 (2)
packages/nuxt/src/imports/module.ts (2)
132-137
: LGTM! Clear and helpful warning messageThe warning implementation effectively alerts developers about naming conflicts between their composables and preset imports. The message includes all necessary details: the conflicting name, the preset source, and the file location.
Consider adding a link to documentation about naming best practices:
- console.warn(`[imports] "${name}" is already defined and auto imported from "${preset ?? 'unknown preset'}" within nuxt itself. Please consider renaming "${name}" at ${i.from}.`) + console.warn(`[imports] "${name}" is already defined and auto imported from "${preset ?? 'unknown preset'}" within nuxt itself. Please consider renaming "${name}" at ${i.from}. See: https://nuxt.com/docs/guide/directory-structure/composables`)🧰 Tools
🪛 GitHub Check: build
[failure] 133-133:
Property 'as' does not exist on type 'ImportPresetWithDeprecation'.
[failure] 133-133:
Argument of type 'string' is not assignable to parameter of type 'ImportPresetWithDeprecation[][]'.
120-138
: Implementation successfully meets PR objectivesThe code effectively implements the warning system for preset import overrides as requested in issue #29258. The implementation:
- Tracks preset imports efficiently using a Map
- Provides clear warnings with actionable information
- Handles edge cases appropriately
Note: As mentioned in the PR description, this doesn't cover all cases (e.g., imports from
pages/module.ts
), but it's a solid first step.For future improvements:
- Consider extracting the warning logic into a separate function for better testability
- Add configuration options to customize or disable these warnings
- Extend the coverage to handle imports from additional locations
🧰 Tools
🪛 GitHub Check: build
[failure] 131-131:
Argument of type 'InlinePreset | PresetImport' is not assignable to parameter of type 'string'.
[failure] 133-133:
Property 'as' does not exist on type 'ImportPresetWithDeprecation'.
[failure] 133-133:
Argument of type 'string' is not assignable to parameter of type 'ImportPresetWithDeprecation[][]'.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
packages/nuxt/src/imports/module.ts
(1 hunks)
🧰 Additional context used
🪛 GitHub Check: build
packages/nuxt/src/imports/module.ts
[failure] 131-131:
Argument of type 'InlinePreset | PresetImport' is not assignable to parameter of type 'string'.
[failure] 133-133:
Property 'as' does not exist on type 'ImportPresetWithDeprecation'.
[failure] 133-133:
Argument of type 'string' is not assignable to parameter of type 'ImportPresetWithDeprecation[][]'.
preset.imports = preset.imports ?? [] | ||
for (const i of preset.imports) { | ||
presetMap.set(i, preset.from) | ||
} | ||
presetMap.set(preset.as, preset.from) |
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.
the previous implementation has type issues. types are weired on the presets. I tried to solve it like this, but seems kinda hacky
preset.imports = preset.imports ?? [] | |
for (const i of preset.imports) { | |
presetMap.set(i, preset.from) | |
} | |
presetMap.set(preset.as, preset.from) | |
for (const i of preset.imports) { | |
if(typeof i === "string") { | |
presetMap.set(i, preset.from) | |
continue | |
} | |
let name, as, from | |
if(Array.isArray(i)) { | |
[name, as, from] = i | |
} | |
else { | |
name = ('name' in i) ? i.name : undefined | |
as = ('as' in i) ? i.as : undefined | |
from = preset.from | |
} | |
if(!name || !from) { | |
continue | |
} | |
presetMap.set(as ?? name, from) | |
} |
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: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
packages/nuxt/src/imports/module.ts
(1 hunks)
🧰 Additional context used
🪛 GitHub Check: build
packages/nuxt/src/imports/module.ts
[failure] 131-131:
Argument of type 'InlinePreset | PresetImport' is not assignable to parameter of type 'string'.
[failure] 133-133:
Property 'as' does not exist on type 'ImportPresetWithDeprecation'.
const name = i.as ?? i.name | ||
const preset = presetMap.get(name) | ||
|
||
if (preset) { | ||
console.warn(`[imports] "${name}" is already defined and auto imported from "${preset ?? 'unknown preset'}" within nuxt itself. Please consider renaming "${name}" at ${i.from}.`) | ||
} |
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.
🛠️ Refactor suggestion
Enhance the warning implementation.
While the warning implementation works, it could be improved for better developer experience and code consistency:
- Use the Nuxt logger utility for consistent warning format
- Enhance code structure with early returns
- Make the warning message more actionable
Apply this diff to implement these improvements:
-const name = i.as ?? i.name
-const preset = presetMap.get(name)
-
-if (preset) {
- console.warn(`[imports] "${name}" is already defined and auto imported from "${preset ?? 'unknown preset'}" within nuxt itself. Please consider renaming "${name}" at ${i.from}.`)
-}
+const importName = i.as ?? i.name
+const presetSource = presetMap.get(importName)
+if (!presetSource) {
+ continue
+}
+
+logger.warn(
+ `Import conflict: "${importName}" is already auto-imported from preset "${presetSource}"\n` +
+ ` Source: ${i.from}\n` +
+ ` Solution: Consider renaming the import or using a different name via 'as' syntax`
+)
Committable suggestion skipped: line range outside the PR's diff.
🧰 Tools
🪛 GitHub Check: build
[failure] 133-133:
Property 'as' does not exist on type 'ImportPresetWithDeprecation'.
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.
I think what we want is:
- not to warn if using an import from a layer
- only warn if overriding with an equal
priority
- this way modules can intentionally override without warnings
I can try. This shouldn't be possible in src/imports/module, should it? Any advise? |
🔗 Linked issue
#29258
📚 Description
This pr adds a console.warn if the /composables or /utils override some imports from the defined presets.
It implements this in packages/nuxt/src/imports/module.ts (as suggested in #29258). This is not perfect. This pr does not cover/warn about imports from https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/pages/module.ts#L355, for example.
The text of the warning can of course still be adapted/updated.
I have done my best to test it. I tried to stubGlobal('console') but had no luck so far.
output if adding "useState" at
/composables
Summary by CodeRabbit
New Features
presetMap
for better tracking of imported presets.Bug Fixes
Chores