]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/quotes.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / quotes.md
CommitLineData
8f9d1d4d
DC
1---
2title: quotes
8f9d1d4d
DC
3rule_type: layout
4---
5
6
eb39fafa
DC
7
8JavaScript allows you to define strings in one of three ways: double quotes, single quotes, and backticks (as of ECMAScript 6). For example:
9
10```js
11/*eslint-env es6*/
12
13var double = "double";
14var single = 'single';
15var backtick = `backtick`; // ES6 only
16```
17
18Each of these lines creates a string and, in some cases, can be used interchangeably. The choice of how to define strings in a codebase is a stylistic one outside of template literals (which allow embedded of expressions to be interpreted).
19
20Many codebases require strings to be defined in a consistent manner.
21
22## Rule Details
23
24This rule enforces the consistent use of either backticks, double, or single quotes.
25
26## Options
27
28This rule has two options, a string option and an object option.
29
30String option:
31
32* `"double"` (default) requires the use of double quotes wherever possible
33* `"single"` requires the use of single quotes wherever possible
34* `"backtick"` requires the use of backticks wherever possible
35
36Object option:
37
38* `"avoidEscape": true` allows strings to use single-quotes or double-quotes so long as the string contains a quote that would have to be escaped otherwise
39* `"allowTemplateLiterals": true` allows strings to use backticks
40
41**Deprecated**: The object property `avoid-escape` is deprecated; please use the object property `avoidEscape` instead.
42
43### double
44
45Examples of **incorrect** code for this rule with the default `"double"` option:
46
8f9d1d4d
DC
47::: incorrect
48
eb39fafa
DC
49```js
50/*eslint quotes: ["error", "double"]*/
51
52var single = 'single';
53var unescaped = 'a string containing "double" quotes';
54var backtick = `back\ntick`; // you can use \n in single or double quoted strings
55```
56
8f9d1d4d
DC
57:::
58
eb39fafa
DC
59Examples of **correct** code for this rule with the default `"double"` option:
60
8f9d1d4d
DC
61::: correct
62
eb39fafa
DC
63```js
64/*eslint quotes: ["error", "double"]*/
65/*eslint-env es6*/
66
67var double = "double";
68var backtick = `back
69tick`; // backticks are allowed due to newline
70var backtick = tag`backtick`; // backticks are allowed due to tag
71```
72
8f9d1d4d
DC
73:::
74
eb39fafa
DC
75### single
76
77Examples of **incorrect** code for this rule with the `"single"` option:
78
8f9d1d4d
DC
79::: incorrect
80
eb39fafa
DC
81```js
82/*eslint quotes: ["error", "single"]*/
83
84var double = "double";
85var unescaped = "a string containing 'single' quotes";
86```
87
8f9d1d4d
DC
88:::
89
eb39fafa
DC
90Examples of **correct** code for this rule with the `"single"` option:
91
8f9d1d4d
DC
92::: correct
93
eb39fafa
DC
94```js
95/*eslint quotes: ["error", "single"]*/
96/*eslint-env es6*/
97
98var single = 'single';
99var backtick = `back${x}tick`; // backticks are allowed due to substitution
100```
101
8f9d1d4d
DC
102:::
103
eb39fafa
DC
104### backticks
105
106Examples of **incorrect** code for this rule with the `"backtick"` option:
107
8f9d1d4d
DC
108::: incorrect
109
eb39fafa
DC
110```js
111/*eslint quotes: ["error", "backtick"]*/
112
113var single = 'single';
114var double = "double";
115var unescaped = 'a string containing `backticks`';
116```
117
8f9d1d4d
DC
118:::
119
eb39fafa
DC
120Examples of **correct** code for this rule with the `"backtick"` option:
121
8f9d1d4d
DC
122::: correct
123
eb39fafa
DC
124```js
125/*eslint quotes: ["error", "backtick"]*/
126/*eslint-env es6*/
127
128var backtick = `backtick`;
129```
130
8f9d1d4d
DC
131:::
132
eb39fafa
DC
133### avoidEscape
134
135Examples of additional **correct** code for this rule with the `"double", { "avoidEscape": true }` options:
136
8f9d1d4d
DC
137::: correct
138
eb39fafa
DC
139```js
140/*eslint quotes: ["error", "double", { "avoidEscape": true }]*/
141
142var single = 'a string containing "double" quotes';
143```
144
8f9d1d4d
DC
145:::
146
eb39fafa
DC
147Examples of additional **correct** code for this rule with the `"single", { "avoidEscape": true }` options:
148
8f9d1d4d
DC
149::: correct
150
eb39fafa
DC
151```js
152/*eslint quotes: ["error", "single", { "avoidEscape": true }]*/
153
154var double = "a string containing 'single' quotes";
155```
156
8f9d1d4d
DC
157:::
158
eb39fafa
DC
159Examples of additional **correct** code for this rule with the `"backtick", { "avoidEscape": true }` options:
160
8f9d1d4d
DC
161::: correct
162
eb39fafa
DC
163```js
164/*eslint quotes: ["error", "backtick", { "avoidEscape": true }]*/
165
166var double = "a string containing `backtick` quotes"
167```
168
8f9d1d4d
DC
169:::
170
eb39fafa
DC
171### allowTemplateLiterals
172
173Examples of additional **correct** code for this rule with the `"double", { "allowTemplateLiterals": true }` options:
174
8f9d1d4d
DC
175::: correct
176
eb39fafa
DC
177```js
178/*eslint quotes: ["error", "double", { "allowTemplateLiterals": true }]*/
179
180var double = "double";
181var double = `double`;
182```
183
8f9d1d4d
DC
184:::
185
eb39fafa
DC
186Examples of additional **correct** code for this rule with the `"single", { "allowTemplateLiterals": true }` options:
187
8f9d1d4d
DC
188::: correct
189
eb39fafa
DC
190```js
191/*eslint quotes: ["error", "single", { "allowTemplateLiterals": true }]*/
192
193var single = 'single';
194var single = `single`;
195```
196
8f9d1d4d
DC
197:::
198
199`{ "allowTemplateLiterals": false }` will not disallow the usage of all template literals. If you want to forbid any instance of template literals, use [no-restricted-syntax](no-restricted-syntax) and target the `TemplateLiteral` selector.
eb39fafa
DC
200
201## When Not To Use It
202
203If you do not need consistency in your string styles, you can safely disable this rule.