Skip to content

Add "no-multi-spaces" rule (fixes #133) #138

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
Aug 14, 2017
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
31 changes: 31 additions & 0 deletions docs/rules/no-multi-spaces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This rule warns about the usage of extra whitespaces between attributes (no-multi-spaces)

The `--fix` option on the command line can automatically fix some of the problems reported by this rule.

This rule aims to remove multiple spaces in a row between attributes witch are not used for indentation.

## Rule Details

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

```html
<template>
<div class="foo" :style="foo"
:foo="bar" >
</div>
</template>
```

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

```html
<template>
<div class="foo"
:style="foo">
</div>
</template>
```

### Options

Nothing
66 changes: 66 additions & 0 deletions lib/rules/no-multi-spaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @fileoverview This rule warns about the usage of extra whitespaces between attributes
* @author Armano
*/
'use strict'

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description: 'This rule warns about the usage of extra whitespaces between attributes',
category: 'Stylistic Issues',
recommended: false
},
fixable: 'whitespace', // or "code" or "whitespace"
schema: []
},

/**
* @param {RuleContext} context - The rule context.
* @returns {Object} AST event handlers.
*/
create (context) {
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------

return {
Program (node) {
if (context.parserServices.getTemplateBodyTokenStore == null) {
context.report({
loc: { line: 1, column: 0 },
message: 'Use the latest vue-eslint-parser. See also https://github.com/vuejs/eslint-plugin-vue#what-is-the-use-the-latest-vue-eslint-parser-error.'
})
return
}
const sourceCode = context.getSourceCode()
const tokenStore = context.parserServices.getTemplateBodyTokenStore()
const tokens = tokenStore.getTokens(node.templateBody, { includeComments: true })

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) {
context.report({
node: token,
loc: {
start: prevToken.loc.end,
end: token.loc.start
},
message: "Multiple spaces found before '{{displayValue}}'.",
fix: (fixer) => fixer.replaceTextRange([prevToken.range[1], token.range[0]], ' '),
data: {
displayValue: sourceCode.getText(token)
}
})
}
prevToken = token
}
}
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"dependencies": {
"requireindex": "^1.1.0",
"vue-eslint-parser": "2.0.0-beta.6"
"vue-eslint-parser": "2.0.0-beta.7"
},
"devDependencies": {
"@types/node": "^4.2.16",
Expand Down
177 changes: 177 additions & 0 deletions tests/lib/rules/no-multi-spaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/**
* @fileoverview This rule warns about the usage of extra whitespaces between attributes
* @author Armano
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const rule = require('../../../lib/rules/no-multi-spaces')
const RuleTester = require('eslint').RuleTester

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

const ruleTester = new RuleTester({
parser: 'vue-eslint-parser',
parserOptions: { ecmaVersion: 2015 }
})

ruleTester.run('no-multi-spaces', rule, {
valid: [
'',
'<template></template>',
'<template><div /></template>',
'<template><div class="foo"></div></template>',
'<template><div class=" foo " style=" foo "></div></template>',
'<template><div class="foo" @click="bar"></div></template>',
'<template><div class="foo"\n :style="foo"></div></template>',
'<template><div class="foo"\n\t\t\t:style="foo"></div></template>',
'<template><div class="foo"\n :style="foo"\n ></div></template>',
'<template><div class="foo"\n :style="foo" /></template>',
'<template><div class="foo"\n :style="foo"\n /></template>',
'<template><div>{{ test }}</div></template>',
'<template><div>{{test}}</div></template>',
'<template><div>{{test}}<!-- fooo --></div></template>',
'<template><div>{{test}} <!-- fooo --></div></template>',
'<template><div v-for="i in b">{{ i }}</div></template>',
'<template><div v-for=" i in b ">{{ i }}</div></template>',
'<template><div :test="` `"> {{ a }} </div></template>',
'<template><div :test="` `"> \n {{ a }} </div></template>'
],
invalid: [
{
code: '<template><div /></template>',
output: '<template><div /></template>',
errors: [{
message: "Multiple spaces found before '/>'.",
type: 'HTMLSelfClosingTagClose'
}]
},
{
code: '<template><div class="foo" /></template>',
output: '<template><div class="foo" /></template>',
errors: [
{
message: "Multiple spaces found before 'class'.",
type: 'HTMLIdentifier'
},
{
message: "Multiple spaces found before '/>'.",
type: 'HTMLSelfClosingTagClose'
}
]
},
{
code: '<template><div\t\tclass="foo"\t\t/></template>',
output: '<template><div class="foo" /></template>',
errors: [
{
message: "Multiple spaces found before 'class'.",
type: 'HTMLIdentifier'
},
{
message: "Multiple spaces found before '/>'.",
type: 'HTMLSelfClosingTagClose'
}
]
},
{
code: '<template><div :class="foo" /></template>',
output: '<template><div :class="foo" /></template>',
errors: [
{
message: "Multiple spaces found before ':class'.",
type: 'HTMLIdentifier'
},
{
message: "Multiple spaces found before '/>'.",
type: 'HTMLSelfClosingTagClose'
}
]
},
{
code: '<template><div :foo="" class="foo" /></template>',
output: '<template><div :foo="" class="foo" /></template>',
errors: [{
message: "Multiple spaces found before '/>'.",
type: 'HTMLSelfClosingTagClose'
}]
},
{
code: '<template><div foo="" class="foo" /></template>',
output: '<template><div foo="" class="foo" /></template>',
errors: [{
message: "Multiple spaces found before '/>'.",
type: 'HTMLSelfClosingTagClose'
}]
},
{
code: '<template><foo v-foo="" class="foo" /></template>',
output: '<template><foo v-foo="" class="foo" /></template>',
errors: [{
message: "Multiple spaces found before '/>'.",
type: 'HTMLSelfClosingTagClose'
}]
},
{
code: '<template><foo v-foo="" \n class="foo" /></template>',
output: '<template><foo v-foo="" \n class="foo" /></template>',
errors: [
{
message: "Multiple spaces found before '/>'.",
type: 'HTMLSelfClosingTagClose'
}
]
},
{
code: '<template><div>{{ test }}</div></template>',
output: '<template><div>{{ test }}</div></template>',
errors: [
{
message: "Multiple spaces found before 'test'.",
type: 'Identifier'
},
{
message: "Multiple spaces found before '}}'.",
type: 'VExpressionEnd'
}
]
},
{
code: '<template><div ></div></template>',
output: '<template><div ></div></template>',
errors: [
{
message: "Multiple spaces found before '>'.",
type: 'HTMLTagClose'
}
]
},
{
code: '<template><div v-for=" i in b ">{{ test }}</div></template>',
output: '<template><div v-for=" i in b ">{{ test }}</div></template>',
errors: [
{
message: "Multiple spaces found before 'i'.",
type: 'Identifier'
},
{
message: "Multiple spaces found before 'in'.",
type: 'Keyword'
},
{
message: "Multiple spaces found before 'b'.",
type: 'Identifier'
},
{
message: "Multiple spaces found before '\"'.",
type: 'Punctuator'
}
]
}
]
})