Skip to content

Commit 2f45da4

Browse files
committed
⭐️New: Add vue/arrow-spacing rule
1 parent 9c49dcc commit 2f45da4

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-0
lines changed

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ For example:
140140
| Rule ID | Description | |
141141
|:--------|:------------|:---|
142142
| [vue/array-bracket-spacing](./array-bracket-spacing.md) | enforce consistent spacing inside array brackets | :wrench: |
143+
| [vue/arrow-spacing](./arrow-spacing.md) | enforce consistent spacing before and after the arrow in arrow functions | :wrench: |
143144
| [vue/component-name-in-template-casing](./component-name-in-template-casing.md) | enforce specific casing for the component naming style in template | :wrench: |
144145
| [vue/eqeqeq](./eqeqeq.md) | require the use of `===` and `!==` | :wrench: |
145146
| [vue/key-spacing](./key-spacing.md) | enforce consistent spacing between keys and values in object literal properties | :wrench: |

docs/rules/arrow-spacing.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
pageClass: rule-details
3+
sidebarDepth: 0
4+
title: vue/arrow-spacing
5+
description: enforce consistent spacing before and after the arrow in arrow functions
6+
---
7+
# vue/arrow-spacing
8+
> enforce consistent spacing before and after the arrow in arrow functions
9+
10+
- :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.
11+
12+
This rule is the same rule as core [arrow-spacing] rule but it applies to the expressions in `<template>`.
13+
14+
## :books: Further reading
15+
16+
- [arrow-spacing]
17+
18+
[arrow-spacing]: https://eslint.org/docs/rules/arrow-spacing
19+
20+
## :mag: Implementation
21+
22+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/arrow-spacing.js)
23+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/arrow-spacing.js)

lib/configs/no-layout-rules.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
module.exports = {
77
rules: {
88
'vue/array-bracket-spacing': 'off',
9+
'vue/arrow-spacing': 'off',
910
'vue/html-closing-bracket-newline': 'off',
1011
'vue/html-closing-bracket-spacing': 'off',
1112
'vue/html-indent': 'off',

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
module.exports = {
99
rules: {
1010
'array-bracket-spacing': require('./rules/array-bracket-spacing'),
11+
'arrow-spacing': require('./rules/arrow-spacing'),
1112
'attribute-hyphenation': require('./rules/attribute-hyphenation'),
1213
'attributes-order': require('./rules/attributes-order'),
1314
'comment-directive': require('./rules/comment-directive'),

lib/rules/arrow-spacing.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @author Yosuke Ota
3+
*/
4+
'use strict'
5+
6+
const { wrapCoreRule } = require('../utils')
7+
8+
// eslint-disable-next-line
9+
module.exports = wrapCoreRule(require('eslint/lib/rules/arrow-spacing'))

tests/lib/rules/arrow-spacing.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @author Yosuke Ota
3+
*/
4+
'use strict'
5+
6+
const RuleTester = require('eslint').RuleTester
7+
const rule = require('../../../lib/rules/arrow-spacing')
8+
9+
const tester = new RuleTester({
10+
parser: 'vue-eslint-parser',
11+
parserOptions: { ecmaVersion: 2015 }
12+
})
13+
14+
tester.run('arrow-spacing', rule, {
15+
valid: [
16+
`<template>
17+
<div :attr="() => a" />
18+
</template>`,
19+
`<template>
20+
<div @click="() => a" />
21+
</template>`,
22+
`<template>
23+
<div @click="
24+
const fn = () => a
25+
fn()
26+
" />
27+
</template>`
28+
],
29+
invalid: [
30+
{
31+
code: `
32+
<template>
33+
<div :attr="()=>a" />
34+
</template>`,
35+
output: `
36+
<template>
37+
<div :attr="() => a" />
38+
</template>`,
39+
errors: [
40+
{
41+
message: 'Missing space before =>.',
42+
line: 3
43+
},
44+
{
45+
message: 'Missing space after =>.',
46+
line: 3
47+
}
48+
]
49+
},
50+
{
51+
code: `
52+
<template>
53+
<div @click="()=>a" />
54+
</template>`,
55+
output: `
56+
<template>
57+
<div @click="() => a" />
58+
</template>`,
59+
errors: [
60+
{
61+
message: 'Missing space before =>.',
62+
line: 3
63+
},
64+
{
65+
message: 'Missing space after =>.',
66+
line: 3
67+
}
68+
]
69+
},
70+
{
71+
code: `
72+
<template>
73+
<div @click="
74+
const fn = ()=>a
75+
fn()
76+
" />
77+
</template>`,
78+
output: `
79+
<template>
80+
<div @click="
81+
const fn = () => a
82+
fn()
83+
" />
84+
</template>`,
85+
errors: [
86+
{
87+
message: 'Missing space before =>.',
88+
line: 4
89+
},
90+
{
91+
message: 'Missing space after =>.',
92+
line: 4
93+
}
94+
]
95+
}
96+
]
97+
})

0 commit comments

Comments
 (0)