]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/quote-props.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / quote-props.md
CommitLineData
eb39fafa
DC
1# require quotes around object literal property names (quote-props)
2
3Object literal property names can be defined in two ways: using literals or using strings. For example, these two objects are equivalent:
4
5```js
6var object1 = {
7 property: true
8};
9
10var object2 = {
11 "property": true
12};
13```
14
15In many cases, it doesn't matter if you choose to use an identifier instead of a string or vice-versa. Even so, you might decide to enforce a consistent style in your code.
16
17There are, however, some occasions when you must use quotes:
18
191. If you are using an ECMAScript 3 JavaScript engine (such as IE8) and you want to use a keyword (such as `if`) as a property name. This restriction was removed in ECMAScript 5.
202. You want to use a non-identifier character in your property name, such as having a property with a space like `"one two"`.
21
22Another example where quotes do matter is when using numeric literals as property keys:
23
24```js
25var object = {
26 1e2: 1,
27 100: 2
28};
29```
30
31This may look alright at first sight, but this code in fact throws a syntax error in ECMAScript 5 strict mode. This happens because `1e2` and `100` are coerced into strings before getting used as the property name. Both `String(1e2)` and `String(100)` happen to be equal to `"100"`, which causes the "Duplicate data property in object literal not allowed in strict mode" error. Issues like that can be tricky to debug, so some prefer to require quotes around all property names.
32
33## Rule Details
34
35This rule requires quotes around object literal property names.
36
37## Options
38
39This rule has two options, a string option and an object option.
40
41String option:
42
43* `"always"` (default) requires quotes around all object literal property names
44* `"as-needed"` disallows quotes around object literal property names that are not strictly required
45* `"consistent"` enforces a consistent quote style; in a given object, either all of the properties should be quoted, or none of the properties should be quoted
46* `"consistent-as-needed"` requires quotes around all object literal property names if any name strictly requires quotes, otherwise disallows quotes around object property names
47
48Object option:
49
50* `"keywords": true` requires quotes around language keywords used as object property names (only applies when using `as-needed` or `consistent-as-needed`)
51* `"unnecessary": true` (default) disallows quotes around object literal property names that are not strictly required (only applies when using `as-needed`)
52* `"unnecessary": false` allows quotes around object literal property names that are not strictly required (only applies when using `as-needed`)
53* `"numbers": true` requires quotes around numbers used as object property names (only applies when using `as-needed`)
54
55### always
56
57Examples of **incorrect** code for this rule with the default `"always"` option:
58
59```js
60/*eslint quote-props: ["error", "always"]*/
61
62var object = {
63 foo: "bar",
64 baz: 42
65};
66```
67
68Examples of **correct** code for this rule with the default `"always"` option:
69
70```js
71/*eslint quote-props: ["error", "always"]*/
72/*eslint-env es6*/
73
74var object1 = {
75 "foo": "bar",
76 "baz": 42,
77 "qux-lorem": true
78};
79
80var object2 = {
81 'foo': 'bar',
82 'baz': 42,
83 'qux-lorem': true
84};
85
86var object3 = {
87 foo() {
88 return;
89 }
90};
91```
92
93### as-needed
94
95Examples of **incorrect** code for this rule with the `"as-needed"` option:
96
97```js
98/*eslint quote-props: ["error", "as-needed"]*/
99
100var object = {
101 "a": 0,
102 "0": 0,
103 "true": 0,
104 "null": 0
105};
106```
107
108Examples of **correct** code for this rule with the `"as-needed"` option:
109
110```js
111/*eslint quote-props: ["error", "as-needed"]*/
112/*eslint-env es6*/
113
114var object1 = {
115 "a-b": 0,
116 "0x0": 0,
117 "1e2": 0
118};
119
120var object2 = {
121 foo: 'bar',
122 baz: 42,
123 true: 0,
124 0: 0,
125 'qux-lorem': true
126};
127
128var object3 = {
129 foo() {
130 return;
131 }
132};
133```
134
135### consistent
136
137Examples of **incorrect** code for this rule with the `"consistent"` option:
138
139```js
140/*eslint quote-props: ["error", "consistent"]*/
141
142var object1 = {
143 foo: "bar",
144 "baz": 42,
145 "qux-lorem": true
146};
147
148var object2 = {
149 'foo': 'bar',
150 baz: 42
151};
152```
153
154Examples of **correct** code for this rule with the `"consistent"` option:
155
156```js
157/*eslint quote-props: ["error", "consistent"]*/
158
159var object1 = {
160 "foo": "bar",
161 "baz": 42,
162 "qux-lorem": true
163};
164
165var object2 = {
166 'foo': 'bar',
167 'baz': 42
168};
169
170var object3 = {
171 foo: 'bar',
172 baz: 42
173};
174```
175
176### consistent-as-needed
177
178Examples of **incorrect** code for this rule with the `"consistent-as-needed"` option:
179
180```js
181/*eslint quote-props: ["error", "consistent-as-needed"]*/
182
183var object1 = {
184 foo: "bar",
185 "baz": 42,
186 "qux-lorem": true
187};
188
189var object2 = {
190 'foo': 'bar',
191 'baz': 42
192};
193```
194
195Examples of **correct** code for this rule with the `"consistent-as-needed"` option:
196
197```js
198/*eslint quote-props: ["error", "consistent-as-needed"]*/
199
200var object1 = {
201 "foo": "bar",
202 "baz": 42,
203 "qux-lorem": true
204};
205
206var object2 = {
207 foo: 'bar',
208 baz: 42
209};
210```
211
212### keywords
213
214Examples of additional **incorrect** code for this rule with the `"as-needed", { "keywords": true }` options:
215
216```js
217/*eslint quote-props: ["error", "as-needed", { "keywords": true }]*/
218
219var x = {
220 while: 1,
221 volatile: "foo"
222};
223```
224
225Examples of additional **incorrect** code for this rule with the `"consistent-as-needed", { "keywords": true }` options:
226
227```js
228/*eslint quote-props: ["error", "consistent-as-needed", { "keywords": true }]*/
229
230var x = {
231 "prop": 1,
232 "bar": "foo"
233};
234```
235
236### unnecessary
237
238Examples of additional **correct** code for this rule with the `"as-needed", { "unnecessary": false }` options:
239
240```js
241/*eslint quote-props: ["error", "as-needed", { "keywords": true, "unnecessary": false }]*/
242
243var x = {
244 "while": 1,
245 "foo": "bar" // Would normally have caused a warning
246};
247```
248
249### numbers
250
251Examples of additional **incorrect** code for this rule with the `"as-needed", { "numbers": true }` options:
252
253```js
254/*eslint quote-props: ["error", "as-needed", { "numbers": true }]*/
255
256var x = {
257 100: 1
258}
259```
260
261## When Not To Use It
262
263If you don't care if property names are consistently wrapped in quotes or not, and you don't target legacy ES3 environments, turn this rule off.
264
265## Further Reading
266
267* [Reserved words as property names](https://kangax.github.io/compat-table/es5/#Reserved_words_as_property_names)
268* [Unquoted property names / object keys in JavaScript](https://mathiasbynens.be/notes/javascript-properties)