-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy patheslint.config.mjs
More file actions
170 lines (165 loc) · 6.15 KB
/
eslint.config.mjs
File metadata and controls
170 lines (165 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { createConfigForNuxt } from '@nuxt/eslint-config/flat'
/**
* Flag bare prop references in templates of components that use
* `useComponentProps`. Bare refs auto-resolve to the raw `defineProps` result
* via Vue's compiler-generated `__props.X`, bypassing the proxy that resolves
* `<UTheme :props>` and `app.config` defaults.
*
* Auto-fixes by rewriting `arrow` → `props.arrow`.
*
* In `<script setup>`, every free identifier in a template expression resolves
* to either (a) a setup-scope binding or (b) `__props.X`. So if an identifier
* isn't a known setup binding, slot-scoped variable, or JS global, it must be
* a prop access — and therefore needs the `props.` prefix to flow through the
* proxy. This catches inherited props (extended/picked from imported types)
* that no static interface walk would find.
*/
const KNOWN_GLOBALS = new Set([
'undefined', 'null', 'true', 'false', 'NaN', 'Infinity',
'console', 'window', 'document', 'navigator', 'location', 'history',
'Math', 'JSON', 'Object', 'Array', 'String', 'Number', 'Boolean',
'Date', 'RegExp', 'Promise', 'Symbol', 'Error', 'Map', 'Set',
'WeakMap', 'WeakSet', 'Proxy', 'Reflect',
'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'encodeURIComponent', 'decodeURIComponent'
])
const noBarePropRefs = {
meta: {
type: 'problem',
docs: {
description: 'Require `props.X` access in templates of components using `useComponentProps`'
},
fixable: 'code',
schema: [],
messages: {
bareRef: 'Bare prop reference `{{ name }}` bypasses the `useComponentProps` proxy. Use `{{ propsVar }}.{{ name }}` so `<UTheme :props>` defaults flow through.'
}
},
create(context) {
const parserServices = context.sourceCode?.parserServices ?? context.parserServices
if (!parserServices?.defineTemplateBodyVisitor) {
return {}
}
let usesComponentProps = false
let propsVar = 'props'
let rawPropsVar = '_props'
const setupBindings = new Set()
function collectIdsFromPattern(pattern) {
if (!pattern) return
if (pattern.type === 'Identifier') {
setupBindings.add(pattern.name)
} else if (pattern.type === 'ObjectPattern') {
for (const prop of pattern.properties) {
if (prop.type === 'Property') collectIdsFromPattern(prop.value)
else if (prop.type === 'RestElement') collectIdsFromPattern(prop.argument)
}
} else if (pattern.type === 'ArrayPattern') {
for (const el of pattern.elements) {
if (el) collectIdsFromPattern(el)
}
} else if (pattern.type === 'AssignmentPattern') {
collectIdsFromPattern(pattern.left)
} else if (pattern.type === 'RestElement') {
collectIdsFromPattern(pattern.argument)
}
}
return parserServices.defineTemplateBodyVisitor(
{
VExpressionContainer(node) {
if (!usesComponentProps) return
const refs = node.references ?? []
for (const ref of refs) {
if (ref.variable) continue
const id = ref.id
const name = id.name
if (!name) continue
if (name === propsVar || name === rawPropsVar) continue
if (setupBindings.has(name)) continue
if (KNOWN_GLOBALS.has(name)) continue
if (name.startsWith('$') || name.startsWith('_')) continue
// Skip PascalCase identifiers — they're TypeScript type references
// inside `as TypeName` casts, generic params (`T`), or `keyof X`,
// not runtime prop reads. Vue components / props are camelCase by
// convention; type names are PascalCase.
if (/^[A-Z]/.test(name)) continue
context.report({
node: id,
messageId: 'bareRef',
data: { name, propsVar },
fix(fixer) {
// Handle object literal shorthand: `{ to, target }` should
// become `{ to: props.to, target: props.target }`, not the
// syntactically-broken `{ props.to, props.target }`.
const parent = id.parent
if (
parent
&& parent.type === 'Property'
&& parent.shorthand
&& parent.key === id
) {
return fixer.replaceText(parent, `${name}: ${propsVar}.${name}`)
}
return fixer.replaceText(id, `${propsVar}.${name}`)
}
})
}
}
},
{
'Program > VariableDeclaration > VariableDeclarator'(node) {
collectIdsFromPattern(node.id)
},
'Program > FunctionDeclaration'(node) {
if (node.id?.type === 'Identifier') setupBindings.add(node.id.name)
},
'Program > ClassDeclaration'(node) {
if (node.id?.type === 'Identifier') setupBindings.add(node.id.name)
},
ImportDeclaration(node) {
for (const spec of node.specifiers) {
if (spec.local?.type === 'Identifier') setupBindings.add(spec.local.name)
}
},
'CallExpression[callee.name="useComponentProps"]'(node) {
usesComponentProps = true
const decl = node.parent?.type === 'VariableDeclarator' ? node.parent : null
if (decl?.id?.type === 'Identifier') {
propsVar = decl.id.name
}
const rawArg = node.arguments[1]
if (rawArg?.type === 'Identifier') {
rawPropsVar = rawArg.name
}
}
}
)
}
}
export default createConfigForNuxt({
features: {
tooling: true,
stylistic: {
commaDangle: 'never',
braceStyle: '1tbs'
}
}
}).overrideRules({
'import/first': 'off',
'import/order': 'off',
'vue/multi-word-component-names': 'off',
'vue/max-attributes-per-line': ['error', { singleline: 5 }],
'@typescript-eslint/ban-types': 'off',
'@typescript-eslint/no-empty-object-type': 'off',
'@typescript-eslint/no-explicit-any': 'off'
}).append({
files: ['src/runtime/components/**/*.vue'],
plugins: {
'nuxt-ui': {
rules: {
'no-bare-prop-refs': noBarePropRefs
}
}
},
rules: {
'nuxt-ui/no-bare-prop-refs': 'error'
}
})