Skip to content

Commit 5023e75

Browse files
rzzfFloEdelmann
andauthored
feat(vue/no-multi-spaces): add ignoreEOLComments property (#2989)
Co-authored-by: Flo Edelmann <git@flo-edelmann.de>
1 parent 4b4630b commit 5023e75

File tree

4 files changed

+203
-2
lines changed

4 files changed

+203
-2
lines changed

.changeset/swift-spies-nail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-vue": minor
3+
---
4+
5+
Added new `ignoreEOLComments` option to `vue/no-multi-spaces` rule

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: 158 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,132 @@ 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+
errors: [
385+
{
386+
message: "Multiple spaces found before '// comment'.",
387+
line: 5,
388+
column: 23,
389+
endLine: 5,
390+
endColumn: 25
391+
}
392+
]
393+
},
394+
{
395+
code: `
396+
<template>
397+
<div
398+
:class="{
399+
'foo': foo /* multiline comment */
400+
}"
401+
></div>
402+
</template>
403+
`,
404+
output: `
405+
<template>
406+
<div
407+
:class="{
408+
'foo': foo /* multiline comment */
409+
}"
410+
></div>
411+
</template>
412+
`,
413+
errors: [
414+
{
415+
message: "Multiple spaces found before '/* multiline comment */'.",
416+
line: 5,
417+
column: 23,
418+
endLine: 5,
419+
endColumn: 25
420+
}
421+
]
422+
},
423+
{
424+
code: `
425+
<template>
426+
<div
427+
:class="{
428+
'foo': foo // comment
429+
}"
430+
></div>
431+
</template>
432+
`,
433+
output: `
434+
<template>
435+
<div
436+
:class="{
437+
'foo': foo // comment
438+
}"
439+
></div>
440+
</template>
441+
`,
442+
options: [
443+
{
444+
ignoreEOLComments: false
445+
}
446+
],
447+
errors: [
448+
{
449+
message: "Multiple spaces found before '// comment'.",
450+
line: 5,
451+
column: 23,
452+
endLine: 5,
453+
endColumn: 25
454+
}
455+
]
456+
},
457+
{
458+
code: `
459+
<template>
460+
<div
461+
:class="{
462+
'foo': foo /* multiline comment */
463+
}"
464+
></div>
465+
</template>
466+
`,
467+
output: `
468+
<template>
469+
<div
470+
:class="{
471+
'foo': foo /* multiline comment */
472+
}"
473+
></div>
474+
</template>
475+
`,
476+
options: [
477+
{
478+
ignoreEOLComments: false
479+
}
480+
],
481+
errors: [
482+
{
483+
message: "Multiple spaces found before '/* multiline comment */'.",
484+
line: 5,
485+
column: 23,
486+
endLine: 5,
487+
endColumn: 25
488+
}
489+
]
332490
}
333491
]
334492
})

0 commit comments

Comments
 (0)