-
-
Notifications
You must be signed in to change notification settings - Fork 969
fix(noRedundantUseStrict): keeps leading trivia #5868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<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())?; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(), | ||
|
|
||
7 changes: 7 additions & 0 deletions
7
crates/biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid-with-trivia.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
37 changes: 37 additions & 0 deletions
37
.../biome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/invalid-with-trivia.js.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| │ ----------------------- | ||
|
|
||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.