Skip to content

Commit ae2d78a

Browse files
committedMar 29, 2024·
feat(eslint-config): options for typescript, add some snapshot tests
1 parent d77ebe7 commit ae2d78a

File tree

7 files changed

+252
-5
lines changed

7 files changed

+252
-5
lines changed
 

‎packages/eslint-config/src/flat/configs/typescript.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import * as parserTs from '@typescript-eslint/parser'
22
import pluginTs from '@typescript-eslint/eslint-plugin'
33
import type { FlatConfigItem } from 'eslint-flat-config-utils'
4+
import { resolveOptions } from '../utils'
5+
import type { NuxtESLintConfigOptions } from '@nuxt/eslint-config/flat'
6+
7+
export default function typescript(options: NuxtESLintConfigOptions): FlatConfigItem[] {
8+
const resolved = resolveOptions(options)
9+
10+
if (resolved.features.typescript === false) {
11+
return []
12+
}
13+
14+
const tsOptions = resolved.features.typescript === true ? {} : resolved.features.typescript
415

5-
export default function typescript(): FlatConfigItem[] {
616
return [
717
{
818
name: 'nuxt:typescript:setup',
@@ -21,7 +31,9 @@ export default function typescript(): FlatConfigItem[] {
2131
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2232
...pluginTs.configs['eslint-recommended'].overrides![0].rules as any,
2333
...pluginTs.configs.recommended.rules,
24-
...pluginTs.configs.strict.rules,
34+
...(tsOptions.strict === false
35+
? {}
36+
: pluginTs.configs.strict.rules),
2537

2638
'@typescript-eslint/no-non-null-assertion': 'off',
2739
'@typescript-eslint/consistent-type-imports': ['error', { disallowTypeAnnotations: false, prefer: 'type-imports' }],

‎packages/eslint-config/src/flat/configs/vue.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { FlatConfigItem } from 'eslint-flat-config-utils'
99

1010
export default function vue(options: NuxtESLintConfigOptions): FlatConfigItem[] {
1111
const resolved = resolveOptions(options)
12+
const hasTs = resolved.features.typescript !== false
1213

1314
return [
1415
{
@@ -21,7 +22,7 @@ export default function vue(options: NuxtESLintConfigOptions): FlatConfigItem[]
2122
parserOptions: {
2223
ecmaVersion: 'latest',
2324
extraFileExtensions: ['.vue'],
24-
parser: parserTs,
25+
parser: hasTs ? parserTs : undefined,
2526
sourceType: 'module',
2627
ecmaFeatures: {
2728
jsx: true,

‎packages/eslint-config/src/flat/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function createConfigForNuxt(options: NuxtESLintConfigOptions = {}): Flat
4141
pipeline.append(
4242
base(),
4343
javascript(),
44-
typescript(),
44+
typescript(resolved),
4545
vue(resolved),
4646
)
4747
}

‎packages/eslint-config/src/flat/types.ts

+14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ export interface NuxtESLintFeaturesOptions {
1717
* @default false
1818
*/
1919
stylistic?: boolean | StylisticCustomizeOptions<true>
20+
21+
/**
22+
* Options for TypeScript setup
23+
*
24+
* @default true
25+
*/
26+
typescript?: boolean | {
27+
/**
28+
* Enable strict rules
29+
* @see https://typescript-eslint.io/users/configs#strict
30+
* @default true
31+
*/
32+
strict?: boolean
33+
}
2034
}
2135

2236
export interface NuxtESLintConfigOptions {

‎packages/eslint-config/src/flat/utils.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function resolveOptions(
1515
...config.dirs,
1616
} as NuxtESLintConfigOptionsResolved['dirs']
1717

18-
dirs.root ||= [process.cwd()]
18+
dirs.root ||= ['.']
1919
dirs.src ||= dirs.root
2020
dirs.pages ||= dirs.src.map(src => `${src}/pages`)
2121
dirs.layouts ||= dirs.src.map(src => `${src}/layouts`)
@@ -30,6 +30,7 @@ export function resolveOptions(
3030
features: {
3131
standalone: true,
3232
stylistic: false,
33+
typescript: true,
3334
...config.features,
3435
},
3536
dirs,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`flat config composition > custom src dirs 1`] = `
4+
[
5+
{
6+
"ignores": [
7+
"**/dist",
8+
"**/node_modules",
9+
"**/.nuxt",
10+
"**/.output",
11+
"**/.vercel",
12+
"**/.netlify",
13+
],
14+
},
15+
{
16+
"name": "nuxt:javascript",
17+
},
18+
{
19+
"name": "nuxt:typescript:setup",
20+
},
21+
{
22+
"files": [
23+
"**/*.ts",
24+
"**/*.tsx",
25+
"**/*.mts",
26+
"**/*.cts",
27+
"**/*.vue",
28+
],
29+
"name": "nuxt:typescript",
30+
},
31+
{
32+
"files": [
33+
"**/*.ts",
34+
"**/*.tsx",
35+
"**/*.mts",
36+
"**/*.cts",
37+
"**/*.vue",
38+
],
39+
"name": "nuxt:typescript:disables",
40+
},
41+
{
42+
"name": "nuxt:vue:setup",
43+
},
44+
{
45+
"files": [
46+
"**/*.vue",
47+
],
48+
"name": "nuxt:vue",
49+
},
50+
{
51+
"name": "nuxt:rules",
52+
},
53+
{
54+
"files": [
55+
"src1/layouts/**/*.{js,ts,jsx,tsx,vue}",
56+
"src2/layouts/**/*.{js,ts,jsx,tsx,vue}",
57+
"src1/pages/**/*.{js,ts,jsx,tsx,vue}",
58+
"src2/pages/**/*.{js,ts,jsx,tsx,vue}",
59+
],
60+
"name": "nuxt:vue:single-root",
61+
},
62+
{
63+
"files": [
64+
"src1/app.{js,ts,jsx,tsx,vue}",
65+
"src1/error.{js,ts,jsx,tsx,vue}",
66+
"src2/app.{js,ts,jsx,tsx,vue}",
67+
"src2/error.{js,ts,jsx,tsx,vue}",
68+
"src1/layouts/**/*.{js,ts,jsx,tsx,vue}",
69+
"src2/layouts/**/*.{js,ts,jsx,tsx,vue}",
70+
"src1/pages/**/*.{js,ts,jsx,tsx,vue}",
71+
"src2/pages/**/*.{js,ts,jsx,tsx,vue}",
72+
"src1/components/*/**/*.{js,ts,jsx,tsx,vue}",
73+
"src2/components/*/**/*.{js,ts,jsx,tsx,vue}",
74+
],
75+
"name": "nuxt:vue:routes:disables",
76+
},
77+
]
78+
`;
79+
80+
exports[`flat config composition > empty 1`] = `
81+
[
82+
{
83+
"ignores": [
84+
"**/dist",
85+
"**/node_modules",
86+
"**/.nuxt",
87+
"**/.output",
88+
"**/.vercel",
89+
"**/.netlify",
90+
],
91+
},
92+
{
93+
"name": "nuxt:javascript",
94+
},
95+
{
96+
"name": "nuxt:typescript:setup",
97+
},
98+
{
99+
"files": [
100+
"**/*.ts",
101+
"**/*.tsx",
102+
"**/*.mts",
103+
"**/*.cts",
104+
"**/*.vue",
105+
],
106+
"name": "nuxt:typescript",
107+
},
108+
{
109+
"files": [
110+
"**/*.ts",
111+
"**/*.tsx",
112+
"**/*.mts",
113+
"**/*.cts",
114+
"**/*.vue",
115+
],
116+
"name": "nuxt:typescript:disables",
117+
},
118+
{
119+
"name": "nuxt:vue:setup",
120+
},
121+
{
122+
"files": [
123+
"**/*.vue",
124+
],
125+
"name": "nuxt:vue",
126+
},
127+
{
128+
"name": "nuxt:rules",
129+
},
130+
{
131+
"files": [
132+
"layouts/**/*.{js,ts,jsx,tsx,vue}",
133+
"pages/**/*.{js,ts,jsx,tsx,vue}",
134+
],
135+
"name": "nuxt:vue:single-root",
136+
},
137+
{
138+
"files": [
139+
"app.{js,ts,jsx,tsx,vue}",
140+
"error.{js,ts,jsx,tsx,vue}",
141+
"layouts/**/*.{js,ts,jsx,tsx,vue}",
142+
"pages/**/*.{js,ts,jsx,tsx,vue}",
143+
"components/*/**/*.{js,ts,jsx,tsx,vue}",
144+
],
145+
"name": "nuxt:vue:routes:disables",
146+
},
147+
]
148+
`;
149+
150+
exports[`flat config composition > non-standalone 1`] = `
151+
[
152+
{
153+
"name": "nuxt:rules",
154+
},
155+
{
156+
"files": [
157+
"layouts/**/*.{js,ts,jsx,tsx,vue}",
158+
"pages/**/*.{js,ts,jsx,tsx,vue}",
159+
],
160+
"name": "nuxt:vue:single-root",
161+
},
162+
{
163+
"files": [
164+
"app.{js,ts,jsx,tsx,vue}",
165+
"error.{js,ts,jsx,tsx,vue}",
166+
"layouts/**/*.{js,ts,jsx,tsx,vue}",
167+
"pages/**/*.{js,ts,jsx,tsx,vue}",
168+
"components/*/**/*.{js,ts,jsx,tsx,vue}",
169+
],
170+
"name": "nuxt:vue:routes:disables",
171+
},
172+
]
173+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { createConfigForNuxt } from '../src/flat'
3+
import type { FlatConfigItem } from 'eslint-flat-config-utils'
4+
5+
const cwd = process.cwd()
6+
7+
function getFlatConfigDigest(configs: FlatConfigItem[]) {
8+
return configs.map((config) => {
9+
return JSON.parse(JSON.stringify({
10+
name: config.name,
11+
files: config.files,
12+
ignores: config.ignores,
13+
}).replaceAll(cwd, '<cwd>'))
14+
})
15+
}
16+
17+
describe('flat config composition', () => {
18+
it('empty', async () => {
19+
const configs = await createConfigForNuxt()
20+
21+
expect(getFlatConfigDigest(configs))
22+
.toMatchSnapshot()
23+
})
24+
25+
it('non-standalone', async () => {
26+
const configs = await createConfigForNuxt({
27+
features: {
28+
standalone: false,
29+
},
30+
})
31+
32+
expect(getFlatConfigDigest(configs))
33+
.toMatchSnapshot()
34+
})
35+
36+
it('custom src dirs', async () => {
37+
const configs = await createConfigForNuxt({
38+
dirs: {
39+
src: ['src1', 'src2'],
40+
},
41+
})
42+
43+
expect(getFlatConfigDigest(configs))
44+
.toMatchSnapshot()
45+
})
46+
})

0 commit comments

Comments
 (0)
Please sign in to comment.