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