Skip to content

Commit

Permalink
fix: allow single name file under prefixed components folder, fix #419
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 9, 2024
1 parent 8a065c7 commit cd0157c
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 11 deletions.
3 changes: 3 additions & 0 deletions eslint.config.js
Expand Up @@ -11,6 +11,9 @@ export default createConfigForNuxt({
'playground',
'docs',
],
componentsPrefixed: [
'playground/components-prefixed',
],
},
})
.append(
Expand Down
6 changes: 4 additions & 2 deletions packages/eslint-config/src/flat/configs/disables.ts
Expand Up @@ -9,7 +9,7 @@ export default function disables(options: NuxtESLintConfigOptions): Linter.FlatC
const dirs = resolved.dirs
const nestedGlobPattern = `**/*.${GLOB_EXTS}`

const fileRoutes = [
const fileRoutes = [...new Set([
// These files must have one-word names as they have a special meaning in Nuxt.
...dirs.src.flatMap(layersDir => [
join(layersDir, `app.${GLOB_EXTS}`),
Expand All @@ -22,7 +22,9 @@ export default function disables(options: NuxtESLintConfigOptions): Linter.FlatC

// These files should have multiple words in their names as they are within subdirectories.
...(dirs.components.map(componentsDir => join(componentsDir, '*', nestedGlobPattern)) || []),
]
// Prefixed components can have one-word names in file
...(dirs.componentsPrefixed.map(componentsDir => join(componentsDir, nestedGlobPattern)) || []),
])]

const configs: Linter.FlatConfig[] = []

Expand Down
6 changes: 6 additions & 0 deletions packages/eslint-config/src/flat/types.ts
Expand Up @@ -70,6 +70,12 @@ export interface NuxtESLintConfigOptions {
*/
components?: string[]

/**
* Directory for components with prefix
* Ignore `vue/multi-word-component-names`
*/
componentsPrefixed?: string[]

/**
* Directory for composobles
*/
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-config/src/flat/utils.ts
Expand Up @@ -25,6 +25,7 @@ export function resolveOptions(
dirs.modules ||= dirs.src.map(src => `${src}/modules`)
dirs.middleware ||= dirs.src.map(src => `${src}/middleware`)
dirs.servers ||= dirs.src.map(src => `${src}/servers`)
dirs.componentsPrefixed ||= []

const resolved: NuxtESLintConfigOptionsResolved = {
features: {
Expand Down
22 changes: 13 additions & 9 deletions packages/module/src/modules/config/utils.ts
Expand Up @@ -7,6 +7,7 @@ export function getDirs(nuxt: Nuxt): NuxtESLintConfigOptions['dirs'] {
pages: [],
composables: [],
components: [],
componentsPrefixed: [],
layouts: [],
plugins: [],
middleware: [],
Expand All @@ -17,7 +18,7 @@ export function getDirs(nuxt: Nuxt): NuxtESLintConfigOptions['dirs'] {
}

for (const layer of nuxt.options._layers) {
const r = (t: string) => relative(nuxt.options.rootDir, resolve(layer.config.srcDir, t))
const r = (t: string) => relative(nuxt.options.rootDir, resolve(layer.config.srcDir, t.replace(/^~[/\\]/, '')))

dirs.src.push(r(''))
dirs.pages.push(r(nuxt.options.dir.pages || 'pages'))
Expand All @@ -33,14 +34,17 @@ export function getDirs(nuxt: Nuxt): NuxtESLintConfigOptions['dirs'] {
dirs.composables.push(r(dir))
}

if (layer.config.components) {
const options = layer.config.components || {}
if (options !== true && 'dirs' in options) {
for (const dir of options.dirs || []) {
if (typeof dir === 'string')
dirs.components.push(r(dir))
else if (dir && 'path' in dir && typeof dir.path === 'string')
dirs.components.push(r(dir.path))
if (layer.config.components && layer.config.components !== true) {
const options = Array.isArray(layer.config.components)
? { dirs: layer.config.components }
: layer.config.components
for (const dir of options.dirs || []) {
if (typeof dir === 'string')
dirs.components.push(r(dir))
else if (dir && 'path' in dir && typeof dir.path === 'string') {
dirs.components.push(r(dir.path))
if (dir.prefix)
dirs.componentsPrefixed.push(r(dir.path))
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions playground/components-prefixed/A.vue
@@ -0,0 +1,8 @@
<script setup lang="ts">
</script>

<template>
<div>
<slot />
</div>
</template>
8 changes: 8 additions & 0 deletions playground/components/TheB.vue
@@ -0,0 +1,8 @@
<script setup lang="ts">
</script>

<template>
<div>
<slot />
</div>
</template>
4 changes: 4 additions & 0 deletions playground/nuxt.config.ts
Expand Up @@ -5,6 +5,10 @@ export default defineNuxtConfig({
devtools: {
enabled: true,
},
components: [
'~/components',
{ path: '~/components-prefixed', prefix: 'Prefix' },
],
eslint: {
config: {
stylistic: true,
Expand Down

0 comments on commit cd0157c

Please sign in to comment.