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 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
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ For example:
| [vue/script-indent](./script-indent.md) | enforce consistent indentation in `<script>` | :wrench: |
| [vue/space-infix-ops](./space-infix-ops.md) | require spacing around infix operators | :wrench: |
| [vue/space-unary-ops](./space-unary-ops.md) | enforce consistent spacing before or after unary operators | :wrench: |
| [vue/v-on-function-call](./v-on-function-call.md) | enforce or forbid parentheses after method calls without arguments in `v-on` directives | :wrench: |

## Deprecated

Expand Down
77 changes: 77 additions & 0 deletions docs/rules/v-on-function-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/v-on-function-call
description: enforce or forbid parentheses after method calls without arguments in `v-on` directives
---
# vue/v-on-function-call
> enforce or forbid parentheses after method calls without arguments in `v-on` directives

- :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.

## :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>
```

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/v-on-function-call.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/v-on-function-call.js)
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ module.exports = {
'this-in-template': require('./rules/this-in-template'),
'use-v-on-exact': require('./rules/use-v-on-exact'),
'v-bind-style': require('./rules/v-bind-style'),
'v-on-function-call': require('./rules/v-on-function-call'),
'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-function-call.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: {
type: 'suggestion',
docs: {
description: 'enforce or forbid parentheses after method calls without arguments in `v-on` directives',
category: undefined,
url: 'https://eslint.vuejs.org/rules/v-on-function-call.html'
},
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] > VExpressionContainer > Identifier" (node) {
if (!always) return
context.report({
node,
loc: node.loc,
message: "Method calls inside of 'v-on' directives must have parentheses."
})
},

"VAttribute[directive=true][key.name='on'][key.argument!=null] VOnExpression > ExpressionStatement > *" (node) {
if (!always && node.type === 'CallExpression' && node.arguments.length === 0) {
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])
}
})
}
}
})
}
}
72 changes: 72 additions & 0 deletions tests/lib/rules/v-on-function-call.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @author Niklas Higi
*/
'use strict'

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

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

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

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

tester.run('v-on-function-call', 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>',
output: `<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>',
output: `<template><div @click="foo"></div></template>`,
errors: ["Method calls without arguments inside of 'v-on' directives must not have parentheses."],
options: ['never']
},
{
filename: 'test.vue',
code: '<template><div @click="foo( )"></div></template>',
output: `<template><div @click="foo"></div></template>`,
errors: ["Method calls without arguments inside of 'v-on' directives must not have parentheses."],
options: ['never']
}
]
})