Skip to content

Commit aa93f6b

Browse files
committed
feat: add isTestFile utility and update file-component-naming rule to skip test files
1 parent 5149754 commit aa93f6b

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

src/rules/file-component-naming.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path'
2-
import { parseRuleOptions, toPascalCase, isComponent, isFileIgnored, isOutsideSrc } from '../utils'
2+
import { parseRuleOptions, toPascalCase, isComponent, isFileIgnored, isOutsideSrc, isTestFile } from '../utils'
33

44
const defaultOptions = {
55
src: 'src', // Base source directory
@@ -34,6 +34,7 @@ export default {
3434
const filename = context.getFilename()
3535
const { src, ignore } = parseRuleOptions(context, defaultOptions)
3636

37+
if (isTestFile(filename)) return {}
3738
if (!isComponent(filename)) return {}
3839
if (isFileIgnored(filename, ignore)) return {}
3940
if (isOutsideSrc(filename, src)) return {}

src/utils.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function toPascalCase(name) {
2929
// Utility to check if a file is a Vue component based on its path
3030
export function isComponent(filename) {
3131
if (!filename) return false
32-
const lower = filename.toLowerCase()
32+
const lower = String(filename).toLowerCase()
3333
return lower.endsWith('.vue') || lower.includes('.component.') || lower.includes('.comp.')
3434
}
3535

@@ -46,3 +46,10 @@ export function isOutsideSrc(filename, src) {
4646
const parts = rel.split(path.sep)
4747
return !parts.includes(src)
4848
}
49+
50+
// Utility to check if a file is likely a test file based on its path
51+
export function isTestFile(filename) {
52+
if (!filename) return false
53+
const lower = String(filename).toLowerCase()
54+
return lower.includes('/test/') || lower.includes('/tests/') || lower.includes('.spec.') || lower.includes('.test.')
55+
}

tests/rules/file-component-naming.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,10 @@ describe('vue-modular/file-component-naming (compact)', () => {
8282
const ctxAbs = { getFilename: () => absFilename, options: [{ ignore: [absFilename] }] }
8383
expect(rule.create(ctxAbs)).toEqual({})
8484
})
85+
86+
it('skips test files via direct create() invocation', () => {
87+
const testFile = path.join(process.cwd(), 'src', 'components', 'MyComponent.spec.vue')
88+
const ctxTest = { getFilename: () => testFile, options: [{}] }
89+
expect(rule.create(ctxTest)).toEqual({})
90+
})
8591
})

tests/utils.test.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path'
22
import { describe, it, expect } from 'vitest'
3-
import { parseRuleOptions, toPascalCase, isComponent, isFileIgnored, isOutsideSrc } from '../src/utils.js'
3+
import { parseRuleOptions, toPascalCase, isComponent, isFileIgnored, isOutsideSrc, isTestFile } from '../src/utils.js'
44

55
describe('src/utils', () => {
66
describe('parseRuleOptions', () => {
@@ -53,11 +53,24 @@ describe('src/utils', () => {
5353
describe('isOutsideSrc', () => {
5454
it('returns false when inside src and true when outside', () => {
5555
const inSrc = path.join(process.cwd(), 'src', 'components', 'Foo.vue')
56-
const out = path.join(process.cwd(), 'other', 'components', 'Foo.vue')
56+
const outSrc = path.join(process.cwd(), 'other', 'components', 'Foo.vue')
5757
expect(isOutsideSrc(inSrc, 'src')).toBe(false)
58-
expect(isOutsideSrc(out, 'src')).toBe(true)
58+
expect(isOutsideSrc(outSrc, 'src')).toBe(true)
5959
expect(isOutsideSrc(inSrc, '')).toBe(false)
6060
expect(isOutsideSrc(inSrc, undefined)).toBe(false)
6161
})
6262
})
63+
64+
describe('isTestFile', () => {
65+
it('detects test files by folder or filename', () => {
66+
expect(isTestFile(path.join(process.cwd(), 'src', 'tests', 'Foo.spec.js'))).toBe(true)
67+
expect(isTestFile(path.join(process.cwd(), 'src', 'test', 'Foo.js'))).toBe(true)
68+
expect(isTestFile(path.join(process.cwd(), 'src', 'components', 'Foo.test.ts'))).toBe(true)
69+
expect(isTestFile(path.join(process.cwd(), 'src', 'components', 'Foo.spec.vue'))).toBe(true)
70+
expect(isTestFile(path.join(process.cwd(), 'src', 'components', 'NotATest.vue'))).toBe(false)
71+
expect(isTestFile('')).toBe(false)
72+
expect(isTestFile(null)).toBe(false)
73+
expect(isTestFile(undefined)).toBe(false)
74+
})
75+
})
6376
})

0 commit comments

Comments
 (0)