useStrictMode
¥Summary
-
规则生效日期:
v1.8.0¥Rule available since:
v1.8.0 -
诊断类别:
lint/suspicious/useStrictMode¥Diagnostic Category:
lint/suspicious/useStrictMode -
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
¥How to configure
{ "linter": { "rules": { "suspicious": { "useStrictMode": "error" } } }}¥Description
强制在脚本文件中使用指令 "use strict"。
¥Enforce the use of the directive "use strict" in script files.
JavaScript 严格模式 禁止了一些过时的 JavaScript 语法,并进行了一些细微的语义更改,以便 JavaScript 引擎能够进行更多优化。EcmaScript 模块始终处于严格模式,而 JavaScript 脚本默认处于非严格模式,也称为松散模式。开发者可以在脚本文件的开头添加 "use strict" 指令,以启用该文件中的严格模式。
¥The JavaScript strict mode prohibits some obsolete JavaScript syntaxes and makes some slight semantic changes to allow more optimizations by JavaScript engines.
EcmaScript modules are always in strict mode, while JavaScript scripts are by default in non-strict mode, also known as sloppy mode.
A developer can add the "use strict" directive at the start of a script file to enable the strict mode in that file.
Biome 将 CommonJS(.cjs)文件视为脚本文件。默认情况下,Biome 将 JavaScript 文件 (.js) 识别为模块文件,除非在 package.json 中指定了 "type": "commonjs"。
¥Biome considers a CommonJS (.cjs) file as a script file.
By default, Biome recognizes a JavaScript file (.js) as a module file, except if "type": "commonjs" is specified in package.json.
¥Examples
¥Invalid
var a = 1;code-block.cjs:1:1 lint/suspicious/useStrictMode FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Unexpected absence of the directive “use strict”.
> 1 │ var a = 1;
│ ^^^^^^^^^^
2 │
ℹ Strict mode allows to opt-in some optimisations of the runtime engines, and it eliminates some JavaScript silent errors by changing them to throw errors.
ℹ Check the MDN web docs for more information regarding strict mode.
ℹ Safe fix: Insert a top level “use strict”.
1 │ + “use·strict”;
1 2 │ var a = 1;
2 3 │
¥Valid
"use strict";
var a = 1;¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号