Skip to content

Commit c91086a

Browse files
committed
feat: add exclude tags option
1 parent 16c8778 commit c91086a

File tree

4 files changed

+111
-3
lines changed

4 files changed

+111
-3
lines changed

lib/rules/attribute-hyphenation.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const utils = require('../utils')
88
const casing = require('../utils/casing')
9+
const { toRegExp } = require('../utils/regexp')
910
const svgAttributes = require('../utils/svg-attributes-weird-case.json')
1011

1112
/**
@@ -56,6 +57,12 @@ module.exports = {
5657
},
5758
uniqueItems: true,
5859
additionalItems: false
60+
},
61+
exclude: {
62+
type: 'array',
63+
items: { type: 'string' },
64+
uniqueItems: true,
65+
additionalItems: false
5966
}
6067
},
6168
additionalProperties: false
@@ -72,6 +79,10 @@ module.exports = {
7279
const option = context.options[0]
7380
const optionsPayload = context.options[1]
7481
const useHyphenated = option !== 'never'
82+
/** @type {RegExp[]} */
83+
const excludedTags = ((optionsPayload && optionsPayload.exclude) || []).map(
84+
toRegExp
85+
)
7586
const ignoredAttributes = ['data-', 'aria-', 'slot-scope', ...svgAttributes]
7687

7788
if (optionsPayload && optionsPayload.ignore) {
@@ -130,11 +141,19 @@ module.exports = {
130141
return useHyphenated ? value.toLowerCase() === value : !/-/.test(value)
131142
}
132143

144+
/**
145+
* @param {string} name
146+
*/
147+
function isExcludedTags(name) {
148+
return excludedTags.some((re) => re.test(name))
149+
}
150+
133151
return utils.defineTemplateBodyVisitor(context, {
134152
VAttribute(node) {
153+
const element = node.parent.parent
135154
if (
136-
!utils.isCustomComponent(node.parent.parent) &&
137-
node.parent.parent.name !== 'slot'
155+
(!utils.isCustomComponent(element) && element.name !== 'slot') ||
156+
isExcludedTags(element.rawName)
138157
)
139158
return
140159

lib/rules/v-on-event-hyphenation.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const utils = require('../utils')
44
const casing = require('../utils/casing')
5+
const { toRegExp } = require('../utils/regexp')
56

67
module.exports = {
78
meta: {
@@ -35,6 +36,12 @@ module.exports = {
3536
},
3637
uniqueItems: true,
3738
additionalItems: false
39+
},
40+
exclude: {
41+
type: 'array',
42+
items: { type: 'string' },
43+
uniqueItems: true,
44+
additionalItems: false
3845
}
3946
},
4047
additionalProperties: false
@@ -56,6 +63,10 @@ module.exports = {
5663
const useHyphenated = option !== 'never'
5764
/** @type {string[]} */
5865
const ignoredAttributes = (optionsPayload && optionsPayload.ignore) || []
66+
/** @type {RegExp[]} */
67+
const excludedTags = ((optionsPayload && optionsPayload.exclude) || []).map(
68+
toRegExp
69+
)
5970
const autofix = Boolean(optionsPayload && optionsPayload.autofix)
6071

6172
const caseConverter = casing.getConverter(
@@ -99,9 +110,22 @@ module.exports = {
99110
return useHyphenated ? value.toLowerCase() === value : !/-/.test(value)
100111
}
101112

113+
/**
114+
* @param {string} name
115+
*/
116+
function isExcludedTags(name) {
117+
return excludedTags.some((re) => re.test(name))
118+
}
119+
102120
return utils.defineTemplateBodyVisitor(context, {
103121
"VAttribute[directive=true][key.name.name='on']"(node) {
104-
if (!utils.isCustomComponent(node.parent.parent)) return
122+
const element = node.parent.parent
123+
if (
124+
!utils.isCustomComponent(element) ||
125+
isExcludedTags(element.rawName)
126+
) {
127+
return
128+
}
105129
if (!node.key.argument || node.key.argument.type !== 'VIdentifier') {
106130
return
107131
}

tests/lib/rules/attribute-hyphenation.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ ruleTester.run('attribute-hyphenation', rule, {
8585
filename: 'test.vue',
8686
code: '<template><div><custom :myName.sync="prop"></custom></div></template>',
8787
options: ['never']
88+
},
89+
{
90+
filename: 'test.vue',
91+
code: `
92+
<template>
93+
<VueComponent my-prop></VueComponent>
94+
<custom-component my-prop></custom-component>
95+
</template>
96+
`,
97+
options: ['never', { exclude: ['VueComponent', '/^custom-/'] }]
8898
}
8999
],
90100

@@ -450,6 +460,28 @@ ruleTester.run('attribute-hyphenation', rule, {
450460
line: 1
451461
}
452462
]
463+
},
464+
{
465+
code: `
466+
<template>
467+
<custom my-prop/>
468+
<CustomComponent my-prop/>
469+
</template>
470+
`,
471+
output: `
472+
<template>
473+
<custom myProp/>
474+
<CustomComponent my-prop/>
475+
</template>
476+
`,
477+
options: ['never', { exclude: ['CustomComponent'] }],
478+
errors: [
479+
{
480+
message: "Attribute 'my-prop' can't be hyphenated.",
481+
type: 'VIdentifier',
482+
line: 3
483+
}
484+
]
453485
}
454486
]
455487
})

tests/lib/rules/v-on-event-hyphenation.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ tester.run('v-on-event-hyphenation', rule, {
4444
</template>
4545
`,
4646
options: ['never', { ignore: ['custom'] }]
47+
},
48+
{
49+
code: `
50+
<template>
51+
<VueComponent v-on:custom-event="events"/>
52+
</template>
53+
`,
54+
options: ['never', { ignore: ['custom-event'] }]
55+
},
56+
{
57+
code: `
58+
<template>
59+
<VueComponent v-on:custom-event="events"/>
60+
<custom-component v-on:custom-event="events"/>
61+
</template>
62+
`,
63+
options: ['never', { exclude: ['/^Vue/', 'custom-component'] }]
4764
}
4865
],
4966
invalid: [
@@ -179,6 +196,22 @@ tester.run('v-on-event-hyphenation', rule, {
179196
"v-on event '@upDate:model-value' can't be hyphenated.",
180197
"v-on event '@up-date:model-value' can't be hyphenated."
181198
]
199+
},
200+
{
201+
code: `
202+
<template>
203+
<VueComponent v-on:custom-event="events"/>
204+
<CustomComponent v-on:custom-event="events"/>
205+
</template>
206+
`,
207+
output: `
208+
<template>
209+
<VueComponent v-on:customEvent="events"/>
210+
<CustomComponent v-on:custom-event="events"/>
211+
</template>
212+
`,
213+
options: ['never', { autofix: true, exclude: ['CustomComponent'] }],
214+
errors: ["v-on event 'v-on:custom-event' can't be hyphenated."]
182215
}
183216
]
184217
})

0 commit comments

Comments
 (0)