Skip to content

Commit

Permalink
Fixed crashes of no-reserved-component-names, `match-component-file…
Browse files Browse the repository at this point in the history
…-name` and `component-definition-name-casing` rules. (#1019)
  • Loading branch information
ota-meshi committed Dec 27, 2019
1 parent ee4143b commit 3f94c08
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 40 deletions.
18 changes: 6 additions & 12 deletions lib/rules/component-definition-name-casing.js
Expand Up @@ -73,21 +73,15 @@ module.exports = {
}

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

if (calleeObject.type === 'Identifier' && calleeObject.name === 'Vue') {
if (parent.arguments && parent.arguments.length === 2) {
const argument = parent.arguments[0]
if (canConvert(argument)) {
convertName(argument)
}
}
if (canConvert(argument)) {
convertName(argument)
}
}
},
}),
utils.executeOnVue(context, (obj) => {
const node = obj.properties
.find(item => (
Expand Down
20 changes: 7 additions & 13 deletions lib/rules/match-component-file-name.js
Expand Up @@ -101,21 +101,15 @@ module.exports = {
}

return Object.assign({},
{
"CallExpression > MemberExpression > Identifier[name='component']" (node) {
const parent = node.parent.parent
const calleeObject = utils.unwrapTypes(parent.callee.object)

if (calleeObject.type === 'Identifier' && calleeObject.name === 'Vue') {
if (parent.arguments && parent.arguments.length === 2) {
const argument = parent.arguments[0]
if (canVerify(argument)) {
verifyName(argument)
}
}
utils.executeOnCallVueComponent(context, (node) => {
if (node.arguments.length === 2) {
const argument = node.arguments[0]

if (canVerify(argument)) {
verifyName(argument)
}
}
},
}),
utils.executeOnVue(context, (object) => {
const node = object.properties
.find(item => (
Expand Down
21 changes: 6 additions & 15 deletions lib/rules/no-reserved-component-names.js
Expand Up @@ -86,24 +86,15 @@ module.exports = {
}

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

if (calleeObject.type === 'Identifier' &&
calleeObject.name === 'Vue' &&
parent.arguments &&
parent.arguments.length === 2
) {
const argument = parent.arguments[0]

if (canVerify(argument)) {
reportIfInvalid(argument)
}
if (canVerify(argument)) {
reportIfInvalid(argument)
}
}
},
}),
utils.executeOnVue(context, (obj) => {
// Report if a component has been registered locally with a reserved name.
utils.getRegisteredComponents(obj)
Expand Down
24 changes: 24 additions & 0 deletions lib/utils/index.js
Expand Up @@ -635,6 +635,30 @@ module.exports = {
}
},

/**
* Check call `Vue.component` and call callback.
* @param {RuleContext} _context The ESLint rule context object.
* @param {Function} cb Callback function
*/
executeOnCallVueComponent (_context, cb) {
return {
"CallExpression > MemberExpression > Identifier[name='component']": (node) => {
const callExpr = node.parent.parent
const callee = callExpr.callee

if (callee.type === 'MemberExpression') {
const calleeObject = this.unwrapTypes(callee.object)

if (calleeObject.type === 'Identifier' &&
calleeObject.name === 'Vue' &&
callee.property === node &&
callExpr.arguments.length >= 1) {
cb(callExpr)
}
}
}
}
},
/**
* Return generator with all properties
* @param {ASTNode} node Node to check
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/component-definition-name-casing.js
Expand Up @@ -136,6 +136,12 @@ ruleTester.run('component-definition-name-casing', rule, {
code: `Vue.component(\`fooBar\${foo}\`, component)`,
options: ['kebab-case'],
parserOptions
},
// https://github.com/vuejs/eslint-plugin-vue/issues/1018
{
filename: 'test.js',
code: `fn1(component.data)`,
parserOptions
}
],

Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/match-component-file-name.js
Expand Up @@ -531,6 +531,12 @@ ruleTester.run('match-component-file-name', rule, {
`,
options: [{ shouldMatchCase: true }],
parserOptions: jsxParserOptions
},
// https://github.com/vuejs/eslint-plugin-vue/issues/1018
{
filename: 'test.jsx',
code: `fn1(component.data)`,
parserOptions
}
],

Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/no-reserved-component-names.js
Expand Up @@ -311,6 +311,12 @@ ruleTester.run('no-reserved-component-names', rule, {
`,
parser: require.resolve('vue-eslint-parser'),
parserOptions
},
// https://github.com/vuejs/eslint-plugin-vue/issues/1018
{
filename: 'test.js',
code: `fn1(component.data)`,
parserOptions
}
],

Expand Down

0 comments on commit 3f94c08

Please sign in to comment.