Skip to content

Commit cf083cb

Browse files
committed
Apply changes suggested in code review
1 parent 435e9d0 commit cf083cb

File tree

3 files changed

+32
-33
lines changed

3 files changed

+32
-33
lines changed

docs/rules/curly-bracket-spacing.md renamed to docs/rules/mustache-curly-spacing.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Enforce spacing on the style of curly brackets. (curly-bracket-spacing)
1+
# Enforce spacing on the style of mustache curly brackets. (mustache-curly-spacing)
22

33
- :wrench: The `--fix` option on the [command line](http://eslint.org/docs/user-guide/command-line-interface#fix) can automatically fix some of the problems reported by this rule.
44

@@ -27,7 +27,7 @@ This rule aims to enforce unified spacing of curly brackets.
2727
Default spacing is set to `always`
2828

2929
```
30-
'vue/curly-bracket-spacing': [2, 'always'|'never']
30+
'vue/mustache-curly-spacing': [2, 'always'|'never']
3131
```
3232

3333
### `"always"` - Expect one space between expression and curly brackets.

lib/rules/curly-bracket-spacing.js renamed to lib/rules/mustache-curly-spacing.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @fileoverview Enforce spacing on the style of curly brackets.
2+
* @fileoverview Enforce spacing on the style of mustache curly brackets.
33
* @author Armano
44
*/
55
'use strict'
@@ -17,7 +17,7 @@ const utils = require('../utils')
1717
module.exports = {
1818
meta: {
1919
docs: {
20-
description: 'Enforce spacing on the style of curly brackets.',
20+
description: 'Enforce spacing on the style of mustache curly brackets.',
2121
category: 'Stylistic Issues',
2222
recommended: false
2323
},
@@ -31,7 +31,7 @@ module.exports = {
3131

3232
create (context) {
3333
const options = context.options[0]
34-
const optSpaces = options !== 'never' ? 1 : 0
34+
const optSpaces = options !== 'never'
3535
const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore()
3636

3737
// ----------------------------------------------------------------------
@@ -41,7 +41,7 @@ module.exports = {
4141
function checkTokens (leftToken, rightToken) {
4242
if (leftToken.loc.end.line === rightToken.loc.start.line) {
4343
const spaces = rightToken.loc.start.column - leftToken.loc.end.column
44-
if (optSpaces !== spaces) {
44+
if (optSpaces === (spaces === 0)) {
4545
context.report({
4646
node: rightToken,
4747
loc: {
@@ -64,14 +64,17 @@ module.exports = {
6464
// ----------------------------------------------------------------------
6565

6666
utils.registerTemplateBodyVisitor(context, {
67-
VExpressionContainer (node) {
67+
'VExpressionContainer[expression!=null]' (node) {
6868
const tokens = template.getTokens(node, {
6969
includeComments: true,
7070
filter: token => token.type !== 'HTMLWhitespace' // When there is only whitespace between ignore it
7171
})
7272

7373
const startToken = tokens.shift()
74+
if (!startToken || startToken.type !== 'VExpressionStart') return
7475
const endToken = tokens.pop()
76+
if (!endToken || endToken.type !== 'VExpressionEnd') return
77+
7578
if (tokens.length > 0) {
7679
checkTokens(startToken, tokens[0])
7780
checkTokens(tokens[tokens.length - 1], endToken)

tests/lib/rules/curly-bracket-spacing.js renamed to tests/lib/rules/mustache-curly-spacing.js

+22-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @fileoverview Enforce spacing on the style of curly brackets.
2+
* @fileoverview Enforce spacing on the style of mustache curly brackets.
33
* @author Armano
44
*/
55
'use strict'
@@ -8,7 +8,7 @@
88
// Requirements
99
// ------------------------------------------------------------------------------
1010

11-
const rule = require('../../../lib/rules/curly-bracket-spacing')
11+
const rule = require('../../../lib/rules/mustache-curly-spacing')
1212
const RuleTester = require('eslint').RuleTester
1313

1414
// ------------------------------------------------------------------------------
@@ -20,7 +20,7 @@ const ruleTester = new RuleTester({
2020
parserOptions: { ecmaVersion: 2015 }
2121
})
2222

23-
ruleTester.run('curly-bracket-spacing', rule, {
23+
ruleTester.run('mustache-curly-spacing', rule, {
2424

2525
valid: [
2626
{
@@ -35,6 +35,10 @@ ruleTester.run('curly-bracket-spacing', rule, {
3535
filename: 'test.vue',
3636
code: '<template> <div id=" "></div> </template>'
3737
},
38+
{
39+
filename: 'test.vue',
40+
code: '<template> <div :style=" " :class=" foo " v-if=foo ></div> </template>'
41+
},
3842
{
3943
filename: 'test.vue',
4044
code: '<template><div>{{ text }}</div></template>'
@@ -62,20 +66,25 @@ ruleTester.run('curly-bracket-spacing', rule, {
6266
filename: 'test.vue',
6367
code: '<template><div>{{ text }}</div></template>',
6468
options: ['always']
65-
}
66-
],
67-
68-
invalid: [
69+
},
6970
{
7071
filename: 'test.vue',
7172
code: '<template><div>{{ }}</div></template>',
72-
output: '<template><div>{{ }}</div></template>',
73-
options: ['always'],
74-
errors: [{
75-
message: 'Found 9 whitespaces, 1 expected.',
76-
type: 'VExpressionEnd'
77-
}]
73+
options: ['always']
7874
},
75+
{
76+
filename: 'test.vue',
77+
code: '<template><div>{{ }}</div></template>',
78+
options: ['never']
79+
},
80+
{
81+
filename: 'test.vue',
82+
code: '<template><div>{{ text }}</div></template>',
83+
options: ['always']
84+
}
85+
],
86+
87+
invalid: [
7988
{
8089
filename: 'test.vue',
8190
code: '<template><div>{{ text}}</div></template>',
@@ -142,19 +151,6 @@ ruleTester.run('curly-bracket-spacing', rule, {
142151
type: 'VExpressionEnd'
143152
}]
144153
},
145-
{
146-
filename: 'test.vue',
147-
code: '<template><div>{{ text }}</div></template>',
148-
output: '<template><div>{{ text }}</div></template>',
149-
options: ['always'],
150-
errors: [{
151-
message: 'Found 3 whitespaces, 1 expected.',
152-
type: 'Identifier'
153-
}, {
154-
message: 'Found 3 whitespaces, 1 expected.',
155-
type: 'VExpressionEnd'
156-
}]
157-
},
158154
{
159155
filename: 'test.vue',
160156
code: '<template><div>{{ text }}</div></template>',

0 commit comments

Comments
 (0)