Skip to content

Commit f5652dc

Browse files
authored
Merge branch 'master' into feat-ref
2 parents 9273594 + 827ab4b commit f5652dc

File tree

4 files changed

+136
-10
lines changed

4 files changed

+136
-10
lines changed

lib/rules/max-props.js

+22-8
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,41 @@ module.exports = {
3939

4040
/**
4141
* @param {import('../utils').ComponentProp[]} props
42+
* @param {CallExpression | Property} node
4243
*/
43-
function checkMaxNumberOfProps(props) {
44-
if (props.length > option.maxProps && props[0].node) {
44+
function checkMaxNumberOfProps(props, node) {
45+
const uniqueProps = new Set(props.map((prop) => prop.propName))
46+
const propCount = uniqueProps.size
47+
if (propCount > option.maxProps && props[0].node) {
4548
context.report({
46-
node: props[0].node.parent,
49+
node,
4750
messageId: 'tooManyProps',
4851
data: {
49-
propCount: props.length,
52+
propCount,
5053
limit: option.maxProps
5154
}
5255
})
5356
}
5457
}
5558

5659
return utils.compositingVisitors(
57-
utils.executeOnVue(context, (obj) => {
58-
checkMaxNumberOfProps(utils.getComponentPropsFromOptions(obj))
60+
utils.executeOnVue(context, (node) => {
61+
const propsNode = node.properties.find(
62+
/** @returns {p is Property} */
63+
(p) =>
64+
p.type === 'Property' && utils.getStaticPropertyName(p) === 'props'
65+
)
66+
67+
if (!propsNode) return
68+
69+
checkMaxNumberOfProps(
70+
utils.getComponentPropsFromOptions(node),
71+
propsNode
72+
)
5973
}),
6074
utils.defineScriptSetupVisitor(context, {
61-
onDefinePropsEnter(_, props) {
62-
checkMaxNumberOfProps(props)
75+
onDefinePropsEnter(node, props) {
76+
checkMaxNumberOfProps(props, node)
6377
}
6478
})
6579
)

lib/rules/no-bare-strings-in-template.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ module.exports = {
154154
const directives = opts.directives || DEFAULT_DIRECTIVES
155155

156156
const allowlistRe = new RegExp(
157-
allowlist.map((w) => regexp.escape(w)).join('|'),
157+
allowlist
158+
.map((w) => regexp.escape(w))
159+
.sort((a, b) => b.length - a.length)
160+
.join('|'),
158161
'gu'
159162
)
160163

tests/lib/rules/max-props.js

+73
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ tester.run('max-props', rule, {
2626
`,
2727
options: [{ maxProps: 5 }]
2828
},
29+
{
30+
filename: 'test.vue',
31+
code: `
32+
<script setup>
33+
defineProps(['prop1', 'prop2'])
34+
</script>
35+
`,
36+
options: [{ maxProps: 5 }]
37+
},
2938
{
3039
filename: 'test.vue',
3140
code: `
@@ -99,6 +108,20 @@ tester.run('max-props', rule, {
99108
parser: require.resolve('@typescript-eslint/parser')
100109
}
101110
}
111+
},
112+
{
113+
filename: 'test.vue',
114+
code: `
115+
<script setup lang="ts">
116+
defineProps<{prop1: string, prop2: string} | {prop1: number}>()
117+
</script>
118+
`,
119+
options: [{ maxProps: 2 }],
120+
languageOptions: {
121+
parserOptions: {
122+
parser: require.resolve('@typescript-eslint/parser')
123+
}
124+
}
102125
}
103126
],
104127
invalid: [
@@ -160,6 +183,56 @@ tester.run('max-props', rule, {
160183
endLine: 3
161184
}
162185
]
186+
},
187+
{
188+
filename: 'test.vue',
189+
code: `
190+
<script setup lang="ts">
191+
defineProps<{prop1: string, prop2: string} | {prop1: number, prop3: string}>()
192+
</script>
193+
`,
194+
options: [{ maxProps: 2 }],
195+
languageOptions: {
196+
parserOptions: {
197+
parser: require.resolve('@typescript-eslint/parser')
198+
}
199+
},
200+
errors: [
201+
{
202+
message: 'Component has too many props (3). Maximum allowed is 2.',
203+
line: 3,
204+
endLine: 3
205+
}
206+
]
207+
},
208+
{
209+
filename: 'test.vue',
210+
code: `
211+
<script setup lang="ts">
212+
defineProps<{
213+
prop1: string
214+
} & {
215+
prop2?: true;
216+
prop3?: never;
217+
} | {
218+
prop2?: false;
219+
prop3?: boolean;
220+
}>()
221+
</script>
222+
`,
223+
options: [{ maxProps: 2 }],
224+
languageOptions: {
225+
parserOptions: {
226+
parser: require.resolve('@typescript-eslint/parser')
227+
}
228+
},
229+
errors: [
230+
{
231+
message: 'Component has too many props (3). Maximum allowed is 2.',
232+
line: 3,
233+
endLine: 11
234+
}
235+
]
163236
}
164237
]
165238
})

tests/lib/rules/no-bare-strings-in-template.js

+37-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,25 @@ tester.run('no-bare-strings-in-template', rule, {
114114
title="( ) , . & + - = * / # % ! ? : [ ] { } < > • — | &lpar; &rpar; &comma; &period; &amp; &AMP; &plus; &minus; &equals; &ast; &midast; &sol; &num; &percnt; &excl; &quest; &colon; &lsqb; &lbrack; &rsqb; &rbrack; &lcub; &lbrace; &rcub; &rbrace; &lt; &LT; &gt; &GT; &bull; &bullet; &mdash; &ndash; &nbsp; &Tab; &NewLine; &verbar; &vert; &VerticalLine;"
115115
/>
116116
</template>
117-
`
117+
`,
118+
{
119+
// https://github.com/vuejs/eslint-plugin-vue/issues/2681
120+
code: `
121+
<template>
122+
<h1> foo </h1>
123+
<h1> foo_bar </h1>
124+
</template>
125+
`,
126+
options: [{ allowlist: ['foo', 'foo_bar'] }]
127+
},
128+
{
129+
code: `
130+
<template>
131+
<h1>@@</h1>
132+
</template>
133+
`,
134+
options: [{ allowlist: ['@@'] }]
135+
}
118136
],
119137
invalid: [
120138
{
@@ -227,6 +245,24 @@ tester.run('no-bare-strings-in-template', rule, {
227245
}
228246
]
229247
},
248+
{
249+
code: `
250+
<template>
251+
<h1>foo</h1>
252+
<h1>foo_bar</h1>
253+
</template>
254+
`,
255+
options: [{ allowlist: ['foo'] }],
256+
errors: [
257+
{
258+
messageId: 'unexpected',
259+
line: 4,
260+
column: 13,
261+
endLine: 4,
262+
endColumn: 20
263+
}
264+
]
265+
},
230266
{
231267
code: `
232268
<template>

0 commit comments

Comments
 (0)