Skip to content

⭐️New: Add vue/v-on-parens rule #481

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

Merged
merged 9 commits into from
Feb 3, 2019
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ Enforce all the rules in this category, as well as all higher priority rules, wi
| | [vue/no-confusing-v-for-v-if](./docs/rules/no-confusing-v-for-v-if.md) | disallow confusing `v-for` and `v-if` on the same element |
| :wrench: | [vue/order-in-components](./docs/rules/order-in-components.md) | enforce order of properties in components |
| | [vue/this-in-template](./docs/rules/this-in-template.md) | enforce usage of `this` in template |
| :wrench: | [vue/v-on-parens](./docs/rules/v-on-parens.md) | enforce or forbid parentheses after method calls without arguments in `v-on` directives |

### Uncategorized

Expand Down
66 changes: 66 additions & 0 deletions docs/rules/v-on-parens.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# enforce or forbid parentheses after method calls without arguments in `v-on` directives (vue/v-on-parens)

- :gear: This rule is included in `"plugin:vue/recommended"`.
- :wrench: The `--fix` option on the [command line](http://eslint.org/docs/user-guide/command-line-interface#fix) can automatically fix some of the problems reported by this rule.

## :book: Rule Details

:-1: Example of **incorrect** code for this rule:

```html
<button v-on:click="closeModal()">
Close
</button>
```

:+1: Example of **correct** code for this rule:

```html
<button v-on:click="closeModal">
Close
</button>
```

## :wrench: Options

Default is set to `never`.

```
'vue/v-on-parens': [2, 'always'|'never']
```

### `"always"` - Always use parentheses in `v-on` directives

:-1: Example of **incorrect** code:

```html
<button v-on:click="closeModal">
Close
</button>
```

:+1: Example of **correct** code:

```html
<button v-on:click="closeModal()">
Close
</button>
```

### `"never"` - Never use parentheses in `v-on` directives for method calls without arguments

:-1: Example of **incorrect** code:

```html
<button v-on:click="closeModal()">
Close
</button>
```

:+1: Example of **correct** code:

```html
<button v-on:click="closeModal">
Close
</button>
```
3 changes: 2 additions & 1 deletion lib/configs/recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
'vue/html-quotes': 'error',
'vue/no-confusing-v-for-v-if': 'error',
'vue/order-in-components': 'error',
'vue/this-in-template': 'error'
'vue/this-in-template': 'error',
'vue/v-on-parens': 'error'
}
}
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = {
'script-indent': require('./rules/script-indent'),
'this-in-template': require('./rules/this-in-template'),
'v-bind-style': require('./rules/v-bind-style'),
'v-on-parens': require('./rules/v-on-parens'),
'v-on-style': require('./rules/v-on-style'),
'valid-template-root': require('./rules/valid-template-root'),
'valid-v-bind': require('./rules/valid-v-bind'),
Expand Down
60 changes: 60 additions & 0 deletions lib/rules/v-on-parens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @author Niklas Higi
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const utils = require('../utils')

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description: 'enforce or forbid parentheses after method calls without arguments in `v-on` directives',
category: 'recommended',
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.5.0/docs/rules/v-on-parens.md'
},
fixable: 'code',
schema: [
{ enum: ['always', 'never'] }
]
},

create (context) {
const always = context.options[0] === 'always'

return utils.defineTemplateBodyVisitor(context, {
"VAttribute[directive=true][key.name='on'][key.argument!=null] VOnExpression > ExpressionStatement > *" (node) {
const hasParens = node.type === 'CallExpression'
if (hasParens && node.arguments.length > 0) return

if (always && !hasParens) {
context.report({
node,
loc: node.loc,
message: "Method calls inside of 'v-on' directives must have parentheses.",
fix: fixer => fixer.insertTextAfter(node, '()')
})
} else if (!always && hasParens) {
context.report({
node,
loc: node.loc,
message: "Method calls without arguments inside of 'v-on' directives must not have parentheses.",
fix: fixer => {
const nodeString = context.getSourceCode().getText().substring(node.range[0], node.range[1])
// This ensures that parens are also removed if they contain whitespace
const parensLength = nodeString.match(/\(\s*\)\s*$/)[0].length
return fixer.removeRange([node.end - parensLength, node.end])
}
})
}
}
})
}
}
63 changes: 63 additions & 0 deletions tests/lib/rules/v-on-parens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @author Niklas Higi
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/v-on-parens')

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

const tester = new RuleTester({
parser: 'vue-eslint-parser',
parserOptions: { ecmaVersion: 2015 }
})

tester.run('v-on-parens', rule, {
valid: [
{
filename: 'test.vue',
code: ''
},
{
filename: 'test.vue',
code: '<template><div @click="foo(123)"></div></template>',
options: ['always']
},
{
filename: 'test.vue',
code: '<template><div @click="foo(123)"></div></template>',
options: ['never']
},
{
filename: 'test.vue',
code: '<template><div @click="foo()"></div></template>',
options: ['always']
},
{
filename: 'test.vue',
code: '<template><div @click="foo"></div></template>',
options: ['never']
}
],
invalid: [
{
filename: 'test.vue',
code: '<template><div @click="foo"></div></template>',
errors: ["Method calls inside of 'v-on' directives must have parentheses."],
options: ['always']
},
{
filename: 'test.vue',
code: '<template><div @click="foo()"></div></template>',
errors: ["Method calls without arguments inside of 'v-on' directives must not have parentheses."],
options: ['never']
}
]
})