Skip to content

noRedundantUseStrict

诊断类别:lint/suspicious/noRedundantUseStrict

¥Diagnostic Category: lint/suspicious/noRedundantUseStrict

自从:v1.0.0

¥Since: v1.0.0

防止有多余的 "use strict"

¥Prevents from having redundant "use strict".

.mjs 文件中,或在 package.json 将库定义为模块的项目内的 .js 文件中,不需要指令 "use strict"

¥The directive "use strict" isn’t needed in .mjs files, or in .js files inside projects where the package.json defines library as module:

{
"type": "module"
}

相反,.cjs 文件被视为 “scripts”,指令 "use strict" 被接受和建议。

¥Instead, .cjs files are considered “scripts” and the directive "use strict" is accepted and advised.

请注意,领先的琐事,例如冗余 "use strict" 之前的注释或换行符也将被删除。这样注释指令就不会被转移到错误的地方。

¥Note that the leading trivia, e.g., comments or newlines preceding the redundant "use strict" will also be removed. So that comment directives won’t be transferred to a wrong place.

¥Examples

¥Invalid

"use strict";
function foo() {
"use strict";
}
code-block.cjs:3:3 lint/suspicious/noRedundantUseStrict  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Redundant use strict directive.

1 │ “use strict”;
2 │ function foo() {
> 3 │ “use strict”;
^^^^^^^^^^^^^
4 │ }
5 │

This outer use strict directive already enables strict mode.

> 1 │ “use strict”;
^^^^^^^^^^^^^
2 │ function foo() {
3 │ “use strict”;

Safe fix: Remove the redundant use strict directive.

1 1 “use strict”;
2 2 function foo() {
3 - ·use·strict;
4 3 }
5 4

"use strict";
"use strict";
function foo() {
}
code-block.cjs:2:1 lint/suspicious/noRedundantUseStrict  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Redundant use strict directive.

1 │ “use strict”;
> 2 │ “use strict”;
^^^^^^^^^^^^^
3 │
4 │ function foo() {

This outer use strict directive already enables strict mode.

> 1 │ “use strict”;
^^^^^^^^^^^^^
2 │ “use strict”;
3 │

Safe fix: Remove the redundant use strict directive.

1 1 “use strict”;
2 - use·strict;
3 2
4 3 function foo() {

function foo() {
"use strict";
"use strict";
}
code-block.cjs:3:1 lint/suspicious/noRedundantUseStrict  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Redundant use strict directive.

1 │ function foo() {
2 │ “use strict”;
> 3 │ “use strict”;
^^^^^^^^^^^^^
4 │ }
5 │

This outer use strict directive already enables strict mode.

1 │ function foo() {
> 2 │ “use strict”;
^^^^^^^^^^^^^
3 │ “use strict”;
4 │ }

Safe fix: Remove the redundant use strict directive.

1 1 function foo() {
2 2 “use strict”;
3 - use·strict;
4 3 }
5 4

class C1 {
test() {
"use strict";
}
}
code-block.cjs:3:3 lint/suspicious/noRedundantUseStrict  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Redundant use strict directive.

1 │ class C1 {
2 │ test() {
> 3 │ “use strict”;
^^^^^^^^^^^^^
4 │ }
5 │ }

All parts of a class’s body are already in strict mode.

> 1 │ class C1 {
^^^^^^^^^^
> 2 │ test() {
> 3 │ “use strict”;
> 4 │ }
> 5 │ }
^
6 │

Safe fix: Remove the redundant use strict directive.

1 1 class C1 {
2 2 test() {
3 - use·strict;
4 3 }
5 4 }

const C2 = class {
test() {
"use strict";
}
};
code-block.cjs:3:3 lint/suspicious/noRedundantUseStrict  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Redundant use strict directive.

1 │ const C2 = class {
2 │ test() {
> 3 │ “use strict”;
^^^^^^^^^^^^^
4 │ }
5 │ };

All parts of a class’s body are already in strict mode.

> 1 │ const C2 = class {
^^^^^^^
> 2 │ test() {
> 3 │ “use strict”;
> 4 │ }
> 5 │ };
^
6 │

Safe fix: Remove the redundant use strict directive.

1 1 const C2 = class {
2 2 test() {
3 - use·strict;
4 3 }
5 4 };

¥Valid

function foo() {
}
function foo() {
"use strict";
}
function bar() {
"use strict";
}

¥Related links