8000 [Update] Improve the range of `mustache-interpolation-spacing` (#276) · Demivan/eslint-plugin-vue@3137c1f · GitHub
[go: up one dir, main page]

Skip to content

Commit 3137c1f

Browse files
mysticateamichalsnik
authored andcommitted
[Update] Improve the range of mustache-interpolation-spacing (vuejs#276)
* Fix: improve range of mustache-interpolation-spacing * Chore: add linebreaks
1 parent 6a9ef03 commit 3137c1f

File tree

2 files changed

+68
-79
lines changed

2 files changed

+68
-79
lines changed

lib/rules/mustache-interpolation-spacing.js

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,58 +29,68 @@ module.exports = {
2929
},
3030

3131
create (context) {
32-
const options = context.options[0]
33-
const optSpaces = options !== 'never'
34-
const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore()
35-
36-
// ----------------------------------------------------------------------
37-
// Helpers
38-
// ----------------------------------------------------------------------
39-
40-
function checkTokens (leftToken, rightToken) {
41-
if (leftToken.loc.end.line === rightToken.loc.start.line) {
42-
const spaces = rightToken.loc.start.column - leftToken.loc.end.column
43-
const noSpacesFound = spaces === 0
44-
45-
if (optSpaces === noSpacesFound) {
46-
context.report({
47-
node: rightToken,
48-
loc: {
49-
start: leftToken.loc.end,
50-
end: rightToken.loc.start
51-
},
52-
message: 'Found {{spaces}} whitespaces, {{type}} expected.',
53-
data: {
54-
spaces: spaces === 0 ? 'none' : spaces,
55-
type: optSpaces ? '1' : 'none'
56-
},
57-
fix: (fixer) => fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], optSpaces ? ' ' : '')
58-
})
59-
}
60-
}
61-
}
32+
const options = context.options[0] || 'always'
33+
const template =
34+
context.parserServices.getTemplateBodyTokenStore &&
35+
context.parserServices.getTemplateBodyTokenStore()
6236

6337
// ----------------------------------------------------------------------
6438
// Public
6539
// ----------------------------------------------------------------------
6640

6741
return utils.defineTemplateBodyVisitor(context, {
6842
'VExpressionContainer[expression!=null]' (node) {
69-
const tokens = template.getTokens(node, {
70-
includeComments: true,
71-
filter: token => token.type !== 'HTMLWhitespace' // When there is only whitespace between ignore it
72-
})
43+
const openBrace = template.getFirstToken(node)
44+
const closeBrace = template.getLastToken(node)
45+
46+
if (
47+
!openBrace ||
48+
!closeBrace ||
49+
openBrace.type !== 'VExpressionStart' ||
50+
closeBrace.type !== 'VExpressionEnd'
51+
) {
52+
return
53+
}
7354

74-
const startToken = tokens.shift()
75-
if (!startToken || startToken.type !== 'VExpressionStart') return
76-
const endToken = tokens.pop()
77-
if (!endToken || endToken.type !== 'VExpressionEnd') return
55+
const firstToken = template.getTokenAfter(openBrace, { includeComments: true })
56+
const lastToken = template.getTokenBefore(closeBrace, { includeComments: true })
7857

79-
if (tokens.length > 0) {
80-
checkTokens(startToken, tokens[0])
81-
checkTokens(tokens[tokens.length - 1], endToken)
58+
if (options === 'always') {
59+
if (openBrace.range[1] === firstToken.range[0]) {
60+
context.report({
61+
node: openBrace,
62+
message: "Expected 1 space after '{{', but not found.",
63+
fix: (fixer) => fixer.insertTextAfter(openBrace, ' ')
64+
})
65+
}
66+
if (closeBrace.range[0] === lastToken.range[1]) {
67+
context.report({
68+
node: closeBrace,
69+
message: "Expected 1 space before '}}', but not found.",
70+
fix: (fixer) => fixer.insertTextBefore(closeBrace, ' ')
71+
})
72+
}
8273
} else {
83-
checkTokens(startToken, endToken)
74+
if (openBrace.range[1] !== firstToken.range[0]) {
75+
context.report({
76+
loc: {
77+
start: openBrace.loc.start,
78+
end: firstToken.loc.start
79+
},
80+
message: "Expected no space after '{{', but found.",
81+
fix: (fixer) => fixer.removeRange([openBrace.range[1], firstToken.range[0]])
82+
})
83+
}
84+
if (closeBrace.range[0] !== lastToken.range[1]) {
85+
context.report({
86+
loc: {
87+
start: lastToken.loc.end,
88+
end: closeBrace.loc.end
89+
},
90+
message: "Expected no space before '}}', but found.",
91+
fix: (fixer) => fixer.removeRange([lastToken.range[1], closeBrace.range[0]])
92+
})
93+
}
8494
}
8595
}
8696
})

tests/lib/rules/mustache-interpolation-spacing.js

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -90,79 +90,58 @@ ruleTester.run('mustache-interpolation-spacing', rule, {
9090
code: '<template><div>{{ text}}</div></template>',
9191
output: '<template><div>{{ text }}</div></template>',
9292
options: ['always'],
93-
errors: [{
94-
message: 'Found none whitespaces, 1 expected.',
95-
type: 'VExpressionEnd'
96-
}]
93+
errors: ["Expected 1 space before '}}', but not found."]
9794
},
9895
{
9996
filename: 'test.vue',
10097
code: '<template><div>{{text }}</div></template>',
10198
output: '<template><div>{{ text }}</div></template>',
10299
options: ['always'],
103-
errors: [{
104-
message: 'Found none whitespaces, 1 expected.',
105-
type: 'Identifier'
106-
}]
100+
errors: ["Expected 1 space after '{{', but not found."]
107101
},
108102
{
109103
filename: 'test.vue',
110104
code: '<template><div>{{ text}}</div></template>',
111105
output: '<template><div>{{text}}</div></template>',
112106
options: ['never'],
113-
errors: [{
114-
message: 'Found 1 whitespaces, none expected.',
115-
type: 'Identifier'
116-
}]
107+
errors: ["Expected no space after '{{', but found."]
117108
},
118109
{
119110
filename: 'test.vue',
120111
code: '<template><div>{{text }}</div></template>',
121112
output: '<template><div>{{text}}</div></template>',
122113
options: ['never'],
123-
errors: [{
124-
message: 'Found 1 whitespaces, none expected.',
125-
type: 'VExpressionEnd'
126-
}]
114+
errors: ["Expected no space before '}}', but found."]
127115
},
128116
{
129117
filename: 'test.vue',
130118
code: '<template><div>{{text}}</div></template>',
131119
output: '<template><div>{{ text }}</div></template>',
132120
options: ['always'],
133-
errors: [{
134-
message: 'Found none whitespaces, 1 expected.',
135-
type: 'Identifier'
136-
}, {
137-
message: 'Found none whitespaces, 1 expected.',
138-
type: 'VExpressionEnd'
139-
}]
121+
errors: [
122+
"Expected 1 space after '{{', but not found.",
123+
"Expected 1 space before '}}', but not found."
124+
]
140125
},
141126
{
142127
filename: 'test.vue',
143128
code: '<template><div>{{ text }}</div></template>',
144129
output: '<template><div>{{text}}</div></template>',
145130
options: ['never'],
146-
errors: [{
147-
message: 'Found 1 whitespaces, none expected.',
148-
type: 'Identifier'
149-
}, {
150-
message: 'Found 1 whitespaces, none expected.',
151-
type: 'VExpressionEnd'
152-
}]
131+
errors: [
132+
"Expected no space after '{{', but found.",
133+
"Expected no space before '}}', but found."
134+
]
153135
},
154136
{
155137
filename: 'test.vue',
156138
code: '<template><div>{{ text }}</div></template>',
157139
output: '<template><div>{{text}}</div></template>',
158140
options: ['never'],
159-
errors: [{
160-
message: 'Found 3 whitespaces, none expected.',
161-
type: 'Identifier'
162-
}, {
163-
message: 'Found 3 whitespaces, none expected.',
164-
type: 'VExpressionEnd'
165-
}]
141+
errors: [
142+
"Expected no space after '{{', but found.",
143+
"Expected no space before '}}', but found."
144+
]
166145
}
167146
]
168147
})

0 commit comments

Comments
 (0)
0