strict
Require or disallow "use strict" directives
Strict mode turns silent mistakes into errors. For example, assigning to a variable that was never declared throws instead of creating a global. ES modules and class bodies are always strict, so a "use strict" directive there does nothing. CommonJS files and plain scripts are only strict if they say so.
In a module, every "use strict" is reported, no matter which option you set. In other files, the option decides where the directive goes. By default a .js file is linted as a module and a .cjs file as CommonJS.
--fix removes directives that do nothing: in a module, in a class, in a function that's already strict, or written twice. It never adds one.
Configure#
Off by default. To turn it on:
[lint.rules]
"strict" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"strict" = ["error", "safe"] # "never", "global", "function" or "safe"Examples#
Reported, because the file is a module:
"use strict";'use strict' is unnecessary inside of modules.
export function run() {
save();
}Reported, because a CommonJS file needs the directive at the top:
const { save } = require("./store.cjs");Use the global form of 'use strict'.
save();export function run() {
save();
}In a .cjs file:
"use strict";
const { save } = require("./store.cjs");
save();Options#
| Option | What it does |
|---|---|
"safe" (default) | The same as "global" in a CommonJS file, and the same as "function" in any other script. |
"global" | Require one directive at the top of the file, and report directives inside functions. |
"function" | Require a directive in every outermost function, and report one at the top of the file. Use it for scripts that are joined with other scripts, where a directive at the top would apply to all of them. |
"never" | Report every directive. |
With "function" in a .cjs file, functions nested in a strict function don't need their own directive:
"function"No problemsfunction setup() {
"use strict";
function render() {
draw();
}
render();
}Here both the directive at the top and setup are reported:
"function"2 problems"use strict";Use the function form of 'use strict'.
function setup() {Use the function form of 'use strict'.
draw();
}When not to use it#
If all your files are ES modules, there's nothing to decide. The rule then only catches a leftover "use strict".