Skip to content

Commit 6da3cae

Browse files
ota-meshimichalsnik
authored andcommitted
⭐️New: Add vue/arrow-spacing rule (#767)
1 parent 99e9607 commit 6da3cae

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-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: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
code: `
30+
<template>
31+
<div :attr="()=>a" />
32+
</template>`,
33+
options: [{ before: false, after: false }]
34+
}
35+
],
36+
invalid: [
37+
{
38+
code: `
39+
<template>
40+
<div :attr="()=>a" />
41+
</template>`,
42+
output: `
43+
<template>
44+
<div :attr="() => a" />
45+
</template>`,
46+
errors: [
47+
{
48+
message: 'Missing space before =>.',
49+
line: 3
50+
},
51+
{
52+
message: 'Missing space after =>.',
53+
line: 3
54+
}
55+
]
56+
},
57+
{
58+
code: `
59+
<template>
60+
<div @click="()=>a" />
61+
</template>`,
62+
output: `
63+
<template>
64+
<div @click="() => a" />
65+
</template>`,
66+
errors: [
67+
{
68+
message: 'Missing space before =>.',
69+
line: 3
70+
},
71+
{
72+
message: 'Missing space after =>.',
73+
line: 3
74+
}
75+
]
76+
},
77+
{
78+
code: `
79+
<template>
80+
<div @click="
81+
const fn = ()=>a
82+
fn()
83+
" />
84+
</template>`,
85+
output: `
86+
<template>
87+
<div @click="
88+
const fn = () => a
89+
fn()
90+
" />
91+
</template>`,
92+
errors: [
93+
{
94+
message: 'Missing space before =>.',
95+
line: 4
96+
},
97+
{
98+
message: 'Missing space after =>.',
99+
line: 4
100+
}
101+
]
102+
},
103+
{
104+
code: `
105+
<template>
106+
<div :attr="() => a" />
107+
</template>`,
108+
options: [{ before: false, after: false }],
109+
output: `
110+
<template>
111+
<div :attr="()=>a" />
112+
</template>`,
113+
errors: [
114+
{
115+
message: 'Unexpected space before =>.',
116+
line: 3
117+
},
118+
{
119+
message: 'Unexpected space after =>.',
120+
line: 3
121+
}
122+
]
123+
}
124+
]
125+
})

0 commit comments

Comments
 (0)