8000 Improve vue component detection & fix issue with mixin · vuejs/eslint-plugin-vue@6eea288 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6eea288

Browse files
committed
Improve vue component detection & fix issue with mixin
1 parent 5ed9a17 commit 6eea288

File tree

2 files changed

+87
-16
lines changed

2 files changed

+87
-16
lines changed

lib/rules/component-definition-name-casing.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,33 @@ module.exports = {
5050
}
5151
}
5252

53-
return utils.executeOnVue(context, (obj) => {
54-
if (obj.parent && obj.parent.type === 'CallExpression' && obj.parent.arguments && obj.parent.arguments.length === 2) {
55-
const argument = obj.parent.arguments[0]
56-
if (argument.type === 'Literal') {
57-
convertName(argument)
58-
}
59-
}
53+
return Object.assign({},
54+
{
55+
"CallExpression > MemberExpression > Identifier[name='component']" (node) {
56+
const parent = node.parent.parent
57+
const calleeObject = utils.unwrapTypes(parent.callee.object)
6058

61-
const node = obj.properties
62-
.find(item => (
63-
item.type === 'Property' &&
64-
item.key.name === 'name' &&
65-
item.value.type === 'Literal'
66-
))
59+
if (calleeObject.type === 'Identifier' && calleeObject.name === 'Vue') {
60+
if (parent.arguments && parent.arguments.length === 2) {
61+
const argument = parent.arguments[0]
62+
if (argument.type === 'Literal') {
63+
convertName(argument)
64+
}
65+
}
66+
}
67+
}
68+
},
69+
utils.executeOnVue(context, (obj) => {
70+
const node = obj.properties
71+
.find(item => (
72+
item.type === 'Property' &&
73+
item.key.name === 'name' &&
74+
item.value.type === 'Literal'
75+
))
6776

68-
if (!node) return
69-
convertName(node.value)
70-
})
77+
if (!node) return
78+
convertName(node.value)
79+
})
80+
)
7181
}
7282
}

tests/lib/rules/component-definition-name-casing.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,44 @@ ruleTester.run('component-definition-name-casing', rule, {
9292
code: `Vue.component(fooBar, {})`,
9393
options: ['kebab-case'],
9494
parserOptions
95+
},
96+
{
97+
filename: 'test.vue',
98+
code: `Vue.component('FooBar', component)`,
99+
parserOptions
100+
},
101+
{
102+
filename: 'test.vue',
103+
code: `Vue.component('FooBar', component)`,
104+
options: ['PascalCase'],
105+
parserOptions
106+
},
107+
{
108+
filename: 'test.vue',
109+
code: `Vue.component('foo-bar', component)`,
110+
options: ['kebab-case'],
111+
parserOptions
112+
},
113+
{
114+
filename: 'test.vue',
115+
code: `Vue.component(fooBar, component)`,
116+
options: ['kebab-case'],
117+
parserOptions
118+
},
119+
{
120+
filename: 'test.vue',
121+
code: `Vue.mixin({})`,
122+
parserOptions
123+
},
124+
{
125+
filename: 'test.vue',
126+
code: `foo({})`,
127+
parserOptions
128+
},
129+
{
130+
filename: 'test.vue',
131+
code: `foo('foo-bar', {})`,
132+
parserOptions
95133
}
96134
],
97135

@@ -231,6 +269,29 @@ ruleTester.run('component-definition-name-casing', rule, {
231269
line: 3
232270
}]
233271
},
272+
{
273+
filename: 'test.vue',
274+
code: `Vue.component('foo-bar', component)`,
275+
output: `Vue.component('FooBar', component)`,
276+
parserOptions,
277+
errors: [{
278+
message: 'Property name "foo-bar" is not PascalCase.',
279+
type: 'Literal',
280+
line: 1
281+
}]
282+
},
283+
{
284+
filename: 'test.vue',
285+
code: `(Vue as VueConstructor<Vue>).component('foo-bar', component)`,
286+
output: `(Vue as VueConstructor<Vue>).component('FooBar', component)`,
287+
parserOptions,
288+
parser: 'typescript-eslint-parser',
289+
errors: [{
290+
message: 'Property name "foo-bar" is not PascalCase.',
291+
type: 'Literal',
292+
line: 1
293+
}]
294+
},
234295
{
235296
filename: 'test.vue',
236297
code: `Vue.component('foo-bar', {})`,

0 commit comments

Comments
 (0)
0