Skip to content

Commit f40938e

Browse files
committed
Add more test cases
1 parent ce8f6d1 commit f40938e

File tree

4 files changed

+94
-45
lines changed

4 files changed

+94
-45
lines changed

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

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

31-
// TODO: Check `context.parserServices.getTemplateBodyTokenStore` exists or not.
3231
const sourceCode = context.parserServices.getTemplateBodyTokenStore(context)
3332
const lastToken = sourceCode.getLastToken(node.startTag)
3433
if (lastToken.type !== 'HTMLSelfClosingTagClose') {

lib/rules/no-multi-spaces.js

+33-26
Original file line numberDiff line numberDiff line change
@@ -32,41 +32,48 @@ module.exports = {
3232

3333
return {
3434
Program (node) {
35-
// TODO: Check `context.parserServices.getTemplateBodyTokenStore` exists or not.
35+
if (context.parserServices.getTemplateBodyTokenStore == null) {
36+
context.report({
37+
loc: { line: 1, column: 0 },
38+
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.'
39+
})
40+
return
41+
}
3642
const tokenStore = context.parserServices.getTemplateBodyTokenStore()
3743
const tokens = tokenStore.getTokens(node.templateBody, { includeComments: true })
3844

3945
let prevToken = tokens.shift()
4046
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)
47+
const text = sourceCode.getText(token, (token.range[0] - prevToken.range[1] + 1), 0)
4348

44-
const match = text.match(/([^\r\n\t\s])([ \t]+)([>]?)([\n\r]?)/)
45-
if (!match) {
46-
prevToken = token
47-
continue // there is no errors
48-
}
49+
const match = text.match(/([^\r\n\t\s])([ \t]+)([>]?)([\n\r]?)/)
50+
if (!match) {
51+
prevToken = token
52+
continue // there is no errors
53+
}
4954

50-
const spaces = match[2].length
51-
const requiredSpaces = match[3] === '>' || match[4] !== '' ? 0 : 1
55+
const spaces = match[2].length
56+
const requiredSpaces = match[3] === '>' || match[4] !== '' ? 0 : 1
5257

53-
if (spaces > requiredSpaces) {
54-
context.report({
55-
node: token,
56-
loc: {
57-
start: {
58-
line: prevToken.loc.end.line,
59-
column: prevToken.loc.end.column + requiredSpaces
60-
},
61-
end: {
62-
line: prevToken.loc.end.line,
63-
column: prevToken.loc.end.column + spaces
64-
}
58+
if (spaces > requiredSpaces) {
59+
context.report({
60+
node: token,
61+
loc: {
62+
start: {
63+
line: prevToken.loc.end.line,
64+
column: prevToken.loc.end.column + requiredSpaces
6565
},
66-
message: 'Extra whitespace detected.',
67-
fix: (fixer) => fixer.removeRange([prevToken.range[1] + requiredSpaces, prevToken.range[1] + spaces])
68-
})
69-
}
66+
end: {
67+
line: prevToken.loc.end.line,
68+
column: prevToken.loc.end.column + spaces
69+
}
70+
},
71+
message: "Multiple spaces found before '{{displayValue}}'.",
72+
fix: (fixer) => fixer.removeRange([prevToken.range[1] + requiredSpaces, prevToken.range[1] + spaces]),
73+
data: {
74+
displayValue: ''
75+
}
76+
})
7077
}
7178
prevToken = token
7279
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
},
4646
"dependencies": {
4747
"requireindex": "^1.1.0",
48-
"vue-eslint-parser": "2.0.0-beta.5"
48+
"vue-eslint-parser": "^2.0.0-beta.7"
4949
},
5050
"devDependencies": {
5151
"@types/node": "^4.2.16",

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

+60-17
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@ ruleTester.run('no-multi-spaces', rule, {
3131
'<template><div class="foo"\n\t\t\t:style="foo"></div></template>',
3232
'<template><div class="foo"\n :style="foo"\n ></div></template>',
3333
'<template><div class="foo"\n :style="foo" /></template>',
34-
'<template><div class="foo"\n :style="foo"\n /></template>'
34+
'<template><div class="foo"\n :style="foo"\n /></template>',
35+
'<template><div>{{ test }}</div></template>',
36+
'<template><div>{{test}}</div></template>',
37+
'<template><div>{{test}}<!-- fooo --></div></template>'
3538
],
3639
invalid: [
3740
{
3841
code: '<template><div /></template>',
3942
output: '<template><div /></template>',
4043
errors: [{
41-
message: 'Extra whitespace detected.',
44+
message: "Multiple spaces found before ''.",
4245
type: 'HTMLSelfClosingTagClose'
4346
}]
4447
},
@@ -47,11 +50,11 @@ ruleTester.run('no-multi-spaces', rule, {
4750
output: '<template><div class="foo" /></template>',
4851
errors: [
4952
{
50-
message: 'Extra whitespace detected.',
53+
message: "Multiple spaces found before ''.",
5154
type: 'HTMLIdentifier'
5255
},
5356
{
54-
message: 'Extra whitespace detected.',
57+
message: "Multiple spaces found before ''.",
5558
type: 'HTMLSelfClosingTagClose'
5659
}
5760
]
@@ -61,11 +64,11 @@ ruleTester.run('no-multi-spaces', rule, {
6164
output: '<template><div\tclass="foo"\t/></template>',
6265
errors: [
6366
{
64-
message: 'Extra whitespace detected.',
67+
message: "Multiple spaces found before ''.",
6568
type: 'HTMLIdentifier'
6669
},
6770
{
68-
message: 'Extra whitespace detected.',
71+
message: "Multiple spaces found before ''.",
6972
type: 'HTMLSelfClosingTagClose'
7073
}
7174
]
@@ -75,11 +78,11 @@ ruleTester.run('no-multi-spaces', rule, {
7578
output: '<template><div :class="foo" /></template>',
7679
errors: [
7780
{
78-
message: 'Extra whitespace detected.',
81+
message: "Multiple spaces found before ''.",
7982
type: 'HTMLIdentifier'
8083
},
8184
{
82-
message: 'Extra whitespace detected.',
85+
message: "Multiple spaces found before ''.",
8386
type: 'HTMLSelfClosingTagClose'
8487
}
8588
]
@@ -88,23 +91,23 @@ ruleTester.run('no-multi-spaces', rule, {
8891
code: '<template><div :foo="" class="foo" /></template>',
8992
output: '<template><div :foo="" class="foo" /></template>',
9093
errors: [{
91-
message: 'Extra whitespace detected.',
94+
message: "Multiple spaces found before ''.",
9295
type: 'HTMLSelfClosingTagClose'
9396
}]
9497
},
9598
{
9699
code: '<template><div foo="" class="foo" /></template>',
97100
output: '<template><div foo="" class="foo" /></template>',
98101
errors: [{
99-
message: 'Extra whitespace detected.',
102+
message: "Multiple spaces found before ''.",
100103
type: 'HTMLSelfClosingTagClose'
101104
}]
102105
},
103106
{
104107
code: '<template><foo v-foo="" class="foo" /></template>',
105108
output: '<template><foo v-foo="" class="foo" /></template>',
106109
errors: [{
107-
message: 'Extra whitespace detected.',
110+
message: "Multiple spaces found before ''.",
108111
type: 'HTMLSelfClosingTagClose'
109112
}]
110113
},
@@ -113,22 +116,62 @@ ruleTester.run('no-multi-spaces', rule, {
113116
output: '<template><foo v-foo=""\n class="foo" /></template>',
114117
errors: [
115118
{
116-
message: 'Extra whitespace detected.',
119+
message: "Multiple spaces found before ''.",
117120
type: 'HTMLIdentifier'
118121
},
119122
{
120-
message: 'Extra whitespace detected.',
123+
message: "Multiple spaces found before ''.",
121124
type: 'HTMLSelfClosingTagClose'
122125
}
123126
]
124127
},
125128
{
126-
code: '<template><div class="foo " class=" foo " /></template>',
127-
output: '<template><div class="foo " class=" foo " /></template>',
129+
code: '<template><div>{{ test }}</div></template>',
130+
output: '<template><div>{{ test }}</div></template>',
128131
errors: [
129132
{
130-
message: 'Extra whitespace detected.',
131-
type: 'HTMLIdentifier'
133+
message: "Multiple spaces found before ''.",
134+
type: 'Identifier'
135+
},
136+
{
137+
message: "Multiple spaces found before ''.",
138+
type: 'VExpressionEnd'
139+
}
140+
]
141+
},
142+
{
143+
code: '<template><div>{{test}} <!-- fooo --></div></template>',
144+
output: '<template><div>{{test}} <!-- fooo --></div></template>',
145+
errors: [
146+
{
147+
message: "Multiple spaces found before ''.",
148+
type: 'HTMLComment'
149+
},
150+
{
151+
message: "Multiple spaces found before ''.",
152+
153+
type: 'HTMLComment'
154+
}
155+
] },
156+
{
157+
code: '<template><div v-for=" i in b ">{{ test }}</div></template>',
158+
output: '<template><div v-for=" i in b ">{{ test }}</div></template>',
159+
errors: [
160+
{
161+
message: "Multiple spaces found before ''.",
162+
type: 'Identifier'
163+
},
164+
{
165+
message: "Multiple spaces found before ''.",
166+
type: 'Keyword'
167+
},
168+
{
169+
message: "Multiple spaces found before ''.",
170+
type: 'Identifier'
171+
},
172+
{
173+
message: "Multiple spaces found before ''.",
174+
type: 'Punctuator'
132175
}
133176
]
134177
}

0 commit comments

Comments
 (0)