Skip to content

Commit b363379

Browse files
authored
(Implements #535) Add "ignoreProperties" option to "no-multi-spaces" rule (#591)
* Add "ignoreProperties" option to "no-multi-spaces" rule * Handle extra case
1 parent 1b5a799 commit b363379

File tree

3 files changed

+147
-7
lines changed

3 files changed

+147
-7
lines changed

Diff for: docs/rules/no-multi-spaces.md

+41-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ Examples of **incorrect** code for this rule:
1616
:style="bar" />
1717
```
1818

19+
```html
20+
<i
21+
:class="{
22+
'fa-angle-up' : isExpanded,
23+
'fa-angle-down' : !isExpanded,
24+
}"
25+
/>
26+
```
27+
1928
Examples of **correct** code for this rule:
2029

2130
```html
@@ -25,6 +34,36 @@ Examples of **correct** code for this rule:
2534
/>
2635
```
2736

28-
### Options
37+
```html
38+
<i
39+
:class="{
40+
'fa-angle-up' : isExpanded,
41+
'fa-angle-down' : !isExpanded,
42+
}"
43+
/>
44+
```
45+
46+
## :wrench: Options
47+
48+
This rule has an object option:
2949

30-
Nothing
50+
`"ignoreProperties": false` (default) whether or not objects' properties should be ignored
51+
52+
### Example:
53+
54+
```json
55+
"vue/no-multi-spaces": [2, {
56+
"ignoreProperties": true
57+
}]
58+
```
59+
60+
:+1: Examples of **correct** code for this rule:
61+
62+
```html
63+
<i
64+
:class="{
65+
'fa-angle-up' : isExpanded,
66+
'fa-angle-down' : !isExpanded,
67+
}"
68+
/>
69+
```

Diff for: lib/rules/no-multi-spaces.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
// Rule Definition
99
// ------------------------------------------------------------------------------
1010

11+
const isProperty = (context, node) => {
12+
const sourceCode = context.getSourceCode()
13+
return node.type === 'Punctuator' && sourceCode.getText(node) === ':'
14+
}
15+
1116
module.exports = {
1217
meta: {
1318
docs: {
@@ -16,17 +21,24 @@ module.exports = {
1621
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.3/docs/rules/no-multi-spaces.md'
1722
},
1823
fixable: 'whitespace', // or "code" or "whitespace"
19-
schema: []
24+
schema: [{
25+
type: 'object',
26+
properties: {
27+
ignoreProperties: {
28+
type: 'boolean'
29+
}
30+
},
31+
additionalProperties: false
32+
}]
2033
},
2134

2235
/**
2336
* @param {RuleContext} context - The rule context.
2437
* @returns {Object} AST event handlers.
2538
*/
2639
create (context) {
27-
// ----------------------------------------------------------------------
28-
// Public
29-
// ----------------------------------------------------------------------
40+
const options = context.options[0] || {}
41+
const ignoreProperties = options.ignoreProperties === true
3042

3143
return {
3244
Program (node) {
@@ -47,7 +59,10 @@ module.exports = {
4759
let prevToken = tokens.shift()
4860
for (const token of tokens) {
4961
const spaces = token.range[0] - prevToken.range[1]
50-
if (spaces > 1 && token.loc.start.line === prevToken.loc.start.line) {
62+
const shouldIgnore = ignoreProperties && (
63+
isProperty(context, token) || isProperty(context, prevToken)
64+
)
65+
if (spaces > 1 && token.loc.start.line === prevToken.loc.start.line && !shouldIgnore) {
5166
context.report({
5267
node: token,
5368
loc: {

Diff for: tests/lib/rules/no-multi-spaces.js

+86
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,36 @@ ruleTester.run('no-multi-spaces', rule, {
4444
{
4545
filename: 'test.js',
4646
code: 'export default { }'
47+
},
48+
{
49+
code: `
50+
<template>
51+
<i
52+
:class="{
53+
'fa-angle-up' : isExpanded,
54+
'fa-angle-down' : !isExpanded,
55+
}"
56+
/>
57+
</template>
58+
`,
59+
options: [{
60+
ignoreProperties: true
61+
}]
62+
},
63+
{
64+
code: `
65+
<template>
66+
<i
67+
:class="{
68+
'fa-angle-up': isExpanded,
69+
'fa-angle-down': !isExpanded,
70+
}"
71+
/>
72+
</template>
73+
`,
74+
options: [{
75+
ignoreProperties: true
76+
}]
4777
}
4878
],
4979
invalid: [
@@ -176,6 +206,62 @@ ruleTester.run('no-multi-spaces', rule, {
176206
type: 'Punctuator'
177207
}
178208
]
209+
},
210+
{
211+
code: `
212+
<template>
213+
<i
214+
:class="{
215+
'fa-angle-up' : isExpanded,
216+
'fa-angle-down' : !isExpanded,
217+
}"
218+
/>
219+
</template>
220+
`,
221+
output: `
222+
<template>
223+
<i
224+
:class="{
225+
'fa-angle-up' : isExpanded,
226+
'fa-angle-down' : !isExpanded,
227+
}"
228+
/>
229+
</template>
230+
`,
231+
errors: [
232+
{
233+
message: "Multiple spaces found before ':'.",
234+
type: 'Punctuator'
235+
}
236+
]
237+
},
238+
{
239+
code: `
240+
<template>
241+
<i
242+
:class="{
243+
'fa-angle-up': isExpanded,
244+
'fa-angle-down': !isExpanded,
245+
}"
246+
/>
247+
</template>
248+
`,
249+
output: `
250+
<template>
251+
<i
252+
:class="{
253+
'fa-angle-up': isExpanded,
254+
'fa-angle-down': !isExpanded,
255+
}"
256+
/>
257+
</template>
258+
`,
259+
errors: [
260+
{
261+
message: "Multiple spaces found before 'isExpanded'.",
262+
type: 'Identifier'
263+
}
264+
]
179265
}
180266
]
181267
})

0 commit comments

Comments
 (0)