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(v-on-handler-style): fixed the breaking change caused by the auto-fix on functions without parameters(#2538) #2539

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/rules/v-on-handler-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ module.exports = {
return null
}
const paramCount = methodParamCountMap.get(idCallExpr.callee.name)
// disable the auto-fixed when the node does't have params
if (idCallExpr.arguments.length === 0 || paramCount === 0) {
return null
}
if (paramCount != null && paramCount > 0) {
// The behavior of target method can change given the arguments.
return null
Expand Down Expand Up @@ -368,7 +372,8 @@ module.exports = {
) {
return null
}
if (!isSameParamsAndArgs(idCallExpr)) {
// disable the auto-fixed when the node does't have params
if (node.params.length === 0 || !isSameParamsAndArgs(idCallExpr)) {
// It is not a call with the arguments given as is.
return null
}
Expand Down
65 changes: 12 additions & 53 deletions tests/lib/rules/v-on-handler-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ tester.run('v-on-handler-style', rule, {
<button @click="foo()" />
<button @click="() => foo()" />
</template>`,
output: `<template>
<button @click="foo" />
<button @click="foo" />
<button @click="foo" />
</template>`,
output: null,
heggria marked this conversation as resolved.
Show resolved Hide resolved
options: [['method', 'inline-function']],
errors: [
{
Expand All @@ -107,11 +103,7 @@ tester.run('v-on-handler-style', rule, {
<button @click="foo()" />
<button @click="() => foo()" />
</template>`,
output: `<template>
<button @click="foo" />
<button @click="foo" />
<button @click="foo" />
</template>`,
output: null,
errors: [
{
message: 'Prefer method handler over inline handler in v-on.',
Expand Down Expand Up @@ -181,7 +173,7 @@ tester.run('v-on-handler-style', rule, {
{
filename: 'test.vue',
code: '<template><div @click="foo( )" /></template>',
output: `<template><div @click="foo" /></template>`,
output: null,
options: [['method', 'inline-function']],
errors: [
{
Expand Down Expand Up @@ -266,13 +258,7 @@ tester.run('v-on-handler-style', rule, {
<button @click="{ fn(); }" />
<button @click="{(fn());;;}" />
</template>`,
output: `
<template>
<button @click="fn" />
<button @click="fn" />
<button @click="fn" />
<button @click="fn" />
</template>`,
output: null,
options: [['method', 'inline-function']],
errors: [
{
Expand Down Expand Up @@ -304,11 +290,7 @@ tester.run('v-on-handler-style', rule, {
<div @click=" beforeSpace()" />
<div @click='afterSpace() ' />
</template>`,
output: `
<template>
<div @click="beforeSpace" />
<div @click='afterSpace' />
</template>`,
output: null,
options: [['method', 'inline-function']],
errors: [
{
Expand All @@ -329,10 +311,7 @@ tester.run('v-on-handler-style', rule, {
<template>
<button @click=" &#x66;oo ( ) " />
</template>`,
output: `
<template>
<button @click="&#x66;oo" />
</template>`,
output: null,
options: [['method', 'inline-function']],
errors: [
{
Expand All @@ -353,15 +332,7 @@ tester.run('v-on-handler-style', rule, {
}
}
</script>`,
output: `
<template><button @click="foo" /></template>
<script>
export default {
methods: {
foo() {}
}
}
</script>`,
output: null,
options: [['method', 'inline-function']],
errors: [
{
Expand All @@ -382,15 +353,7 @@ tester.run('v-on-handler-style', rule, {
}
}
</script>`,
output: `
<template><button @click="foo" /></template>
<script>
export default {
methods: {
foo: () => {}
}
}
</script>`,
output: null,
options: [['method', 'inline-function']],
errors: [
{
Expand Down Expand Up @@ -545,11 +508,7 @@ tester.run('v-on-handler-style', rule, {
<button @click="e()" />
</template>
</template>`,
output: `<template>
<template v-for="e in list">
<button @click="e" />
</template>
</template>`,
output: null,
options: [['method', 'inline-function']],
errors: [
{
Expand Down Expand Up @@ -716,9 +675,9 @@ tester.run('v-on-handler-style', rule, {
<button @click="() => { foo() }" />
</template>`,
output: `<template>
<button @click="() => foo()" />
<button @click="foo" />
<button @click="foo" />
<button @click="foo" />
<button @click="() => { foo() }" />
</template>`,
options: [['method', 'inline']],
errors: [
Expand Down Expand Up @@ -1008,7 +967,7 @@ tester.run('v-on-handler-style', rule, {
</template>`,
output: `<template>
<template v-for="e in list">
<button @click="e" />
<button @click="() => e()" />
<button @click="e" />
<button @click="e" />
</template>
Expand Down