Skip to content

useStrictMode

诊断类别:lint/nursery/useStrictMode

¥Diagnostic Category: lint/nursery/useStrictMode

自从:v1.8.0

¥Since: v1.8.0

强制在脚本文件中使用指令 "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 chnmages 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/nursery/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 for more information regarding strict mode.

Safe fix: Insert a top level”use strict” .

1 │ use·strictvar·a·=·1;
++++++++++++

¥Valid

"use strict";
var a = 1;

¥Related links