Skip to content

Commit 3be8ab8

Browse files
feat: add nuxt/no-nuxt-config-test-key (#633)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 1c5ca6a commit 3be8ab8

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

packages/eslint-config/src/configs/nuxt.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ export default function nuxt(options: NuxtESLintConfigOptions): Linter.Config[]
5050
},
5151
})
5252

53+
configs.push({
54+
name: 'nuxt/nuxt-config',
55+
files: [
56+
'**/.config/nuxt.?([cm])[jt]s?(x)',
57+
'**/nuxt.config.?([cm])[jt]s?(x)',
58+
],
59+
rules: {
60+
'nuxt/no-nuxt-config-test-key': 'error',
61+
},
62+
})
63+
5364
if (sortConfigKeys) {
5465
configs.push({
5566
name: 'nuxt/sort-config',

packages/eslint-config/test/__snapshots__/flat-compose.test.ts.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ exports[`flat config composition > custom src dirs 1`] = `
7373
{
7474
"name": "nuxt/rules",
7575
},
76+
{
77+
"files": [
78+
"**/.config/nuxt.?([cm])[jt]s?(x)",
79+
"**/nuxt.config.?([cm])[jt]s?(x)",
80+
],
81+
"name": "nuxt/nuxt-config",
82+
},
7683
{
7784
"files": [
7885
"src1/app.{js,ts,jsx,tsx,vue}",
@@ -164,6 +171,13 @@ exports[`flat config composition > empty 1`] = `
164171
{
165172
"name": "nuxt/rules",
166173
},
174+
{
175+
"files": [
176+
"**/.config/nuxt.?([cm])[jt]s?(x)",
177+
"**/nuxt.config.?([cm])[jt]s?(x)",
178+
],
179+
"name": "nuxt/nuxt-config",
180+
},
167181
{
168182
"files": [
169183
"app.{js,ts,jsx,tsx,vue}",
@@ -201,6 +215,13 @@ exports[`flat config composition > non-standalone 1`] = `
201215
{
202216
"name": "nuxt/rules",
203217
},
218+
{
219+
"files": [
220+
"**/.config/nuxt.?([cm])[jt]s?(x)",
221+
"**/nuxt.config.?([cm])[jt]s?(x)",
222+
],
223+
"name": "nuxt/nuxt-config",
224+
},
204225
{
205226
"files": [
206227
"app.{js,ts,jsx,tsx,vue}",
@@ -292,6 +313,13 @@ exports[`flat config composition > with stylistic 1`] = `
292313
{
293314
"name": "nuxt/rules",
294315
},
316+
{
317+
"files": [
318+
"**/.config/nuxt.?([cm])[jt]s?(x)",
319+
"**/nuxt.config.?([cm])[jt]s?(x)",
320+
],
321+
"name": "nuxt/nuxt-config",
322+
},
295323
{
296324
"files": [
297325
"**/.config/nuxt.?([cm])[jt]s?(x)",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { rule as preferImportMetaRule } from './prefer-import-meta'
22
import { rule as nuxtConfigOrderKeysRule } from './nuxt-config-keys-order'
3+
import { rule as noNuxtConfigTestKeyRule } from './no-nuxt-config-test-key'
34

45
export default {
56
'prefer-import-meta': preferImportMetaRule,
67
'nuxt-config-keys-order': nuxtConfigOrderKeysRule,
8+
'no-nuxt-config-test-key': noNuxtConfigTestKeyRule,
79
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import type { TSESTree as Tree } from '@typescript-eslint/utils'
2+
import { createRule } from '../utils'
3+
4+
type MessageIds = 'default'
5+
6+
type Options = []
7+
8+
export const rule = createRule<MessageIds, Options>({
9+
name: 'no-nuxt-config-test-key',
10+
meta: {
11+
type: 'problem',
12+
docs: {
13+
description: 'Disallow setting `test` key in Nuxt config',
14+
},
15+
schema: [],
16+
messages: {
17+
default: 'Do not set `test` key in Nuxt config. The test environment is automatically detected.',
18+
},
19+
},
20+
defaultOptions: [],
21+
create(context) {
22+
return {
23+
ExportDefaultDeclaration(node) {
24+
let object: Tree.ObjectExpression | undefined
25+
if (node.declaration.type === 'ObjectExpression') {
26+
object = node.declaration
27+
}
28+
else if (node.declaration.type === 'CallExpression' && node.declaration.arguments[0]?.type === 'ObjectExpression') {
29+
object = node.declaration.arguments[0]
30+
}
31+
if (!object) {
32+
return
33+
}
34+
35+
for (const prop of object.properties) {
36+
if (
37+
prop.type === 'Property'
38+
&& prop.key.type === 'Identifier'
39+
&& prop.key.name === 'test'
40+
&& (
41+
(prop.value.type === 'Literal' && typeof prop.value.value === 'boolean')
42+
|| (prop.value.type === 'Identifier' && (prop.value.name === 'true' || prop.value.name === 'false'))
43+
)
44+
) {
45+
context.report({
46+
node: prop,
47+
messageId: 'default',
48+
})
49+
}
50+
}
51+
},
52+
}
53+
},
54+
})

0 commit comments

Comments
 (0)