Skip to content

Commit 876fabf

Browse files
committed
Add "no-multi-spaces" rule.
1 parent d5a288d commit 876fabf

File tree

3 files changed

+9
-31
lines changed

3 files changed

+9
-31
lines changed

docs/rules/no-multi-spaces.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Examples of **incorrect** code for this rule:
1010

1111
```html
1212
<template>
13-
<div class="foo" :style="foo"\t
13+
<div class="foo" :style="foo"
1414
:foo="bar" >
1515
</div>
1616
</template>

lib/rules/no-multi-spaces.js

+5-24
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ module.exports = {
2424
* @returns {Object} AST event handlers.
2525
*/
2626
create (context) {
27-
const sourceCode = context.getSourceCode()
28-
2927
function formatValue (token) {
3028
switch (token.type) {
3129
case 'HTMLSelfClosingTagClose': return '/>'
@@ -53,33 +51,16 @@ module.exports = {
5351

5452
let prevToken = tokens.shift()
5553
for (const token of tokens) {
56-
const length = token.range[0] - prevToken.range[1] + 1
57-
const text = sourceCode.getText(token, length, 0).substring(0, length)
58-
59-
const match = text.match(/([^\r\n\t\s])([ \t]+)([>]?)([\n\r]?)/)
60-
if (!match) {
61-
prevToken = token
62-
continue // there is no errors
63-
}
64-
65-
const spaces = match[2].length
66-
const requiredSpaces = match[3] === '>' || match[4] !== '' ? 0 : 1
67-
68-
if (spaces > requiredSpaces) {
54+
const spaces = token.range[0] - prevToken.range[1]
55+
if (spaces > 1 && token.loc.start.line === prevToken.loc.start.line) {
6956
context.report({
7057
node: token,
7158
loc: {
72-
start: {
73-
line: prevToken.loc.end.line,
74-
column: prevToken.loc.end.column
75-
},
76-
end: {
77-
line: prevToken.loc.end.line,
78-
column: prevToken.loc.end.column + spaces
79-
}
59+
start: prevToken.loc.end,
60+
end: token.loc.start
8061
},
8162
message: "Multiple spaces found before '{{displayValue}}'.",
82-
fix: (fixer) => fixer.replaceTextRange([prevToken.range[1], prevToken.range[1] + spaces], requiredSpaces ? ' ' : ''),
63+
fix: (fixer) => fixer.replaceTextRange([prevToken.range[1], token.range[0]], ' '),
8364
data: {
8465
displayValue: formatValue(token)
8566
}

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

+3-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ ruleTester.run('no-multi-spaces', rule, {
3939
'<template><div>{{test}} <!-- fooo --></div></template>',
4040
'<template><div v-for="i in b">{{ i }}</div></template>',
4141
'<template><div v-for=" i in b ">{{ i }}</div></template>',
42-
'<template><div :test="` `"> {{ a }} </div></template>'
42+
'<template><div :test="` `"> {{ a }} </div></template>',
43+
'<template><div :test="` `"> \n {{ a }} </div></template>'
4344
],
4445
invalid: [
4546
{
@@ -118,12 +119,8 @@ ruleTester.run('no-multi-spaces', rule, {
118119
},
119120
{
120121
code: '<template><foo v-foo="" \n class="foo" /></template>',
121-
output: '<template><foo v-foo=""\n class="foo" /></template>',
122+
output: '<template><foo v-foo="" \n class="foo" /></template>',
122123
errors: [
123-
{
124-
message: "Multiple spaces found before 'class'.",
125-
type: 'HTMLIdentifier'
126-
},
127124
{
128125
message: "Multiple spaces found before '/>'.",
129126
type: 'HTMLSelfClosingTagClose'

0 commit comments

Comments
 (0)