Skip to content

Commit 7a9730b

Browse files
committed
⭐️New: Add vue/keyword-spacing rule
1 parent a8b2ca0 commit 7a9730b

File tree

6 files changed

+177
-0
lines changed

6 files changed

+177
-0
lines changed

Diff for: docs/rules/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ For example:
148148
| [vue/component-name-in-template-casing](./component-name-in-template-casing.md) | enforce specific casing for the component naming style in template | :wrench: |
149149
| [vue/eqeqeq](./eqeqeq.md) | require the use of `===` and `!==` | :wrench: |
150150
| [vue/key-spacing](./key-spacing.md) | enforce consistent spacing between keys and values in object literal properties | :wrench: |
151+
| [vue/keyword-spacing](./keyword-spacing.md) | enforce consistent spacing before and after keywords | :wrench: |
151152
| [vue/match-component-file-name](./match-component-file-name.md) | require component name property to match its file name | |
152153
| [vue/no-restricted-syntax](./no-restricted-syntax.md) | disallow specified syntax | |
153154
| [vue/object-curly-spacing](./object-curly-spacing.md) | enforce consistent spacing inside braces | :wrench: |

Diff for: docs/rules/keyword-spacing.md

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

Diff for: lib/configs/no-layout-rules.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
'vue/html-quotes': 'off',
1717
'vue/html-self-closing': 'off',
1818
'vue/key-spacing': 'off',
19+
'vue/keyword-spacing': 'off',
1920
'vue/max-attributes-per-line': 'off',
2021
'vue/multiline-html-element-content-newline': 'off',
2122
'vue/mustache-interpolation-spacing': 'off',

Diff for: lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = {
2626
'html-self-closing': require('./rules/html-self-closing'),
2727
'jsx-uses-vars': require('./rules/jsx-uses-vars'),
2828
'key-spacing': require('./rules/key-spacing'),
29+
'keyword-spacing': require('./rules/keyword-spacing'),
2930
'match-component-file-name': require('./rules/match-component-file-name'),
3031
'max-attributes-per-line': require('./rules/max-attributes-per-line'),
3132
'multiline-html-element-content-newline': require('./rules/multiline-html-element-content-newline'),

Diff for: lib/rules/keyword-spacing.js

+9
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/keyword-spacing'))

Diff for: tests/lib/rules/keyword-spacing.js

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/**
2+
* @author Yosuke Ota
3+
*/
4+
'use strict'
5+
6+
const RuleTester = require('eslint').RuleTester
7+
const rule = require('../../../lib/rules/keyword-spacing')
8+
9+
const tester = new RuleTester({
10+
parser: 'vue-eslint-parser',
11+
parserOptions: { ecmaVersion: 2015 }
12+
})
13+
14+
tester.run('keyword-spacing', rule, {
15+
valid: [
16+
`<template>
17+
<div @event="
18+
if (foo) {
19+
//...
20+
} else if (bar) {
21+
//...
22+
} else {
23+
//...
24+
}
25+
" />
26+
</template>`,
27+
{
28+
code:
29+
`<template>
30+
<div @event="
31+
if(foo) {
32+
//...
33+
}else if(bar) {
34+
//...
35+
}else{
36+
//...
37+
}
38+
" />
39+
</template>`,
40+
options: [{ before: false, after: false }]
41+
}
42+
],
43+
invalid: [
44+
{
45+
code:
46+
`<template>
47+
<div @event="
48+
if(foo) {
49+
//...
50+
}else if(bar) {
51+
//...
52+
}else{
53+
//...
54+
}
55+
" />
56+
</template>`,
57+
output:
58+
`<template>
59+
<div @event="
60+
if (foo) {
61+
//...
62+
} else if (bar) {
63+
//...
64+
} else {
65+
//...
66+
}
67+
" />
68+
</template>`,
69+
errors: [
70+
{
71+
message: 'Expected space(s) after "if".',
72+
line: 3
73+
},
74+
{
75+
message: 'Expected space(s) before "else".',
76+
line: 5
77+
},
78+
{
79+
message: 'Expected space(s) after "if".',
80+
line: 5
81+
},
82+
{
83+
message: 'Expected space(s) before "else".',
84+
line: 7
85+
},
86+
{
87+
message: 'Expected space(s) after "else".',
88+
line: 7
89+
}
90+
]
91+
},
92+
{
93+
code:
94+
`<template>
95+
<div @event="
96+
if (foo) {
97+
//...
98+
} else if (bar) {
99+
//...
100+
} else {
101+
//...
102+
}
103+
" />
104+
</template>`,
105+
options: [{ before: false, after: false }],
106+
output:
107+
`<template>
108+
<div @event="
109+
if(foo) {
110+
//...
111+
}else if(bar) {
112+
//...
113+
}else{
114+
//...
115+
}
116+
" />
117+
</template>`,
118+
errors: [
119+
{
120+
message: 'Unexpected space(s) after "if".',
121+
line: 3
122+
},
123+
{
124+
message: 'Unexpected space(s) before "else".',
125+
line: 5
126+
},
127+
{
128+
message: 'Unexpected space(s) after "if".',
129+
line: 5
130+
},
131+
{
132+
message: 'Unexpected space(s) before "else".',
133+
line: 7
134+
},
135+
{
136+
message: 'Unexpected space(s) after "else".',
137+
line: 7
138+
}
139+
]
140+
}
141+
]
142+
})

0 commit comments

Comments
 (0)