]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Require or disallow spacing between template tags and their literals (template-tag-spacing) |
2 | ||
3 | With ES6, it's possible to create functions called [tagged template literals](#further-reading) where the function parameters consist of a template literal's strings and expressions. | |
4 | ||
5 | When using tagged template literals, it's possible to insert whitespace between the tag function and the template literal. Since this whitespace is optional, the following lines are equivalent: | |
6 | ||
7 | ```js | |
8 | let hello = func`Hello world`; | |
9 | let hello = func `Hello world`; | |
10 | ``` | |
11 | ||
12 | ## Rule Details | |
13 | ||
14 | This rule aims to maintain consistency around the spacing between template tag functions and their template literals. | |
15 | ||
16 | ## Options | |
17 | ||
18 | ```json | |
19 | { | |
20 | "template-tag-spacing": ["error", "never"] | |
21 | } | |
22 | ``` | |
23 | ||
24 | This rule has one option whose value can be set to `"never"` or `"always"` | |
25 | ||
26 | * `"never"` (default) - Disallows spaces between a tag function and its template literal. | |
27 | * `"always"` - Requires one or more spaces between a tag function and its template literal. | |
28 | ||
29 | ## Examples | |
30 | ||
31 | ### never | |
32 | ||
33 | Examples of **incorrect** code for this rule with the default `"never"` option: | |
34 | ||
35 | ```js | |
36 | /*eslint template-tag-spacing: "error"*/ | |
37 | ||
38 | func `Hello world`; | |
39 | ``` | |
40 | ||
41 | Examples of **correct** code for this rule with the default `"never"` option: | |
42 | ||
43 | ```js | |
44 | /*eslint template-tag-spacing: "error"*/ | |
45 | ||
46 | func`Hello world`; | |
47 | ``` | |
48 | ||
49 | ### always | |
50 | ||
51 | Examples of **incorrect** code for this rule with the `"always"` option: | |
52 | ||
53 | ```js | |
54 | /*eslint template-tag-spacing: ["error", "always"]*/ | |
55 | ||
56 | func`Hello world`; | |
57 | ``` | |
58 | ||
59 | Examples of **correct** code for this rule with the `"always"` option: | |
60 | ||
61 | ```js | |
62 | /*eslint template-tag-spacing: ["error", "always"]*/ | |
63 | ||
64 | func `Hello world`; | |
65 | ``` | |
66 | ||
67 | ## When Not To Use It | |
68 | ||
69 | If you don't want to be notified about usage of spacing between tag functions and their template literals, then it's safe to disable this rule. | |
70 | ||
71 | ## Further Reading | |
72 | ||
73 | If you want to learn more about tagged template literals, check out the links below: | |
74 | ||
75 | * [Template literals (MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals) | |
76 | * [Examples of using tagged template literals (Exploring ES6)](http://exploringjs.com/es6/ch_template-literals.html#_examples-of-using-tagged-template-literals) |