Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positives for default with type Function in vue/no-deprecated-props-default-this rule #1522

Merged
merged 1 commit into from Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/rules/no-deprecated-props-default-this.js
Expand Up @@ -73,6 +73,27 @@ module.exports = {
scopeStack = scopeStack.upper
}
}

/**
* @param {Expression|SpreadElement|null} node
*/
function isFunctionIdentifier(node) {
return node && node.type === 'Identifier' && node.name === 'Function'
}

/**
* @param {Expression} node
* @returns {boolean}
*/
function hasFunctionType(node) {
if (isFunctionIdentifier(node)) {
return true
}
if (node.type === 'ArrayExpression') {
return node.elements.some(isFunctionIdentifier)
}
return false
}
return utils.defineVueVisitor(context, {
onVueObjectEnter(node) {
for (const prop of utils.getComponentProps(node)) {
Expand All @@ -86,6 +107,11 @@ module.exports = {
if (!def) {
continue
}
const type = utils.findProperty(prop.value, 'type')
if (type && hasFunctionType(type.value)) {
// ignore function type
continue
}
if (def.value.type !== 'FunctionExpression') {
continue
}
Expand Down
25 changes: 25 additions & 0 deletions tests/lib/rules/no-deprecated-props-default-this.js
Expand Up @@ -96,6 +96,31 @@ ruleTester.run('no-deprecated-props-default-this', rule, {
}
</script>
`
},
{
// https://github.com/vuejs/eslint-plugin-vue/issues/1464
filename: 'test.vue',
code: `
<template>
<button @click="printMessage">Print message</button>
</template>

<script>

export default {
name: 'App',
props: {
message: String,
printMessage: {
type: Function,
default() {
console.log(this.message);
}
}
}
}
</script>
`
}
],

Expand Down