]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: quotes | |
3 | layout: doc | |
4 | rule_type: layout | |
5 | --- | |
6 | ||
7 | ||
eb39fafa DC |
8 | |
9 | JavaScript 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 | ||
14 | var double = "double"; | |
15 | var single = 'single'; | |
16 | var backtick = `backtick`; // ES6 only | |
17 | ``` | |
18 | ||
19 | Each 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 | ||
21 | Many codebases require strings to be defined in a consistent manner. | |
22 | ||
23 | ## Rule Details | |
24 | ||
25 | This rule enforces the consistent use of either backticks, double, or single quotes. | |
26 | ||
27 | ## Options | |
28 | ||
29 | This rule has two options, a string option and an object option. | |
30 | ||
31 | String 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 | ||
37 | Object 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 | ||
46 | Examples 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 | ||
53 | var single = 'single'; | |
54 | var unescaped = 'a string containing "double" quotes'; | |
55 | var backtick = `back\ntick`; // you can use \n in single or double quoted strings | |
56 | ``` | |
57 | ||
8f9d1d4d DC |
58 | ::: |
59 | ||
eb39fafa DC |
60 | Examples 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 | ||
68 | var double = "double"; | |
69 | var backtick = `back | |
70 | tick`; // backticks are allowed due to newline | |
71 | var backtick = tag`backtick`; // backticks are allowed due to tag | |
72 | ``` | |
73 | ||
8f9d1d4d DC |
74 | ::: |
75 | ||
eb39fafa DC |
76 | ### single |
77 | ||
78 | Examples 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 | ||
85 | var double = "double"; | |
86 | var unescaped = "a string containing 'single' quotes"; | |
87 | ``` | |
88 | ||
8f9d1d4d DC |
89 | ::: |
90 | ||
eb39fafa DC |
91 | Examples 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 | ||
99 | var single = 'single'; | |
100 | var backtick = `back${x}tick`; // backticks are allowed due to substitution | |
101 | ``` | |
102 | ||
8f9d1d4d DC |
103 | ::: |
104 | ||
eb39fafa DC |
105 | ### backticks |
106 | ||
107 | Examples 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 | ||
114 | var single = 'single'; | |
115 | var double = "double"; | |
116 | var unescaped = 'a string containing `backticks`'; | |
117 | ``` | |
118 | ||
8f9d1d4d DC |
119 | ::: |
120 | ||
eb39fafa DC |
121 | Examples 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 | ||
129 | var backtick = `backtick`; | |
130 | ``` | |
131 | ||
8f9d1d4d DC |
132 | ::: |
133 | ||
eb39fafa DC |
134 | ### avoidEscape |
135 | ||
136 | Examples 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 | ||
143 | var single = 'a string containing "double" quotes'; | |
144 | ``` | |
145 | ||
8f9d1d4d DC |
146 | ::: |
147 | ||
eb39fafa DC |
148 | Examples 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 | ||
155 | var double = "a string containing 'single' quotes"; | |
156 | ``` | |
157 | ||
8f9d1d4d DC |
158 | ::: |
159 | ||
eb39fafa DC |
160 | Examples 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 | ||
167 | var double = "a string containing `backtick` quotes" | |
168 | ``` | |
169 | ||
8f9d1d4d DC |
170 | ::: |
171 | ||
eb39fafa DC |
172 | ### allowTemplateLiterals |
173 | ||
174 | Examples 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 | ||
181 | var double = "double"; | |
182 | var double = `double`; | |
183 | ``` | |
184 | ||
8f9d1d4d DC |
185 | ::: |
186 | ||
eb39fafa DC |
187 | Examples 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 | ||
194 | var single = 'single'; | |
195 | var 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 | ||
204 | If you do not need consistency in your string styles, you can safely disable this rule. |