Skip to content

Commit 3f94c08

Browse files
authored
Fixed crashes of no-reserved-component-names, match-component-file-name and component-definition-name-casing rules. (#1019)
1 parent ee4143b commit 3f94c08

7 files changed

+61
-40
lines changed

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,15 @@ module.exports = {
7373
}
7474

7575
return Object.assign({},
76-
{
77-
"CallExpression > MemberExpression > Identifier[name='component']" (node) {
78-
const parent = node.parent.parent
79-
const calleeObject = utils.unwrapTypes(parent.callee.object)
76+
utils.executeOnCallVueComponent(context, (node) => {
77+
if (node.arguments.length === 2) {
78+
const argument = node.arguments[0]
8079

81-
if (calleeObject.type === 'Identifier' && calleeObject.name === 'Vue') {
82-
if (parent.arguments && parent.arguments.length === 2) {
83-
const argument = parent.arguments[0]
84-
if (canConvert(argument)) {
85-
convertName(argument)
86-
}
87-
}
80+
if (canConvert(argument)) {
81+
convertName(argument)
8882
}
8983
}
90-
},
84+
}),
9185
utils.executeOnVue(context, (obj) => {
9286
const node = obj.properties
9387
.find(item => (

lib/rules/match-component-file-name.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,15 @@ module.exports = {
101101
}
102102

103103
return Object.assign({},
104-
{
105-
"CallExpression > MemberExpression > Identifier[name='component']" (node) {
106-
const parent = node.parent.parent
107-
const calleeObject = utils.unwrapTypes(parent.callee.object)
108-
109-
if (calleeObject.type === 'Identifier' && calleeObject.name === 'Vue') {
110-
if (parent.arguments && parent.arguments.length === 2) {
111-
const argument = parent.arguments[0]
112-
if (canVerify(argument)) {
113-
verifyName(argument)
114-
}
115-
}
104+
utils.executeOnCallVueComponent(context, (node) => {
105+
if (node.arguments.length === 2) {
106+
const argument = node.arguments[0]
107+
108+
if (canVerify(argument)) {
109+
verifyName(argument)
116110
}
117111
}
118-
},
112+
}),
119113
utils.executeOnVue(context, (object) => {
120114
const node = object.properties
121115
.find(item => (

lib/rules/no-reserved-component-names.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,15 @@ module.exports = {
8686
}
8787

8888
return Object.assign({},
89-
{
90-
"CallExpression > MemberExpression > Identifier[name='component']" (node) {
91-
const parent = node.parent.parent
92-
const calleeObject = utils.unwrapTypes(parent.callee.object)
89+
utils.executeOnCallVueComponent(context, (node) => {
90+
if (node.arguments.length === 2) {
91+
const argument = node.arguments[0]
9392

94-
if (calleeObject.type === 'Identifier' &&
95-
calleeObject.name === 'Vue' &&
96-
parent.arguments &&
97-
parent.arguments.length === 2
98-
) {
99-
const argument = parent.arguments[0]
100-
101-
if (canVerify(argument)) {
102-
reportIfInvalid(argument)
103-
}
93+
if (canVerify(argument)) {
94+
reportIfInvalid(argument)
10495
}
10596
}
106-
},
97+
}),
10798
utils.executeOnVue(context, (obj) => {
10899
// Report if a component has been registered locally with a reserved name.
109100
utils.getRegisteredComponents(obj)

lib/utils/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,30 @@ module.exports = {
635635
}
636636
},
637637

638+
/**
639+
* Check call `Vue.component` and call callback.
640+
* @param {RuleContext} _context The ESLint rule context object.
641+
* @param {Function} cb Callback function
642+
*/
643+
executeOnCallVueComponent (_context, cb) {
644+
return {
645+
"CallExpression > MemberExpression > Identifier[name='component']": (node) => {
646+
const callExpr = node.parent.parent
647+
const callee = callExpr.callee
648+
649+
if (callee.type === 'MemberExpression') {
650+
const calleeObject = this.unwrapTypes(callee.object)
651+
652+
if (calleeObject.type === 'Identifier' &&
653+
calleeObject.name === 'Vue' &&
654+
callee.property === node &&
655+
callExpr.arguments.length >= 1) {
656+
cb(callExpr)
657+
}
658+
}
659+
}
660+
}
661+
},
638662
/**
639663
* Return generator with all properties
640664
* @param {ASTNode} node Node to check

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ ruleTester.run('component-definition-name-casing', rule, {
136136
code: `Vue.component(\`fooBar\${foo}\`, component)`,
137137
options: ['kebab-case'],
138138
parserOptions
139+
},
140+
// https://github.com/vuejs/eslint-plugin-vue/issues/1018
141+
{
142+
filename: 'test.js',
143+
code: `fn1(component.data)`,
144+
parserOptions
139145
}
140146
],
141147

tests/lib/rules/match-component-file-name.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,12 @@ ruleTester.run('match-component-file-name', rule, {
531531
`,
532532
options: [{ shouldMatchCase: true }],
533533
parserOptions: jsxParserOptions
534+
},
535+
// https://github.com/vuejs/eslint-plugin-vue/issues/1018
536+
{
537+
filename: 'test.jsx',
538+
code: `fn1(component.data)`,
539+
parserOptions
534540
}
535541
],
536542

tests/lib/rules/no-reserved-component-names.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,12 @@ ruleTester.run('no-reserved-component-names', rule, {
311311
`,
312312
parser: require.resolve('vue-eslint-parser'),
313313
parserOptions
314+
},
315+
// https://github.com/vuejs/eslint-plugin-vue/issues/1018
316+
{
317+
filename: 'test.js',
318+
code: `fn1(component.data)`,
319+
parserOptions
314320
}
315321
],
316322

0 commit comments

Comments
 (0)