Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/public-experts-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@biomejs/biome": patch
---

Fixed [#5856](https://github.com/biomejs/biome/issues/5856), `noRedundantUseStrict` now keeps leading trivia
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use crate::JsRuleAction;
use biome_analyze::{Ast, FixKind, Rule, RuleDiagnostic, context::RuleContext, declare_lint_rule};
use biome_console::markup;
use biome_diagnostics::Severity;
use biome_js_factory::make::js_directive;
use biome_js_syntax::{
AnyJsClass, JsDirective, JsDirectiveList, JsFileSource, JsFunctionBody, JsModule, JsScript,
JsSyntaxKind, JsSyntaxToken,
};

use biome_rowan::{AstNode, AstNodeList, BatchMutationExt, declare_node_union};
Expand All @@ -22,10 +24,6 @@ declare_lint_rule! {
///
/// Instead, `.cjs` files are considered "scripts" and the directive `"use strict"` is accepted and advised.
///
/// 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.
Comment thread
ematipico marked this conversation as resolved.
///
/// ## Examples
///
/// ### Invalid
Expand Down Expand Up @@ -192,9 +190,17 @@ impl Rule for NoRedundantUseStrict {
fn action(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<JsRuleAction> {
let node = ctx.query();
let mut mutation = ctx.root().begin();
// This will also remove the trivia of the node
// which is intended
mutation.remove_node(node.clone());
let value_token = node.value_token().ok()?;
let new_node = js_directive(JsSyntaxToken::new_detached(
JsSyntaxKind::JSX_TEXT_LITERAL,
"",
[],
[],
))
.build()
.with_leading_trivia_pieces(value_token.leading_trivia().pieces())?;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should reuse the same trivia piece only if there are comments. If you check the snapshots, all of them have been updated. Instead of deleting the whole line, we remove the directive but we keep the indentation that belonged to the directive.


mutation.replace_node_discard_trivia(node.clone(), new_node);
Some(JsRuleAction::new(
ctx.metadata().action_category(ctx.category(), ctx.group()),
ctx.metadata().applicability(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference types="node" />
// comment
// comment
// comment
"use strict" // comment

let foo = "foo"
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalid-with-trivia.js
---
# Input
```js
/// <reference types="node" />
// comment
// comment
// comment
"use strict" // comment

let foo = "foo"

```

# Diagnostics
```
invalid-with-trivia.js:5:1 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━

× Redundant use strict directive.

3 │ // comment
4 │ // comment
> 5 │ "use strict" // comment
│ ^^^^^^^^^^^^
6 │
7 │ let foo = "foo"

i The entire contents of JavaScript modules are automatically in strict mode, with no statement needed to initiate it.

i Safe fix: Remove the redundant use strict directive.

5 │ "use·strict"·//·comment
│ -----------------------

```
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalid.cjs
snapshot_kind: text
---
# Input
```cjs
Expand Down Expand Up @@ -43,11 +42,8 @@ invalid.cjs:2:1 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━

i Safe fix: Remove the redundant use strict directive.

1 1 │ "use strict";
2 │ - "use·strict";
3 2 │
4 3 │ function test() {

2 │ "use·strict";
│ -------------

```

Expand All @@ -71,12 +67,8 @@ invalid.cjs:5:2 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━

i Safe fix: Remove the redundant use strict directive.

3 3 │
4 4 │ function test() {
5 │ - → "use·strict";
6 5 │ function inner_a() {
7 6 │ "use strict"; // redundant directive

5 │ → "use·strict";
│ -------------

```

Expand All @@ -101,12 +93,8 @@ invalid.cjs:7:3 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━

i Safe fix: Remove the redundant use strict directive.

5 5 │ "use strict";
6 6 │ function inner_a() {
7 │ - → → "use·strict";·//·redundant·directive
8 7 │ }
9 8 │ function inner_b() {

7 │ → → "use·strict";·//·redundant·directive
│ ------------------------------------

```

Expand All @@ -131,11 +119,7 @@ invalid.cjs:11:4 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━

i Safe fix: Remove the redundant use strict directive.

9 9 │ function inner_b() {
10 10 │ function inner_inner() {
11 │ - → → → "use·strict";·//·additional·redundant·directive
12 11 │ }
13 12 │ }

11 │ → → → "use·strict";·//·additional·redundant·directive
│ -----------------------------------------------

```
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalid.js
snapshot_kind: text
---
# Input
```js
Expand Down Expand Up @@ -44,12 +43,8 @@ invalid.js:2:1 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━━

i Safe fix: Remove the redundant use strict directive.

1 │ - //·js·module
2 │ - "use·strict";·//·Associated·comment
1 │ +
3 2 │
4 3 │ function foo() {

2 │ "use·strict";·//·Associated·comment
│ -----------------------------------

```

Expand All @@ -68,12 +63,8 @@ invalid.js:5:2 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━━

i Safe fix: Remove the redundant use strict directive.

3 3 │
4 4 │ function foo() {
5 │ - → "use·strict";
6 5 │ }
7 6 │

5 │ → "use·strict";
│ -------------

```

Expand All @@ -93,12 +84,8 @@ invalid.js:11:3 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━

i Safe fix: Remove the redundant use strict directive.

9 9 │ // All code here is evaluated in strict mode
10 10 │ test() {
11 │ - → → "use·strict";
12 11 │ }
13 12 │ }

11 │ → → "use·strict";
│ -------------

```

Expand All @@ -118,11 +105,7 @@ invalid.js:18:3 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━

i Safe fix: Remove the redundant use strict directive.

16 16 │ // All code here is evaluated in strict mode
17 17 │ test() {
18 │ - → → "use·strict";
19 18 │ }
20 19 │ };

18 │ → → "use·strict";
│ -------------

```
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalid.ts
snapshot_kind: text
---
# Input
```ts
Expand All @@ -27,10 +26,7 @@ invalid.ts:2:2 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━━

i Safe fix: Remove the redundant use strict directive.

1 1 │ function test(): void {
2 │ - → "use·strict";
3 2 │ }
4 3 │

2 │ → "use·strict";
│ -------------

```
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalidClass.cjs
snapshot_kind: text
---
# Input
```cjs
Expand Down Expand Up @@ -46,12 +45,8 @@ invalidClass.cjs:3:3 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━

i Safe fix: Remove the redundant use strict directive.

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

3 │ → → "use·strict";
│ -------------

```

Expand Down Expand Up @@ -82,11 +77,7 @@ invalidClass.cjs:9:3 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━

i Safe fix: Remove the redundant use strict directive.

7 7 │ const C2 = class {
8 8 │ test() {
9 │ - → → "use·strict";
10 9 │ }
11 10 │ };

9 │ → → "use·strict";
│ -------------

```
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalidFunction.cjs
snapshot_kind: text
---
# Input
```cjs
Expand Down Expand Up @@ -35,11 +34,7 @@ invalidFunction.cjs:3:2 lint/suspicious/noRedundantUseStrict FIXABLE ━━━

i Safe fix: Remove the redundant use strict directive.

1 1 │ function test() {
2 2 │ "use strict";
3 │ - → "use·strict";
4 3 │ }
5 4 │

3 │ → "use·strict";
│ -------------

```
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalidFunction.js
snapshot_kind: text
---
# Input
```js
Expand All @@ -28,11 +27,8 @@ invalidFunction.js:2:2 lint/suspicious/noRedundantUseStrict FIXABLE ━━━

i Safe fix: Remove the redundant use strict directive.

1 1 │ function test() {
2 │ - → "use·strict";
3 2 │ "use strict";
4 3 │ }

2 │ → "use·strict";
│ -------------

```

Expand All @@ -52,11 +48,7 @@ invalidFunction.js:3:2 lint/suspicious/noRedundantUseStrict FIXABLE ━━━

i Safe fix: Remove the redundant use strict directive.

1 1 │ function test() {
2 2 │ "use strict";
3 │ - → "use·strict";
4 3 │ }
5 4 │

3 │ → "use·strict";
│ -------------

```