Skip to content

Commit 625e271

Browse files
authored
Add vue/space-in-parens rule (#1157)
* ⭐️New: Add `vue/space-in-parens` rule * update * update * update
1 parent 812f6bb commit 625e271

File tree

5 files changed

+248
-1
lines changed

5 files changed

+248
-1
lines changed

Diff for: docs/rules/space-in-parens.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/space-in-parens
5+
description: enforce consistent spacing inside parentheses
6+
---
7+
# vue/space-in-parens
8+
> enforce consistent spacing inside parentheses
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 [space-in-parens] rule but it applies to the expressions in `<template>`.
13+
14+
## :books: Further reading
15+
16+
- [space-in-parens]
17+
18+
[space-in-parens]: https://eslint.org/docs/rules/space-in-parens
19+
20+
## :mag: Implementation
21+
22+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/space-in-parens.js)
23+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/space-in-parens.js)

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

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = {
3232
'vue/padding-line-between-blocks': 'off',
3333
'vue/script-indent': 'off',
3434
'vue/singleline-html-element-content-newline': 'off',
35+
'vue/space-in-parens': 'off',
3536
'vue/space-infix-ops': 'off',
3637
'vue/space-unary-ops': 'off',
3738
'vue/template-curly-spacing': 'off'

Diff for: lib/rules/space-in-parens.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, no-invalid-meta-docs-categories
9+
module.exports = wrapCoreRule(
10+
require('eslint/lib/rules/space-in-parens'),
11+
{ skipDynamicArguments: true, skipDynamicArgumentsReport: true }
12+
)

Diff for: lib/utils/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ function wrapContextToOverrideReportMethodToSkipDynamicArgument (context) {
117117
report (descriptor, ...args) {
118118
let range = null
119119
if (descriptor.loc) {
120-
range = [sourceCode.getIndexFromLoc(descriptor.loc.start), sourceCode.getIndexFromLoc(descriptor.loc.end)]
120+
const startLoc = isLoc(descriptor.loc.start) ? descriptor.loc.start : descriptor.loc
121+
const endLoc = descriptor.loc.end || startLoc
122+
range = [sourceCode.getIndexFromLoc(startLoc), sourceCode.getIndexFromLoc(endLoc)]
121123
} else if (descriptor.node) {
122124
range = descriptor.node.range
123125
}
@@ -131,6 +133,10 @@ function wrapContextToOverrideReportMethodToSkipDynamicArgument (context) {
131133
context.report(descriptor, ...args)
132134
}
133135
}
136+
137+
function isLoc (loc) {
138+
return loc && typeof loc === 'object' && typeof loc.line === 'number' && typeof loc.column === 'number'
139+
}
134140
}
135141

136142
// ------------------------------------------------------------------------------

Diff for: tests/lib/rules/space-in-parens.js

+205
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/**
2+
* @author Yosuke Ota
3+
*/
4+
'use strict'
5+
6+
const { RuleTester, CLIEngine } = require('eslint')
7+
const semver = require('semver')
8+
const rule = require('../../../lib/rules/space-in-parens')
9+
10+
const errorMessage = semver.lt(CLIEngine.version, '6.4.0')
11+
? (obj) => {
12+
const messageId = obj.messageId
13+
delete obj.messageId
14+
obj.message = messageId.startsWith('missing') ? 'There must be a space inside this paren.' : 'There should be no spaces inside this paren.'
15+
return obj
16+
}
17+
: obj => obj
18+
19+
const tester = new RuleTester({
20+
parser: require.resolve('vue-eslint-parser'),
21+
parserOptions: { ecmaVersion: 2015 }
22+
})
23+
24+
tester.run('space-in-parens', rule, {
25+
valid: [
26+
`<template>
27+
<button
28+
@click="foo(arg)"
29+
/>
30+
</template>`,
31+
{
32+
code: `
33+
<template>
34+
<button
35+
@click="foo( arg )"
36+
/>
37+
</template>`,
38+
options: ['always']
39+
},
40+
`
41+
<template>
42+
<button
43+
:[foo(arg)]="foo(arg)"
44+
/>
45+
</template>`,
46+
{
47+
code: `
48+
<template>
49+
<button
50+
:[foo(arg)]="foo( arg )"
51+
/>
52+
</template>`,
53+
options: ['always']
54+
}
55+
],
56+
invalid: [
57+
{
58+
code: `
59+
<template>
60+
<button
61+
@click="foo( arg )"
62+
/>
63+
</template>`,
64+
output: `
65+
<template>
66+
<button
67+
@click="foo(arg)"
68+
/>
69+
</template>`,
70+
errors: [
71+
errorMessage({
72+
messageId: 'rejectedOpeningSpace',
73+
line: 4
74+
}),
75+
errorMessage({
76+
messageId: 'rejectedClosingSpace',
77+
line: 4
78+
})
79+
]
80+
},
81+
{
82+
code: `
83+
<template>
84+
<button
85+
@click="foo(arg)"
86+
/>
87+
</template>`,
88+
options: ['always'],
89+
output: `
90+
<template>
91+
<button
92+
@click="foo( arg )"
93+
/>
94+
</template>`,
95+
errors: [
96+
errorMessage({
97+
messageId: 'missingOpeningSpace',
98+
line: 4
99+
}),
100+
errorMessage({
101+
messageId: 'missingClosingSpace',
102+
line: 4
103+
})
104+
]
105+
},
106+
{
107+
code: `
108+
<template>
109+
<input
110+
:value="( 1 + 2 ) + 3"
111+
>
112+
</template>`,
113+
output: `
114+
<template>
115+
<input
116+
:value="(1 + 2) + 3"
117+
>
118+
</template>`,
119+
errors: [
120+
errorMessage({
121+
messageId: 'rejectedOpeningSpace',
122+
line: 4
123+
}),
124+
errorMessage({
125+
messageId: 'rejectedClosingSpace',
126+
line: 4
127+
})
128+
]
129+
},
130+
{
131+
code: `
132+
<template>
133+
<input
134+
:value="(1 + 2) + 3"
135+
>
136+
</template>`,
137+
options: ['always'],
138+
output: `
139+
<template>
140+
<input
141+
:value="( 1 + 2 ) + 3"
142+
>
143+
</template>`,
144+
errors: [
145+
errorMessage({
146+
messageId: 'missingOpeningSpace',
147+
line: 4
148+
}),
149+
errorMessage({
150+
messageId: 'missingClosingSpace',
151+
line: 4
152+
})
153+
]
154+
},
155+
{
156+
code: `
157+
<template>
158+
<input
159+
:[(1+2)]="( 1 + 2 ) + 3"
160+
>
161+
</template>`,
162+
output: `
163+
<template>
164+
<input
165+
:[(1+2)]="(1 + 2) + 3"
166+
>
167+
</template>`,
168+
errors: [
169+
errorMessage({
170+
messageId: 'rejectedOpeningSpace',
171+
line: 4
172+
}),
173+
errorMessage({
174+
messageId: 'rejectedClosingSpace',
175+
line: 4
176+
})
177+
]
178+
},
179+
{
180+
code: `
181+
<template>
182+
<input
183+
:[(1+2)]="(1 + 2) + 3"
184+
>
185+
</template>`,
186+
options: ['always'],
187+
output: `
188+
<template>
189+
<input
190+
:[(1+2)]="( 1 + 2 ) + 3"
191+
>
192+
</template>`,
193+
errors: [
194+
errorMessage({
195+
messageId: 'missingOpeningSpace',
196+
line: 4
197+
}),
198+
errorMessage({
199+
messageId: 'missingClosingSpace',
200+
line: 4
201+
})
202+
]
203+
}
204+
]
205+
})

0 commit comments

Comments
 (0)