Skip to content

Commit 2db73ae

Browse files
authored
fix(noRedundantUseStrict): keeps leading trivia (#5868)
1 parent b324b5b commit 2db73ae

File tree

10 files changed

+90
-88
lines changed

10 files changed

+90
-88
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Fixed [#5856](https://github.com/biomejs/biome/issues/5856), `noRedundantUseStrict` now keeps leading trivia

crates/biome_js_analyze/src/lint/suspicious/no_redundant_use_strict.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use crate::JsRuleAction;
22
use biome_analyze::{Ast, FixKind, Rule, RuleDiagnostic, context::RuleContext, declare_lint_rule};
33
use biome_console::markup;
44
use biome_diagnostics::Severity;
5+
use biome_js_factory::make::js_directive;
56
use biome_js_syntax::{
67
AnyJsClass, JsDirective, JsDirectiveList, JsFileSource, JsFunctionBody, JsModule, JsScript,
8+
JsSyntaxKind, JsSyntaxToken,
79
};
810
use biome_rule_options::no_redundant_use_strict::NoRedundantUseStrictOptions;
911

@@ -23,10 +25,6 @@ declare_lint_rule! {
2325
///
2426
/// Instead, `.cjs` files are considered "scripts" and the directive `"use strict"` is accepted and advised.
2527
///
26-
/// Note that the leading trivia, e.g., comments or newlines preceding
27-
/// the redundant `"use strict"` will also be removed. So that comment
28-
/// directives won't be transferred to a wrong place.
29-
///
3028
/// ## Examples
3129
///
3230
/// ### Invalid
@@ -193,9 +191,17 @@ impl Rule for NoRedundantUseStrict {
193191
fn action(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<JsRuleAction> {
194192
let node = ctx.query();
195193
let mut mutation = ctx.root().begin();
196-
// This will also remove the trivia of the node
197-
// which is intended
198-
mutation.remove_node(node.clone());
194+
let value_token = node.value_token().ok()?;
195+
let new_node = js_directive(JsSyntaxToken::new_detached(
196+
JsSyntaxKind::JSX_TEXT_LITERAL,
197+
"",
198+
[],
199+
[],
200+
))
201+
.build()
202+
.with_leading_trivia_pieces(value_token.leading_trivia().pieces())?;
203+
204+
mutation.replace_node_discard_trivia(node.clone(), new_node);
199205
Some(JsRuleAction::new(
200206
ctx.metadata().action_category(ctx.category(), ctx.group()),
201207
ctx.metadata().applicability(),
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference types="node" />
2+
// comment
3+
// comment
4+
// comment
5+
"use strict" // comment
6+
7+
let foo = "foo"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
source: crates/biome_js_analyze/tests/spec_tests.rs
3+
expression: invalid-with-trivia.js
4+
---
5+
# Input
6+
```js
7+
/// <reference types="node" />
8+
// comment
9+
// comment
10+
// comment
11+
"use strict" // comment
12+
13+
let foo = "foo"
14+
15+
```
16+
17+
# Diagnostics
18+
```
19+
invalid-with-trivia.js:5:1 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
20+
21+
× Redundant use strict directive.
22+
23+
3 │ // comment
24+
4 │ // comment
25+
> 5 │ "use strict" // comment
26+
│ ^^^^^^^^^^^^
27+
6 │
28+
7 │ let foo = "foo"
29+
30+
i The entire contents of JavaScript modules are automatically in strict mode, with no statement needed to initiate it.
31+
32+
i Safe fix: Remove the redundant use strict directive.
33+
34+
5 │ "use·strict"·//·comment
35+
│ -----------------------
36+
37+
```

crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid.cjs.snap

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@ invalid.cjs:2:1 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━
4242
4343
i Safe fix: Remove the redundant use strict directive.
4444
45-
1 1"use strict";
46-
2- "use·strict";
47-
3 2
48-
4 3function test() {
49-
45+
2"use·strict";
46+
-------------
5047
5148
```
5249
@@ -70,12 +67,8 @@ invalid.cjs:5:2 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━
7067
7168
i Safe fix: Remove the redundant use strict directive.
7269
73-
3 3
74-
4 4function test() {
75-
5-"use·strict";
76-
6 5function inner_a() {
77-
7 6"use strict"; // redundant directive
78-
70+
5 │ → "use·strict";
71+
-------------
7972
8073
```
8174
@@ -100,12 +93,8 @@ invalid.cjs:7:3 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━
10093
10194
i Safe fix: Remove the redundant use strict directive.
10295
103-
5 5"use strict";
104-
6 6function inner_a() {
105-
7- → → "use·strict"//·redundant·directive
106-
8 7 │ }
107-
9 8function inner_b() {
108-
96+
7 │ → → "use·strict"//·redundant·directive
97+
------------------------------------
10998
11099
```
111100
@@ -130,11 +119,7 @@ invalid.cjs:11:4 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━
130119
131120
i Safe fix: Remove the redundant use strict directive.
132121
133-
9 9function inner_b() {
134-
10 10function inner_inner() {
135-
11- → → → "use·strict"//·additional·redundant·directive
136-
12 11 │ }
137-
13 12 │ }
138-
122+
11 │ → → → "use·strict"//·additional·redundant·directive
123+
-----------------------------------------------
139124
140125
```

crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid.js.snap

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,8 @@ invalid.js:2:1 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━━
4343
4444
i Safe fix: Remove the redundant use strict directive.
4545
46-
1- //·js·module
47-
2- "use·strict"//·Associated·comment
48-
1+
49-
3 2
50-
4 3function foo() {
51-
46+
2"use·strict"//·Associated·comment
47+
-----------------------------------
5248
5349
```
5450
@@ -67,12 +63,8 @@ invalid.js:5:2 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━━
6763
6864
i Safe fix: Remove the redundant use strict directive.
6965
70-
3 3
71-
4 4function foo() {
72-
5-"use·strict";
73-
6 5 │ }
74-
7 6
75-
66+
5 │ → "use·strict";
67+
-------------
7668
7769
```
7870
@@ -92,12 +84,8 @@ invalid.js:11:3 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━
9284
9385
i Safe fix: Remove the redundant use strict directive.
9486
95-
9 9// All code here is evaluated in strict mode
96-
10 10test() {
97-
11 │ - → → "use·strict";
98-
12 11 │ }
99-
13 12}
100-
87+
11 │ → → "use·strict";
88+
│ -------------
10189
10290
```
10391

@@ -117,11 +105,7 @@ invalid.js:18:3 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━
117105
118106
i Safe fix: Remove the redundant use strict directive.
119107
120-
16 16 │ // All code here is evaluated in strict mode
121-
17 17 │ test() {
122-
18- → → "use·strict";
123-
19 18}
124-
20 19 │ };
125-
108+
18 │ → → "use·strict";
109+
│ -------------
126110
127111
```

crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid.ts.snap

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ invalid.ts:2:2 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━━━
2626
2727
i Safe fix: Remove the redundant use strict directive.
2828
29-
1 1 │ function test(): void {
30-
2-"use·strict";
31-
3 2}
32-
4 3 │
33-
29+
2 │ → "use·strict";
30+
│ -------------
3431
3532
```

crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalidClass.cjs.snap

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,8 @@ invalidClass.cjs:3:3 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━
4545
4646
i Safe fix: Remove the redundant use strict directive.
4747
48-
1 1class C1 {
49-
2 2 │ test() {
50-
3- → → "use·strict";
51-
4 3 │ }
52-
5 4 │ }
53-
48+
3 │ → → "use·strict";
49+
-------------
5450
5551
```
5652
@@ -81,11 +77,7 @@ invalidClass.cjs:9:3 lint/suspicious/noRedundantUseStrict FIXABLE ━━━━
8177
8278
i Safe fix: Remove the redundant use strict directive.
8379
84-
7 7const C2 = class {
85-
8 8 │ test() {
86-
9- → → "use·strict";
87-
10 9 │ }
88-
11 10 │ };
89-
80+
9 │ → → "use·strict";
81+
-------------
9082
9183
```

crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalidFunction.cjs.snap

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ invalidFunction.cjs:3:2 lint/suspicious/noRedundantUseStrict FIXABLE ━━━
3434
3535
i Safe fix: Remove the redundant use strict directive.
3636
37-
1 1 │ function test() {
38-
2 2"use strict";
39-
3-"use·strict";
40-
4 3}
41-
5 4 │
42-
37+
3 │ → "use·strict";
38+
│ -------------
4339
4440
```

crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalidFunction.js.snap

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ invalidFunction.js:2:2 lint/suspicious/noRedundantUseStrict FIXABLE ━━━
2727
2828
i Safe fix: Remove the redundant use strict directive.
2929
30-
1 1 │ function test() {
31-
2-"use·strict";
32-
3 2"use strict";
33-
4 3}
34-
30+
2 │ → "use·strict";
31+
│ -------------
3532
3633
```
3734

@@ -51,11 +48,7 @@ invalidFunction.js:3:2 lint/suspicious/noRedundantUseStrict FIXABLE ━━━
5148
5249
i Safe fix: Remove the redundant use strict directive.
5350
54-
1 1 │ function test() {
55-
2 2"use strict";
56-
3-"use·strict";
57-
4 3}
58-
5 4 │
59-
51+
3 │ → "use·strict";
52+
│ -------------
6053
6154
```

0 commit comments

Comments
 (0)