Skip to content

Commit febc727

Browse files
ota-meshimysticatea
authored andcommitted
New: add vue/keyword-spacing rule (#795)
1 parent 66a252d commit febc727

File tree

6 files changed

+198
-0
lines changed

6 files changed

+198
-0
lines changed

docs/rules/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ For example:
149149
| [vue/dot-location](./dot-location.md) | enforce consistent newlines before and after dots | :wrench: |
150150
| [vue/eqeqeq](./eqeqeq.md) | require the use of `===` and `!==` | :wrench: |
151151
| [vue/key-spacing](./key-spacing.md) | enforce consistent spacing between keys and values in object literal properties | :wrench: |
152+
| [vue/keyword-spacing](./keyword-spacing.md) | enforce consistent spacing before and after keywords | :wrench: |
152153
| [vue/match-component-file-name](./match-component-file-name.md) | require component name property to match its file name | |
153154
| [vue/no-boolean-default](./no-boolean-default.md) | disallow boolean defaults | :wrench: |
154155
| [vue/no-empty-pattern](./no-empty-pattern.md) | disallow empty destructuring patterns | |

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)

lib/configs/no-layout-rules.js

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

lib/index.js

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

lib/rules/keyword-spacing.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @author Yosuke Ota
3+
*/
4+
'use strict'
5+
6+
const { wrapCoreRule } = require('../utils')
7+
8+
// eslint-disable-next-line no-invalid-meta
9+
module.exports = wrapCoreRule(
10+
require('eslint/lib/rules/keyword-spacing'),
11+
{ skipDynamicArguments: true }
12+
)

tests/lib/rules/keyword-spacing.js

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
`<template>
43+
<div :[(function(){return(1)})()]="val" />
44+
</template>`
45+
],
46+
invalid: [
47+
{
48+
code:
49+
`<template>
50+
<div @event="
51+
if(foo) {
52+
//...
53+
}else if(bar) {
54+
//...
55+
}else{
56+
//...
57+
}
58+
" />
59+
</template>`,
60+
output:
61+
`<template>
62+
<div @event="
63+
if (foo) {
64+
//...
65+
} else if (bar) {
66+
//...
67+
} else {
68+
//...
69+
}
70+
" />
71+
</template>`,
72+
errors: [
73+
{
74+
message: 'Expected space(s) after "if".',
75+
line: 3
76+
},
77+
{
78+
message: 'Expected space(s) before "else".',
79+
line: 5
80+
},
81+
{
82+
message: 'Expected space(s) after "if".',
83+
line: 5
84+
},
85+
{
86+
message: 'Expected space(s) before "else".',
87+
line: 7
88+
},
89+
{
90+
message: 'Expected space(s) after "else".',
91+
line: 7
92+
}
93+
]
94+
},
95+
{
96+
code:
97+
`<template>
98+
<div @event="
99+
if (foo) {
100+
//...
101+
} else if (bar) {
102+
//...
103+
} else {
104+
//...
105+
}
106+
" />
107+
</template>`,
108+
options: [{ before: false, after: false }],
109+
output:
110+
`<template>
111+
<div @event="
112+
if(foo) {
113+
//...
114+
}else if(bar) {
115+
//...
116+
}else{
117+
//...
118+
}
119+
" />
120+
</template>`,
121+
errors: [
122+
{
123+
message: 'Unexpected space(s) after "if".',
124+
line: 3
125+
},
126+
{
127+
message: 'Unexpected space(s) before "else".',
128+
line: 5
129+
},
130+
{
131+
message: 'Unexpected space(s) after "if".',
132+
line: 5
133+
},
134+
{
135+
message: 'Unexpected space(s) before "else".',
136+
line: 7
137+
},
138+
{
139+
message: 'Unexpected space(s) after "else".',
140+
line: 7
141+
}
142+
]
143+
},
144+
{
145+
code:
146+
`<template>
147+
<div :[(function(){return(1)})()]="(function(){return(1)})()" />
148+
</template>`,
149+
output:
150+
`<template>
151+
<div :[(function(){return(1)})()]="(function(){return (1)})()" />
152+
</template>`,
153+
errors: [
154+
{
155+
message: 'Expected space(s) after "return".',
156+
line: 2
157+
}]
158+
}
159+
]
160+
})

0 commit comments

Comments
 (0)