WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/swift-spies-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-vue": minor
---

Added new `ignoreEOLComments` option to `vue/no-multi-spaces` rule
22 changes: 21 additions & 1 deletion docs/rules/no-multi-spaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ This rule aims at removing multiple spaces in tags, which are not used for inden
```json
{
"vue/no-multi-spaces": ["error", {
"ignoreProperties": false
"ignoreProperties": false,
"ignoreEOLComments": false
}]
}
```

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

### `"ignoreProperties": true`

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

</eslint-code-block>

### `"ignoreEOLComments": true`

<eslint-code-block fix :rules="{'vue/no-multi-spaces': ['error', { 'ignoreEOLComments': true }]}">

```vue
<template>
<!-- ✓ GOOD -->
<div
:class="{
'fa-angle-up' : isExpanded, // comment
'fa-angle-down' : !isExpanded, /* multiline comment */
}"
/>
</template>
```

</eslint-code-block>

## :rocket: Version

This rule was introduced in eslint-plugin-vue v3.12.0
Expand Down
20 changes: 19 additions & 1 deletion lib/rules/no-multi-spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ module.exports = {
properties: {
ignoreProperties: {
type: 'boolean'
},
ignoreEOLComments: {
type: 'boolean'
}
},
additionalProperties: false
Expand All @@ -49,6 +52,7 @@ module.exports = {
create(context) {
const options = context.options[0] || {}
const ignoreProperties = options.ignoreProperties === true
const ignoreEOLComments = options.ignoreEOLComments === true

return {
Program(node) {
Expand All @@ -74,9 +78,23 @@ module.exports = {
let prevToken = /** @type {Token} */ (tokens.shift())
for (const token of tokens) {
const spaces = token.range[0] - prevToken.range[1]
const shouldIgnore =
let shouldIgnore =
ignoreProperties &&
(isProperty(context, token) || isProperty(context, prevToken))

if (
!shouldIgnore &&
ignoreEOLComments &&
(token.type === 'Line' || token.type === 'Block')
) {
const nextToken = tokenStore.getTokenAfter(token, {
includeComments: true
})
if (!nextToken || nextToken.loc.start.line > token.loc.end.line) {
shouldIgnore = true
}
}

if (
spaces > 1 &&
token.loc.start.line === prevToken.loc.start.line &&
Expand Down
100 changes: 100 additions & 0 deletions tests/lib/rules/no-multi-spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,38 @@ ruleTester.run('no-multi-spaces', rule, {
ignoreProperties: true
}
]
},
{
code: `
<template>
<div
:class="{
'foo': foo, // comment
}"
></div>
</template>
`,
options: [
{
ignoreEOLComments: true
}
]
},
{
code: `
<template>
<div
:class="{
'foo': foo /* multiline comment */
}"
></div>
</template>
`,
options: [
{
ignoreEOLComments: true
}
]
}
],
invalid: [
Expand Down Expand Up @@ -329,6 +361,74 @@ ruleTester.run('no-multi-spaces', rule, {
endColumn: 30
}
]
},
{
code: `
<template>
<div
:class="{
'foo': foo // comment
}"
></div>
</template>
`,
output: `
<template>
<div
:class="{
'foo': foo // comment
}"
></div>
</template>
`,
options: [
{
ignoreEOLComments: false
}
],
errors: [
{
message: "Multiple spaces found before '// comment'.",
line: 5,
column: 23,
endLine: 5,
endColumn: 25
}
]
},
{
code: `
<template>
<div
:class="{
'foo': foo /* multiline comment */
}"
></div>
</template>
`,
output: `
<template>
<div
:class="{
'foo': foo /* multiline comment */
}"
></div>
</template>
`,
options: [
{
ignoreEOLComments: false
}
],
errors: [
{
message: "Multiple spaces found before '/* multiline comment */'.",
line: 5,
column: 23,
endLine: 5,
endColumn: 25
}
]
}
]
})
Loading