Skip to content

Commit 4eb39e7

Browse files
authored
Add vue/template-curly-spacing rule (#1142)
1 parent af491ab commit 4eb39e7

File tree

6 files changed

+129
-1
lines changed

6 files changed

+129
-1
lines changed

docs/rules/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ For example:
303303
| [vue/space-infix-ops](./space-infix-ops.md) | require spacing around infix operators | :wrench: |
304304
| [vue/space-unary-ops](./space-unary-ops.md) | enforce consistent spacing before or after unary operators | :wrench: |
305305
| [vue/static-class-names-order](./static-class-names-order.md) | enforce static class names order | :wrench: |
306+
| [vue/template-curly-spacing](./template-curly-spacing.md) | require or disallow spacing around embedded expressions of template strings | :wrench: |
306307
| [vue/v-on-function-call](./v-on-function-call.md) | enforce or forbid parentheses after method calls without arguments in `v-on` directives | :wrench: |
307308

308309
## Deprecated

docs/rules/template-curly-spacing.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
pageClass: rule-details
3+
sidebarDepth: 0
4+
title: vue/template-curly-spacing
5+
description: require or disallow spacing around embedded expressions of template strings
6+
---
7+
# vue/template-curly-spacing
8+
> require or disallow spacing around embedded expressions of template strings
9+
10+
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.
11+
12+
This rule is the same rule as core [template-curly-spacing] rule but it applies to the expressions in `<template>`.
13+
14+
## :books: Further reading
15+
16+
- [template-curly-spacing]
17+
18+
[template-curly-spacing]: https://eslint.org/docs/rules/template-curly-spacing
19+
20+
## :mag: Implementation
21+
22+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/template-curly-spacing.js)
23+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/template-curly-spacing.js)

lib/configs/no-layout-rules.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ module.exports = {
3333
'vue/script-indent': 'off',
3434
'vue/singleline-html-element-content-newline': 'off',
3535
'vue/space-infix-ops': 'off',
36-
'vue/space-unary-ops': 'off'
36+
'vue/space-unary-ops': 'off',
37+
'vue/template-curly-spacing': 'off'
3738
}
3839
}

lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ module.exports = {
112112
'space-infix-ops': require('./rules/space-infix-ops'),
113113
'space-unary-ops': require('./rules/space-unary-ops'),
114114
'static-class-names-order': require('./rules/static-class-names-order'),
115+
'template-curly-spacing': require('./rules/template-curly-spacing'),
115116
'this-in-template': require('./rules/this-in-template'),
116117
'use-v-on-exact': require('./rules/use-v-on-exact'),
117118
'v-bind-style': require('./rules/v-bind-style'),

lib/rules/template-curly-spacing.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @author Yosuke Ota
3+
*/
4+
'use strict'
5+
6+
const { wrapCoreRule } = require('../utils')
7+
8+
// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories
9+
module.exports = wrapCoreRule(
10+
require('eslint/lib/rules/template-curly-spacing'),
11+
{ skipDynamicArguments: true }
12+
)
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* @author Yosuke Ota
3+
*/
4+
'use strict'
5+
6+
const RuleTester = require('eslint').RuleTester
7+
const rule = require('../../../lib/rules/template-curly-spacing')
8+
9+
const tester = new RuleTester({
10+
parser: require.resolve('vue-eslint-parser'),
11+
parserOptions: { ecmaVersion: 2020 }
12+
})
13+
14+
tester.run('template-curly-spacing', rule, {
15+
valid: [
16+
`
17+
<template>
18+
<div :class="[\`foo-\${bar}\`]" />
19+
</template>
20+
`,
21+
`
22+
<template>
23+
<div :[\`foo\${bar}\`]="value" />
24+
</template>
25+
`,
26+
{
27+
code: `
28+
<template>
29+
<div :class="[\`foo-\${ bar }\`]" />
30+
</template>
31+
`,
32+
options: ['always']
33+
},
34+
{
35+
code: `
36+
<template>
37+
<div :[\`foo\${bar}\`]="value" />
38+
</template>
39+
`,
40+
options: ['always']
41+
}
42+
],
43+
invalid: [
44+
{
45+
code: `
46+
<template>
47+
<div :class="[\`foo-\${ bar }\`]" />
48+
</template>
49+
`,
50+
output: `
51+
<template>
52+
<div :class="[\`foo-\${bar}\`]" />
53+
</template>
54+
`,
55+
errors: [
56+
{
57+
message: "Unexpected space(s) after '${'.",
58+
line: 3
59+
},
60+
{
61+
message: "Unexpected space(s) before '}'.",
62+
line: 3
63+
}
64+
]
65+
},
66+
{
67+
code: `
68+
<template>
69+
<div :class="[\`foo-\${bar}\`]" />
70+
</template>
71+
`,
72+
options: ['always'],
73+
output: `
74+
<template>
75+
<div :class="[\`foo-\${ bar }\`]" />
76+
</template>
77+
`,
78+
errors: [
79+
{
80+
message: "Expected space(s) after '${'.",
81+
line: 3
82+
},
83+
{
84+
message: "Expected space(s) before '}'.",
85+
line: 3
86+
}
87+
]
88+
}
89+
]
90+
})

0 commit comments

Comments
 (0)