]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Enforce Usage of Spacing in Template Strings (template-curly-spacing) |
2 | ||
3 | We can embed expressions in template strings with using a pair of `${` and `}`. | |
4 | ||
5 | This rule can force usage of spacing _within_ the curly brace pair according to style guides. | |
6 | ||
7 | ```js | |
8 | let hello = `hello, ${people.name}!`; | |
9 | ``` | |
10 | ||
11 | ## Rule Details | |
12 | ||
13 | This rule aims to maintain consistency around the spacing inside of template literals. | |
14 | ||
15 | ## Options | |
16 | ||
17 | ```json | |
18 | { | |
19 | "template-curly-spacing": ["error", "never"] | |
20 | } | |
21 | ``` | |
22 | ||
23 | This rule has one option which has either `"never"` or `"always"` as value. | |
24 | ||
25 | * `"never"` (by default) - Disallows spaces inside of the curly brace pair. | |
26 | * `"always"` - Requires one or more spaces inside of the curly brace pair. | |
27 | ||
28 | ## Examples | |
29 | ||
30 | ### never | |
31 | ||
32 | Examples of **incorrect** code for this rule with the default `"never"` option: | |
33 | ||
34 | ```js | |
35 | /*eslint template-curly-spacing: "error"*/ | |
36 | ||
37 | `hello, ${ people.name}!`; | |
38 | `hello, ${people.name }!`; | |
39 | ||
40 | `hello, ${ people.name }!`; | |
41 | ``` | |
42 | ||
43 | Examples of **correct** code for this rule with the default `"never"` option: | |
44 | ||
45 | ```js | |
46 | /*eslint template-curly-spacing: "error"*/ | |
47 | ||
48 | `hello, ${people.name}!`; | |
49 | ||
50 | `hello, ${ | |
51 | people.name | |
52 | }!`; | |
53 | ``` | |
54 | ||
55 | ### always | |
56 | ||
57 | Examples of **incorrect** code for this rule with the `"always"` option: | |
58 | ||
59 | ```js | |
60 | /*eslint template-curly-spacing: ["error", "always"]*/ | |
61 | ||
62 | `hello, ${ people.name}!`; | |
63 | `hello, ${people.name }!`; | |
64 | ||
65 | `hello, ${people.name}!`; | |
66 | ``` | |
67 | ||
68 | Examples of **correct** code for this rule with the `"always"` option: | |
69 | ||
70 | ```js | |
71 | /*eslint template-curly-spacing: ["error", "always"]*/ | |
72 | ||
73 | `hello, ${ people.name }!`; | |
74 | ||
75 | `hello, ${ | |
76 | people.name | |
77 | }!`; | |
78 | ``` | |
79 | ||
80 | ## When Not To Use It | |
81 | ||
82 | If you don't want to be notified about usage of spacing inside of template strings, then it's safe to disable this rule. |