Skip to content

Commit a3bba07

Browse files
committed
Improve rule based on code review
1 parent 46436c0 commit a3bba07

7 files changed

+241
-227
lines changed

docs/rules/no-extra-space-between-attributes.md

-36
This file was deleted.

docs/rules/no-multi-spaces.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This rule warns about the usage of extra whitespaces between attributes (no-multi-spaces)
2+
3+
The `--fix` option on the command line can automatically fix some of the problems reported by this rule.
4+
5+
This rule aims to remove multiple spaces in a row between attributes witch are not used for indentation.
6+
7+
## Rule Details
8+
9+
Examples of **incorrect** code for this rule:
10+
11+
```html
12+
<template>
13+
<div class="foo" :style="foo"\t
14+
:foo="bar" >
15+
</div>
16+
</template>
17+
```
18+
19+
Examples of **correct** code for this rule:
20+
21+
```html
22+
<template>
23+
<div class="foo"
24+
:style="foo">
25+
</div>
26+
</template>
27+
```
28+
29+
### Options
30+
31+
Nothing

lib/rules/html-no-self-closing.js

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function create (context) {
2828
return
2929
}
3030

31+
// TODO: Check `context.parserServices.getTemplateBodyTokenStore` exists or not.
3132
const sourceCode = context.parserServices.getTemplateBodyTokenStore(context)
3233
const lastToken = sourceCode.getLastToken(node.startTag)
3334
if (lastToken.type !== 'HTMLSelfClosingTagClose') {

lib/rules/no-extra-space-between-attributes.js

-60
This file was deleted.

lib/rules/no-multi-spaces.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @fileoverview This rule warns about the usage of extra whitespaces between attributes
3+
* @author Armano
4+
*/
5+
'use strict'
6+
7+
// ------------------------------------------------------------------------------
8+
// Rule Definition
9+
// ------------------------------------------------------------------------------
10+
11+
module.exports = {
12+
meta: {
13+
docs: {
14+
description: 'This rule warns about the usage of extra whitespaces between attributes',
15+
category: 'Stylistic Issues',
16+
recommended: false
17+
},
18+
fixable: 'whitespace', // or "code" or "whitespace"
19+
schema: []
20+
},
21+
22+
/**
23+
* @param {RuleContext} context - The rule context.
24+
* @returns {Object} AST event handlers.
25+
*/
26+
create (context) {
27+
const sourceCode = context.getSourceCode()
28+
29+
// ----------------------------------------------------------------------
30+
// Public
31+
// ----------------------------------------------------------------------
32+
33+
return {
34+
Program (node) {
35+
// TODO: Check `context.parserServices.getTemplateBodyTokenStore` exists or not.
36+
const tokenStore = context.parserServices.getTemplateBodyTokenStore()
37+
const tokens = tokenStore.getTokens(node.templateBody, { includeComments: true })
38+
39+
let prevToken = tokens.shift()
40+
for (const token of tokens) {
41+
if (sourceCode.isSpaceBetweenTokens(prevToken, token)) {
42+
const text = sourceCode.getText(token, (token.range[0] - prevToken.range[1] + 1), 0)
43+
44+
const match = text.match(/([^\r\n\t\s])([ \t]+)([>]?)([\n\r]?)/)
45+
if (!match) return // there is no errors
46+
47+
const spaces = match[2].length
48+
const requiredSpaces = match[3] === '>' || match[4] !== '' ? 0 : 1
49+
50+
if (spaces > requiredSpaces) {
51+
context.report({
52+
node: token,
53+
loc: {
54+
start: {
55+
line: prevToken.loc.end.line,
56+
column: prevToken.loc.end.column + requiredSpaces
57+
},
58+
end: {
59+
line: prevToken.loc.end.line,
60+
column: prevToken.loc.end.column + spaces
61+
}
62+
},
63+
message: 'Extra whitespace detected.',
64+
fix: (fixer) => fixer.removeRange([prevToken.range[1] + requiredSpaces, prevToken.range[1] + spaces])
65+
})
66+
}
67+
}
68+
prevToken = token
69+
}
70+
}
71+
}
72+
}
73+
}

tests/lib/rules/no-extra-space-between-attributes.js

-131
This file was deleted.

0 commit comments

Comments
 (0)