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 all 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
2 changes: 1 addition & 1 deletion docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ Rules in this category are enabled for all presets provided by eslint-plugin-vue
| [vue/singleline-html-element-content-newline](./singleline-html-element-content-newline.md) | require a line break before and after the contents of a singleline element | :wrench: | :three::two::lipstick: |
| [vue/v-bind-style](./v-bind-style.md) | enforce `v-bind` directive style | :wrench: | :three::two::hammer: |
| [vue/v-on-event-hyphenation](./v-on-event-hyphenation.md) | enforce v-on event naming style on custom components in template | :wrench: | :three::hammer: |
| [vue/v-on-style](./v-on-style.md) | enforce `v-on` directive style | :wrench: | :three::two::hammer: |
| [vue/v-on-style](./v-on-style.md) | enforce `v-on` directive style | :wrench::bulb: | :three::two::hammer: |
| [vue/v-slot-style](./v-slot-style.md) | enforce `v-slot` directive style | :wrench: | :three::two::hammer: |

</rules-table>
Expand Down
1 change: 1 addition & 0 deletions docs/rules/v-on-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ since: v3.0.0

- :gear: This rule is included in all of `"plugin:vue/vue3-strongly-recommended"`, `*.configs["flat/strongly-recommended"]`, `"plugin:vue/strongly-recommended"`, `*.configs["flat/vue2-strongly-recommended"]`, `"plugin:vue/vue3-recommended"`, `*.configs["flat/recommended"]`, `"plugin:vue/recommended"` and `*.configs["flat/vue2-recommended"]`.
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.
- :bulb: Some problems reported by this rule are manually fixable by editor [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).

## :book: Rule Details

Expand Down
66 changes: 61 additions & 5 deletions lib/rules/v-on-handler-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ module.exports = {
url: 'https://eslint.vuejs.org/rules/v-on-handler-style.html'
},
fixable: 'code',
hasSuggestions: true,
schema: [
{
oneOf: [
Expand Down Expand Up @@ -157,7 +158,11 @@ module.exports = {
preferInlineFunctionOverMethod:
'Prefer inline function over method handler in v-on.',
preferInlineFunctionOverInline:
'Prefer inline function over inline handler in v-on.'
'Prefer inline function over inline handler in v-on.',
removeParenthesesOverInline:
'Remove the parentheses from the inline handler in v-on.',
removeParenthesesOverInlineFunction:
'Remove the parentheses from the inline function in v-on.'
}
},
/** @param {RuleContext} context */
Expand Down Expand Up @@ -300,6 +305,36 @@ module.exports = {
return false
}

// disable the auto-fixed when the node does't have params
if (
!hasComment /* The statement contains comment and cannot be fixed. */ &&
idCallExpr /* The statement is not a simple identifier call and cannot be fixed. */ &&
idCallExpr.arguments.length === 0
) {
const paramCount = methodParamCountMap.get(idCallExpr.callee.name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this rule also has the number of arguments defined in the method. If we check that, I think some code can be safely fixed automatically.
If it's unsafe or we can't identify the number of arguments for the method, I think it's good to make it a suggestion.

if (
idCallExpr &&
(idCallExpr.arguments.length === 0 || paramCount === 0)
) {
context.report({
node,
messageId: 'preferMethodOverInline',
suggest: [
{
messageId: 'removeParenthesesOverInline',
fix(fixer) {
return fixer.replaceTextRange(
rangeWithoutQuotes,
context.getSourceCode().getText(idCallExpr.callee)
)
}
}
]
})
return true
}
}

context.report({
node,
messageId: idCallExpr
Expand All @@ -324,6 +359,7 @@ module.exports = {
)
}
})

return true
}
/**
Expand Down Expand Up @@ -356,6 +392,30 @@ module.exports = {
return false
}

// disable the auto-fixed when the node does't have params
if (
!hasComment /* The statement contains comment and cannot be fixed. */ &&
idCallExpr /* The statement is not a simple identifier call and cannot be fixed. */ &&
(node.params.length === 0 || !isSameParamsAndArgs(idCallExpr))
) {
context.report({
node,
messageId: 'preferMethodOverInlineFunction',
suggest: [
{
messageId: 'removeParenthesesOverInlineFunction',
fix(fixer) {
return fixer.replaceTextRange(
rangeWithoutQuotes,
context.getSourceCode().getText(idCallExpr.callee)
)
}
}
]
})
return true
}

context.report({
node,
messageId: idCallExpr
Expand All @@ -368,10 +428,6 @@ module.exports = {
) {
return null
}
if (!isSameParamsAndArgs(idCallExpr)) {
// It is not a call with the arguments given as is.
return null
}
const paramCount = methodParamCountMap.get(idCallExpr.callee.name)
if (
paramCount != null &&
Expand Down
Loading