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