From 0e5a6d68a41e112d486ea14dabafa05dd73e01a9 Mon Sep 17 00:00:00 2001 From: bushuai Date: Sat, 3 May 2025 15:53:31 +0800 Subject: [PATCH 1/3] fix(noRedundantUseStrict): preserve leading trivia --- .../suspicious/no_redundant_use_strict.rs | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/biome_js_analyze/src/lint/suspicious/no_redundant_use_strict.rs b/crates/biome_js_analyze/src/lint/suspicious/no_redundant_use_strict.rs index 3ef8492516fb..a831c147ef29 100644 --- a/crates/biome_js_analyze/src/lint/suspicious/no_redundant_use_strict.rs +++ b/crates/biome_js_analyze/src/lint/suspicious/no_redundant_use_strict.rs @@ -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}; @@ -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. - /// /// ## Examples /// /// ### Invalid @@ -192,9 +190,17 @@ impl Rule for NoRedundantUseStrict { fn action(ctx: &RuleContext, _state: &Self::State) -> Option { 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())?; + + mutation.replace_node_discard_trivia(node.clone(), new_node); Some(JsRuleAction::new( ctx.metadata().action_category(ctx.category(), ctx.group()), ctx.metadata().applicability(), From e729eb6957b0b7eb1be39d0b4d1633362fd1cdbc Mon Sep 17 00:00:00 2001 From: bushuai Date: Sat, 3 May 2025 17:28:27 +0800 Subject: [PATCH 2/3] test: add test case and update snapshots --- .../invalid-with-trivia.js | 7 ++++ .../invalid-with-trivia.js.snap | 37 +++++++++++++++++++ .../noRedundantUseStrict/invalid.cjs.snap | 32 ++++------------ .../noRedundantUseStrict/invalid.js.snap | 33 ++++------------- .../noRedundantUseStrict/invalid.ts.snap | 8 +--- .../invalidClass.cjs.snap | 17 ++------- .../invalidFunction.cjs.snap | 9 +---- .../invalidFunction.js.snap | 16 ++------ 8 files changed, 72 insertions(+), 87 deletions(-) create mode 100644 crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid-with-trivia.js create mode 100644 crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid-with-trivia.js.snap diff --git a/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid-with-trivia.js b/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid-with-trivia.js new file mode 100644 index 000000000000..c1a7ea7821a5 --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid-with-trivia.js @@ -0,0 +1,7 @@ +/// +// comment +// comment +// comment +"use strict" // comment + +let foo = "foo" diff --git a/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid-with-trivia.js.snap b/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid-with-trivia.js.snap new file mode 100644 index 000000000000..2787bf1811a3 --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid-with-trivia.js.snap @@ -0,0 +1,37 @@ +--- +source: crates/biome_js_analyze/tests/spec_tests.rs +expression: invalid-with-trivia.js +--- +# Input +```js +/// +// 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 + │ ----------------------- + +``` diff --git a/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid.cjs.snap b/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid.cjs.snap index 317033f8241a..363752b30c30 100644 --- a/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid.cjs.snap +++ b/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid.cjs.snap @@ -1,7 +1,6 @@ --- source: crates/biome_js_analyze/tests/spec_tests.rs expression: invalid.cjs -snapshot_kind: text --- # Input ```cjs @@ -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"; + │ ------------- ``` @@ -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"; + │ ------------- ``` @@ -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 + │ ------------------------------------ ``` @@ -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 + │ ----------------------------------------------- ``` diff --git a/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid.js.snap b/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid.js.snap index 8a6af0103874..424662291b70 100644 --- a/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid.js.snap +++ b/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid.js.snap @@ -1,7 +1,6 @@ --- source: crates/biome_js_analyze/tests/spec_tests.rs expression: invalid.js -snapshot_kind: text --- # Input ```js @@ -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 + │ ----------------------------------- ``` @@ -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"; + │ ------------- ``` @@ -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"; + │ ------------- ``` @@ -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"; + │ ------------- ``` diff --git a/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid.ts.snap b/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid.ts.snap index d875b7ac3907..4a526bb661c7 100644 --- a/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid.ts.snap +++ b/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid.ts.snap @@ -1,7 +1,6 @@ --- source: crates/biome_js_analyze/tests/spec_tests.rs expression: invalid.ts -snapshot_kind: text --- # Input ```ts @@ -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"; + │ ------------- ``` diff --git a/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalidClass.cjs.snap b/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalidClass.cjs.snap index 1779f5eac3fa..6b7bc21757c9 100644 --- a/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalidClass.cjs.snap +++ b/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalidClass.cjs.snap @@ -1,7 +1,6 @@ --- source: crates/biome_js_analyze/tests/spec_tests.rs expression: invalidClass.cjs -snapshot_kind: text --- # Input ```cjs @@ -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"; + │ ------------- ``` @@ -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"; + │ ------------- ``` diff --git a/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalidFunction.cjs.snap b/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalidFunction.cjs.snap index 4ad0ee7bbfd3..1eacadc187d4 100644 --- a/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalidFunction.cjs.snap +++ b/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalidFunction.cjs.snap @@ -1,7 +1,6 @@ --- source: crates/biome_js_analyze/tests/spec_tests.rs expression: invalidFunction.cjs -snapshot_kind: text --- # Input ```cjs @@ -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"; + │ ------------- ``` diff --git a/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalidFunction.js.snap b/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalidFunction.js.snap index b40cc3334a82..a48414c2107e 100644 --- a/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalidFunction.js.snap +++ b/crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalidFunction.js.snap @@ -1,7 +1,6 @@ --- source: crates/biome_js_analyze/tests/spec_tests.rs expression: invalidFunction.js -snapshot_kind: text --- # Input ```js @@ -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"; + │ ------------- ``` @@ -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"; + │ ------------- ``` From 3b9f1554267721ad760a3fdeef04afe69006462b Mon Sep 17 00:00:00 2001 From: bushuai Date: Sat, 3 May 2025 17:28:43 +0800 Subject: [PATCH 3/3] docs: add changeset --- .changeset/public-experts-change.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/public-experts-change.md diff --git a/.changeset/public-experts-change.md b/.changeset/public-experts-change.md new file mode 100644 index 000000000000..f25930a26550 --- /dev/null +++ b/.changeset/public-experts-change.md @@ -0,0 +1,5 @@ +--- +"@biomejs/biome": patch +--- + +Fixed [#5856](https://github.com/biomejs/biome/issues/5856), `noRedundantUseStrict` now keeps leading trivia