Skip to content

Commit d5b879d

Browse files
committed
feat(vue/no-multi-spaces): add ignoreEOLComments property
1 parent 4b4630b commit d5b879d

File tree

3 files changed

+140
-2
lines changed

3 files changed

+140
-2
lines changed

docs/rules/no-multi-spaces.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ This rule aims at removing multiple spaces in tags, which are not used for inden
5151
```json
5252
{
5353
"vue/no-multi-spaces": ["error", {
54-
"ignoreProperties": false
54+
"ignoreProperties": false,
55+
"ignoreEOLComments": false
5556
}]
5657
}
5758
```
5859

5960
- `ignoreProperties` ... whether or not objects' properties should be ignored. default `false`
61+
- `ignoreEOLComments` ... whether or not the spaces before EOL comments should be ignored. default `false`
6062

6163
### `"ignoreProperties": true`
6264

@@ -76,6 +78,24 @@ This rule aims at removing multiple spaces in tags, which are not used for inden
7678

7779
</eslint-code-block>
7880

81+
### `"ignoreEOLComments": true`
82+
83+
<eslint-code-block fix :rules="{'vue/no-multi-spaces': ['error', { 'ignoreEOLComments': true }]}">
84+
85+
```vue
86+
<template>
87+
<!-- ✓ GOOD -->
88+
<div
89+
:class="{
90+
'fa-angle-up' : isExpanded, // comment
91+
'fa-angle-down' : !isExpanded, /* multiline comment */
92+
}"
93+
/>
94+
</template>
95+
```
96+
97+
</eslint-code-block>
98+
7999
## :rocket: Version
80100

81101
This rule was introduced in eslint-plugin-vue v3.12.0

lib/rules/no-multi-spaces.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ module.exports = {
3030
properties: {
3131
ignoreProperties: {
3232
type: 'boolean'
33+
},
34+
ignoreEOLComments: {
35+
type: 'boolean'
3336
}
3437
},
3538
additionalProperties: false
@@ -49,6 +52,7 @@ module.exports = {
4952
create(context) {
5053
const options = context.options[0] || {}
5154
const ignoreProperties = options.ignoreProperties === true
55+
const ignoreEOLComments = options.ignoreEOLComments === true
5256

5357
return {
5458
Program(node) {
@@ -74,9 +78,23 @@ module.exports = {
7478
let prevToken = /** @type {Token} */ (tokens.shift())
7579
for (const token of tokens) {
7680
const spaces = token.range[0] - prevToken.range[1]
77-
const shouldIgnore =
81+
let shouldIgnore =
7882
ignoreProperties &&
7983
(isProperty(context, token) || isProperty(context, prevToken))
84+
85+
if (
86+
!shouldIgnore &&
87+
ignoreEOLComments &&
88+
(token.type === 'Line' || token.type === 'Block')
89+
) {
90+
const nextToken = tokenStore.getTokenAfter(token, {
91+
includeComments: true
92+
})
93+
if (!nextToken || nextToken.loc.start.line > token.loc.end.line) {
94+
shouldIgnore = true
95+
}
96+
}
97+
8098
if (
8199
spaces > 1 &&
82100
token.loc.start.line === prevToken.loc.start.line &&

tests/lib/rules/no-multi-spaces.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,38 @@ ruleTester.run('no-multi-spaces', rule, {
7373
ignoreProperties: true
7474
}
7575
]
76+
},
77+
{
78+
code: `
79+
<template>
80+
<div
81+
:class="{
82+
'foo': foo, // comment
83+
}"
84+
></div>
85+
</template>
86+
`,
87+
options: [
88+
{
89+
ignoreEOLComments: true
90+
}
91+
]
92+
},
93+
{
94+
code: `
95+
<template>
96+
<div
97+
:class="{
98+
'foo': foo /* multiline comment */
99+
}"
100+
></div>
101+
</template>
102+
`,
103+
options: [
104+
{
105+
ignoreEOLComments: true
106+
}
107+
]
76108
}
77109
],
78110
invalid: [
@@ -329,6 +361,74 @@ ruleTester.run('no-multi-spaces', rule, {
329361
endColumn: 30
330362
}
331363
]
364+
},
365+
{
366+
code: `
367+
<template>
368+
<div
369+
:class="{
370+
'foo': foo // comment
371+
}"
372+
></div>
373+
</template>
374+
`,
375+
output: `
376+
<template>
377+
<div
378+
:class="{
379+
'foo': foo // comment
380+
}"
381+
></div>
382+
</template>
383+
`,
384+
options: [
385+
{
386+
ignoreEOLComments: false
387+
}
388+
],
389+
errors: [
390+
{
391+
message: "Multiple spaces found before '// comment'.",
392+
line: 5,
393+
column: 23,
394+
endLine: 5,
395+
endColumn: 25
396+
}
397+
]
398+
},
399+
{
400+
code: `
401+
<template>
402+
<div
403+
:class="{
404+
'foo': foo /* multiline comment */
405+
}"
406+
></div>
407+
</template>
408+
`,
409+
output: `
410+
<template>
411+
<div
412+
:class="{
413+
'foo': foo /* multiline comment */
414+
}"
415+
></div>
416+
</template>
417+
`,
418+
options: [
419+
{
420+
ignoreEOLComments: false
421+
}
422+
],
423+
errors: [
424+
{
425+
message: "Multiple spaces found before '/* multiline comment */'.",
426+
line: 5,
427+
column: 23,
428+
endLine: 5,
429+
endColumn: 25
430+
}
431+
]
332432
}
333433
]
334434
})

0 commit comments

Comments
 (0)