Skip to content

(Implements #535) Add "ignoreProperties" option to "no-multi-spaces" rule #591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
43 changes: 41 additions & 2 deletions docs/rules/no-multi-spaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ Examples of **incorrect** code for this rule:
:style="bar" />
```

```html
<i
:class="{
'fa-angle-up' : isExpanded,
'fa-angle-down' : !isExpanded,
}"
/>
```

Examples of **correct** code for this rule:

```html
Expand All @@ -25,6 +34,36 @@ Examples of **correct** code for this rule:
/>
```

### Options
```html
<i
:class="{
'fa-angle-up' : isExpanded,
'fa-angle-down' : !isExpanded,
}"
/>
```

## :wrench: Options

This rule has an object option:

Nothing
`"ignoreProperties": false` (default) whether or not objects' properties should be ignored

### Example:

```json
"vue/no-multi-spaces": [2, {
"ignoreProperties": true
}]
```

:+1: Examples of **correct** code for this rule:

```html
<i
:class="{
'fa-angle-up' : isExpanded,
'fa-angle-down' : !isExpanded,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michalsnik Hello!
Is the spaces after the colon allowed?

    'fa-angle-up':   isExpanded,
    'fa-angle-down': !isExpanded,

It seems that this case is allowed by option { "Property": true } of core no-multi-spaces rule,
so I think that it will be the same behavior is good.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I updated the cases and handle this one as well.

}"
/>
```
25 changes: 20 additions & 5 deletions lib/rules/no-multi-spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
// Rule Definition
// ------------------------------------------------------------------------------

const isProperty = (context, node) => {
const sourceCode = context.getSourceCode()
return node.type === 'Punctuator' && sourceCode.getText(node) === ':'
}

module.exports = {
meta: {
docs: {
Expand All @@ -16,17 +21,24 @@ module.exports = {
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.3/docs/rules/no-multi-spaces.md'
},
fixable: 'whitespace', // or "code" or "whitespace"
schema: []
schema: [{
type: 'object',
properties: {
ignoreProperties: {
type: 'boolean'
}
},
additionalProperties: false
}]
},

/**
* @param {RuleContext} context - The rule context.
* @returns {Object} AST event handlers.
*/
create (context) {
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
const options = context.options[0] || {}
const ignoreProperties = options.ignoreProperties === true

return {
Program (node) {
Expand All @@ -47,7 +59,10 @@ module.exports = {
let prevToken = tokens.shift()
for (const token of tokens) {
const spaces = token.range[0] - prevToken.range[1]
if (spaces > 1 && token.loc.start.line === prevToken.loc.start.line) {
const shouldIgnore = ignoreProperties && (
isProperty(context, token) || isProperty(context, prevToken)
)
if (spaces > 1 && token.loc.start.line === prevToken.loc.start.line && !shouldIgnore) {
context.report({
node: token,
loc: {
Expand Down
86 changes: 86 additions & 0 deletions tests/lib/rules/no-multi-spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,36 @@ ruleTester.run('no-multi-spaces', rule, {
{
filename: 'test.js',
code: 'export default { }'
},
{
code: `
<template>
<i
:class="{
'fa-angle-up' : isExpanded,
'fa-angle-down' : !isExpanded,
}"
/>
</template>
`,
options: [{
ignoreProperties: true
}]
},
{
code: `
<template>
<i
:class="{
'fa-angle-up': isExpanded,
'fa-angle-down': !isExpanded,
}"
/>
</template>
`,
options: [{
ignoreProperties: true
}]
}
],
invalid: [
Expand Down Expand Up @@ -176,6 +206,62 @@ ruleTester.run('no-multi-spaces', rule, {
type: 'Punctuator'
}
]
},
{
code: `
<template>
<i
:class="{
'fa-angle-up' : isExpanded,
'fa-angle-down' : !isExpanded,
}"
/>
</template>
`,
output: `
<template>
<i
:class="{
'fa-angle-up' : isExpanded,
'fa-angle-down' : !isExpanded,
}"
/>
</template>
`,
errors: [
{
message: "Multiple spaces found before ':'.",
type: 'Punctuator'
}
]
},
{
code: `
<template>
<i
:class="{
'fa-angle-up': isExpanded,
'fa-angle-down': !isExpanded,
}"
/>
</template>
`,
output: `
<template>
<i
:class="{
'fa-angle-up': isExpanded,
'fa-angle-down': !isExpanded,
}"
/>
</template>
`,
errors: [
{
message: "Multiple spaces found before 'isExpanded'.",
type: 'Identifier'
}
]
}
]
})