]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/strict.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / strict.md
1 ---
2 title: strict
3 rule_type: suggestion
4 ---
5
6
7
8 A strict mode directive is a `"use strict"` literal at the beginning of a script or function body. It enables strict mode semantics.
9
10 When a directive occurs in global scope, strict mode applies to the entire script:
11
12 ```js
13 "use strict";
14
15 // strict mode
16
17 function foo() {
18 // strict mode
19 }
20 ```
21
22 When a directive occurs at the beginning of a function body, strict mode applies only to that function, including all contained functions:
23
24 ```js
25 function foo() {
26 "use strict";
27 // strict mode
28 }
29
30 function foo2() {
31 // not strict mode
32 };
33
34 (function() {
35 "use strict";
36 function bar() {
37 // strict mode
38 }
39 }());
40 ```
41
42 In the **CommonJS** module system, a hidden function wraps each module and limits the scope of a "global" strict mode directive.
43
44 In **ECMAScript** modules, which always have strict mode semantics, the directives are unnecessary.
45
46 ## Rule Details
47
48 This rule requires or disallows strict mode directives.
49
50 This rule disallows strict mode directives, no matter which option is specified, if ESLint configuration specifies either of the following as [parser options](../use/configure/language-options#specifying-parser-options):
51
52 * `"sourceType": "module"` that is, files are **ECMAScript** modules
53 * `"impliedStrict": true` property in the `ecmaFeatures` object
54
55 This rule disallows strict mode directives, no matter which option is specified, in functions with non-simple parameter lists (for example, parameter lists with default parameter values) because that is a syntax error in **ECMAScript 2016** and later. See the examples of the [function](#function) option.
56
57 This rule does not apply to class static blocks, no matter which option is specified, because class static blocks do not have directives. Therefore, a `"use strict"` statement in a class static block is not a directive, and will be reported by the [no-unused-expressions](no-unused-expressions) rule.
58
59 The `--fix` option on the command line does not insert new `"use strict"` statements, but only removes unneeded statements.
60
61 ## Options
62
63 This rule has a string option:
64
65 * `"safe"` (default) corresponds either of the following options:
66 * `"global"` if ESLint considers a file to be a **CommonJS** module
67 * `"function"` otherwise
68 * `"global"` requires one strict mode directive in the global scope (and disallows any other strict mode directives)
69 * `"function"` requires one strict mode directive in each top-level function declaration or expression (and disallows any other strict mode directives)
70 * `"never"` disallows strict mode directives
71
72 ### safe
73
74 The `"safe"` option corresponds to the `"global"` option if ESLint considers a file to be a **Node.js** or **CommonJS** module because the configuration specifies either of the following:
75
76 * `node` or `commonjs` [environments](../use/configure/language-options#specifying-environments)
77 * `"globalReturn": true` property in the `ecmaFeatures` object of [parser options](../use/configure/language-options#specifying-parser-options)
78
79 Otherwise the `"safe"` option corresponds to the `"function"` option. Note that if `"globalReturn": false` is explicitly specified in the configuration, the `"safe"` option will correspond to the `"function"` option regardless of the specified environment.
80
81 ### global
82
83 Examples of **incorrect** code for this rule with the `"global"` option:
84
85 ::: incorrect
86
87 ```js
88 /*eslint strict: ["error", "global"]*/
89
90 function foo() {
91 }
92 ```
93
94 :::
95
96 ::: incorrect
97
98 ```js
99 /*eslint strict: ["error", "global"]*/
100
101 function foo() {
102 "use strict";
103 }
104 ```
105
106 :::
107
108 ::: incorrect
109
110 ```js
111 /*eslint strict: ["error", "global"]*/
112
113 "use strict";
114
115 function foo() {
116 "use strict";
117 }
118 ```
119
120 :::
121
122 Examples of **correct** code for this rule with the `"global"` option:
123
124 ::: correct
125
126 ```js
127 /*eslint strict: ["error", "global"]*/
128
129 "use strict";
130
131 function foo() {
132 }
133 ```
134
135 :::
136
137 ### function
138
139 This option ensures that all function bodies are strict mode code, while global code is not. Particularly if a build step concatenates multiple scripts, a strict mode directive in global code of one script could unintentionally enable strict mode in another script that was not intended to be strict code.
140
141 Examples of **incorrect** code for this rule with the `"function"` option:
142
143 ::: incorrect
144
145 ```js
146 /*eslint strict: ["error", "function"]*/
147
148 "use strict";
149
150 function foo() {
151 }
152 ```
153
154 :::
155
156 ::: incorrect
157
158 ```js
159 /*eslint strict: ["error", "function"]*/
160
161 function foo() {
162 }
163
164 (function() {
165 function bar() {
166 "use strict";
167 }
168 }());
169 ```
170
171 :::
172
173 ::: incorrect
174
175 ```js
176 /*eslint strict: ["error", "function"]*/
177 /*eslint-env es6*/
178
179 // Illegal "use strict" directive in function with non-simple parameter list.
180 // This is a syntax error since ES2016.
181 function foo(a = 1) {
182 "use strict";
183 }
184
185 // We cannot write "use strict" directive in this function.
186 // So we have to wrap this function with a function with "use strict" directive.
187 function foo(a = 1) {
188 }
189 ```
190
191 :::
192
193 Examples of **correct** code for this rule with the `"function"` option:
194
195 ::: correct
196
197 ```js
198 /*eslint strict: ["error", "function"]*/
199
200 function foo() {
201 "use strict";
202 }
203
204 (function() {
205 "use strict";
206
207 function bar() {
208 }
209
210 function baz(a = 1) {
211 }
212 }());
213
214 var foo = (function() {
215 "use strict";
216
217 return function foo(a = 1) {
218 };
219 }());
220 ```
221
222 :::
223
224 ### never
225
226 Examples of **incorrect** code for this rule with the `"never"` option:
227
228 ::: incorrect
229
230 ```js
231 /*eslint strict: ["error", "never"]*/
232
233 "use strict";
234
235 function foo() {
236 }
237 ```
238
239 :::
240
241 ::: incorrect
242
243 ```js
244 /*eslint strict: ["error", "never"]*/
245
246 function foo() {
247 "use strict";
248 }
249 ```
250
251 :::
252
253 Examples of **correct** code for this rule with the `"never"` option:
254
255 ::: correct
256
257 ```js
258 /*eslint strict: ["error", "never"]*/
259
260 function foo() {
261 }
262 ```
263
264 :::
265
266 ### earlier default (removed)
267
268 (removed) The default option (that is, no string option specified) for this rule was **removed** in ESLint v1.0. The `"function"` option is most similar to the removed option.
269
270 This option ensures that all functions are executed in strict mode. A strict mode directive must be present in global code or in every top-level function declaration or expression. It does not concern itself with unnecessary strict mode directives in nested functions that are already strict, nor with multiple strict mode directives at the same level.
271
272 Examples of **incorrect** code for this rule with the earlier default option which has been removed:
273
274 ::: incorrect
275
276 ```js
277 // "strict": "error"
278
279 function foo() {
280 }
281 ```
282
283 :::
284
285 ::: incorrect
286
287 ```js
288 // "strict": "error"
289
290 (function() {
291 function bar() {
292 "use strict";
293 }
294 }());
295 ```
296
297 :::
298
299 Examples of **correct** code for this rule with the earlier default option which has been removed:
300
301 ::: correct
302
303 ```js
304 // "strict": "error"
305
306 "use strict";
307
308 function foo() {
309 }
310 ```
311
312 :::
313
314 ::: correct
315
316 ```js
317 // "strict": "error"
318
319 function foo() {
320 "use strict";
321 }
322 ```
323
324 :::
325
326 ::: correct
327
328 ```js
329 // "strict": "error"
330
331 (function() {
332 "use strict";
333 function bar() {
334 "use strict";
335 }
336 }());
337 ```
338
339 :::
340
341 ## When Not To Use It
342
343 In a codebase that has both strict and non-strict code, either turn this rule off, or [selectively disable it](../use/configure/rules#disabling-rules) where necessary. For example, functions referencing `arguments.callee` are invalid in strict mode. A [full list of strict mode differences](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode/Transitioning_to_strict_mode#Differences_from_non-strict_to_strict) is available on MDN.