]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/strict.md
import eslint 7.28.0
[pve-eslint.git] / eslint / docs / rules / strict.md
1 # require or disallow strict mode directives (strict)
2
3 A strict mode directive is a `"use strict"` literal at the beginning of a script or function body. It enables strict mode semantics.
4
5 When a directive occurs in global scope, strict mode applies to the entire script:
6
7 ```js
8 "use strict";
9
10 // strict mode
11
12 function foo() {
13 // strict mode
14 }
15 ```
16
17 When a directive occurs at the beginning of a function body, strict mode applies only to that function, including all contained functions:
18
19 ```js
20 function foo() {
21 "use strict";
22 // strict mode
23 }
24
25 function foo2() {
26 // not strict mode
27 };
28
29 (function() {
30 "use strict";
31 function bar() {
32 // strict mode
33 }
34 }());
35 ```
36
37 In the **CommonJS** module system, a hidden function wraps each module and limits the scope of a "global" strict mode directive.
38
39 In **ECMAScript** modules, which always have strict mode semantics, the directives are unnecessary.
40
41 ## Rule Details
42
43 This rule requires or disallows strict mode directives.
44
45 This rule disallows strict mode directives, no matter which option is specified, if ESLint configuration specifies either of the following as [parser options](/docs/user-guide/configuring/language-options.md#specifying-parser-options):
46
47 * `"sourceType": "module"` that is, files are **ECMAScript** modules
48 * `"impliedStrict": true` property in the `ecmaFeatures` object
49
50 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.
51
52 The `--fix` option on the command line does not insert new `"use strict"` statements, but only removes unneeded statements.
53
54 ## Options
55
56 This rule has a string option:
57
58 * `"safe"` (default) corresponds either of the following options:
59 * `"global"` if ESLint considers a file to be a **CommonJS** module
60 * `"function"` otherwise
61 * `"global"` requires one strict mode directive in the global scope (and disallows any other strict mode directives)
62 * `"function"` requires one strict mode directive in each top-level function declaration or expression (and disallows any other strict mode directives)
63 * `"never"` disallows strict mode directives
64
65 ### safe
66
67 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:
68
69 * `node` or `commonjs` [environments](/docs/user-guide/configuring/language-options.md#specifying-environments)
70 * `"globalReturn": true` property in the `ecmaFeatures` object of [parser options](/docs/user-guide/configuring/language-options.md#specifying-parser-options)
71
72 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.
73
74 ### global
75
76 Examples of **incorrect** code for this rule with the `"global"` option:
77
78 ```js
79 /*eslint strict: ["error", "global"]*/
80
81 function foo() {
82 }
83 ```
84
85 ```js
86 /*eslint strict: ["error", "global"]*/
87
88 function foo() {
89 "use strict";
90 }
91 ```
92
93 ```js
94 /*eslint strict: ["error", "global"]*/
95
96 "use strict";
97
98 function foo() {
99 "use strict";
100 }
101 ```
102
103 Examples of **correct** code for this rule with the `"global"` option:
104
105 ```js
106 /*eslint strict: ["error", "global"]*/
107
108 "use strict";
109
110 function foo() {
111 }
112 ```
113
114 ### function
115
116 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.
117
118 Examples of **incorrect** code for this rule with the `"function"` option:
119
120 ```js
121 /*eslint strict: ["error", "function"]*/
122
123 "use strict";
124
125 function foo() {
126 }
127 ```
128
129 ```js
130 /*eslint strict: ["error", "function"]*/
131
132 function foo() {
133 }
134
135 (function() {
136 function bar() {
137 "use strict";
138 }
139 }());
140 ```
141
142 ```js
143 /*eslint strict: ["error", "function"]*/
144 /*eslint-env es6*/
145
146 // Illegal "use strict" directive in function with non-simple parameter list.
147 // This is a syntax error since ES2016.
148 function foo(a = 1) {
149 "use strict";
150 }
151
152 // We cannot write "use strict" directive in this function.
153 // So we have to wrap this function with a function with "use strict" directive.
154 function foo(a = 1) {
155 }
156 ```
157
158 Examples of **correct** code for this rule with the `"function"` option:
159
160 ```js
161 /*eslint strict: ["error", "function"]*/
162
163 function foo() {
164 "use strict";
165 }
166
167 (function() {
168 "use strict";
169
170 function bar() {
171 }
172
173 function baz(a = 1) {
174 }
175 }());
176
177 var foo = (function() {
178 "use strict";
179
180 return function foo(a = 1) {
181 };
182 }());
183 ```
184
185 ### never
186
187 Examples of **incorrect** code for this rule with the `"never"` option:
188
189 ```js
190 /*eslint strict: ["error", "never"]*/
191
192 "use strict";
193
194 function foo() {
195 }
196 ```
197
198 ```js
199 /*eslint strict: ["error", "never"]*/
200
201 function foo() {
202 "use strict";
203 }
204 ```
205
206 Examples of **correct** code for this rule with the `"never"` option:
207
208 ```js
209 /*eslint strict: ["error", "never"]*/
210
211 function foo() {
212 }
213 ```
214
215 ### earlier default (removed)
216
217 (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.
218
219 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.
220
221 Examples of **incorrect** code for this rule with the earlier default option which has been removed:
222
223 ```js
224 // "strict": "error"
225
226 function foo() {
227 }
228 ```
229
230 ```js
231 // "strict": "error"
232
233 (function() {
234 function bar() {
235 "use strict";
236 }
237 }());
238 ```
239
240 Examples of **correct** code for this rule with the earlier default option which has been removed:
241
242 ```js
243 // "strict": "error"
244
245 "use strict";
246
247 function foo() {
248 }
249 ```
250
251 ```js
252 // "strict": "error"
253
254 function foo() {
255 "use strict";
256 }
257 ```
258
259 ```js
260 // "strict": "error"
261
262 (function() {
263 "use strict";
264 function bar() {
265 "use strict";
266 }
267 }());
268 ```
269
270 ## When Not To Use It
271
272 In a codebase that has both strict and non-strict code, either turn this rule off, or [selectively disable it](/docs/user-guide/configuring/rules.md#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.