This repository was archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 650
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
96 changes: 96 additions & 0 deletions
96
crates/rome_js_analyze/src/analyzers/nursery/no_confusing_arrow.rs
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,96 @@ | ||
use rome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic}; | ||
use rome_console::markup; | ||
use rome_js_syntax::JsArrowFunctionExpression; | ||
|
||
declare_rule! { | ||
/// Disallow arrow functions where they could be confused with comparisons. | ||
/// | ||
/// Arrow functions (`=>`) are similar in syntax to some comparison operators (`>`, `<`, `<=`, and `>=`). | ||
/// This rule warns against using the arrow function syntax in places where it could be confused with a comparison operator. | ||
/// | ||
/// Source: https://eslint.org/docs/latest/rules/no-confusing-arrow | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// var x = a => 1 ? 2 : 3; | ||
/// var x = (a) => 1 ? 2 : 3; | ||
/// ``` | ||
/// | ||
/// ## Valid | ||
/// | ||
/// ```js | ||
/// var x = a => (1 ? 2 : 3); | ||
/// var x = (a) => (1 ? 2 : 3); | ||
/// var x = (a) => { | ||
/// return 1 ? 2 : 3; | ||
/// }; | ||
/// var x = a => { return 1 ? 2 : 3; }; | ||
/// ``` | ||
/// | ||
pub(crate) NoConfusingArrow { | ||
version: "next", | ||
name: "noConfusingArrow", | ||
recommended: false, | ||
} | ||
} | ||
|
||
impl Rule for NoConfusingArrow { | ||
type Query = Ast<JsArrowFunctionExpression>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let arrow_fn = ctx.query(); | ||
|
||
arrow_fn | ||
.body() | ||
.ok()? | ||
.as_any_js_expression()? | ||
.as_js_conditional_expression() | ||
.is_some() | ||
.then_some(()) | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
Some(RuleDiagnostic::new( | ||
rule_category!(), | ||
ctx.query().fat_arrow_token().ok()?.text_trimmed_range(), | ||
markup! { | ||
"Fat arrows can be confused with some comparison operators (" | ||
<Emphasis>"<"</Emphasis>", " | ||
<Emphasis>">"</Emphasis>", " | ||
<Emphasis>"<="</Emphasis>", " | ||
<Emphasis>">="</Emphasis> | ||
")." | ||
}, | ||
)) | ||
} | ||
|
||
// Formatter is not happy with the added parenthesis at the time of commit. | ||
// This should be fixed before enabling the action. | ||
//fn action(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<JsRuleAction> { | ||
// let body_expr = ctx.query().body().ok()?.as_any_js_expression()?.clone(); | ||
// | ||
// let mut mutation = ctx.root().begin(); | ||
// | ||
// mutation.replace_node( | ||
// body_expr.clone(), | ||
// AnyJsExpression::from(make::js_parenthesized_expression( | ||
// JsSyntaxToken::new_detached(JsSyntaxKind::L_PAREN, "(", [], []), | ||
// body_expr, | ||
// JsSyntaxToken::new_detached(JsSyntaxKind::R_PAREN, ")", [], []), | ||
// )), | ||
// ); | ||
// | ||
// Some(JsRuleAction { | ||
// category: ActionCategory::QuickFix, | ||
// applicability: Applicability::Always, | ||
// message: markup! { "Wrap the function body in parenthesis." }.to_owned(), | ||
// mutation, | ||
// }) | ||
//} | ||
} |
2 changes: 2 additions & 0 deletions
2
crates/rome_js_analyze/tests/specs/nursery/noConfusingArrow/invalid.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,2 @@ | ||
var x = a => 1 ? 2 : 3; | ||
var x = (a) => 1 ? 2 : 3; |
39 changes: 39 additions & 0 deletions
39
crates/rome_js_analyze/tests/specs/nursery/noConfusingArrow/invalid.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,39 @@ | ||
--- | ||
source: crates/rome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.js | ||
--- | ||
# Input | ||
```js | ||
var x = a => 1 ? 2 : 3; | ||
var x = (a) => 1 ? 2 : 3; | ||
|
||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.js:1:11 lint/nursery/noConfusingArrow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
! Fat arrows can be confused with some comparison operators (<, >, <=, >=). | ||
|
||
> 1 │ var x = a => 1 ? 2 : 3; | ||
│ ^^ | ||
2 │ var x = (a) => 1 ? 2 : 3; | ||
3 │ | ||
|
||
|
||
``` | ||
|
||
``` | ||
invalid.js:2:13 lint/nursery/noConfusingArrow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
! Fat arrows can be confused with some comparison operators (<, >, <=, >=). | ||
|
||
1 │ var x = a => 1 ? 2 : 3; | ||
> 2 │ var x = (a) => 1 ? 2 : 3; | ||
│ ^^ | ||
3 │ | ||
|
||
|
||
``` | ||
|
||
|
8 changes: 8 additions & 0 deletions
8
crates/rome_js_analyze/tests/specs/nursery/noConfusingArrow/valid.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,8 @@ | ||
/* should not generate diagnostics */ | ||
|
||
var x = a => (1 ? 2 : 3); | ||
var x = (a) => (1 ? 2 : 3); | ||
var x = (a) => { | ||
return 1 ? 2 : 3; | ||
}; | ||
var x = a => { return 1 ? 2 : 3; }; |
18 changes: 18 additions & 0 deletions
18
crates/rome_js_analyze/tests/specs/nursery/noConfusingArrow/valid.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,18 @@ | ||
--- | ||
source: crates/rome_js_analyze/tests/spec_tests.rs | ||
expression: valid.js | ||
--- | ||
# Input | ||
```js | ||
/* should not generate diagnostics */ | ||
|
||
var x = a => (1 ? 2 : 3); | ||
var x = (a) => (1 ? 2 : 3); | ||
var x = (a) => { | ||
return 1 ? 2 : 3; | ||
}; | ||
var x = a => { return 1 ? 2 : 3; }; | ||
|
||
``` | ||
|
||
|
Oops, something went wrong.
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.