+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
### Editors
### Formatter
### Linter
#### New rules
- [`noConfusingArrow`](https://docs.rome.tools/lint/rules/noConfusingArrow/)
### Parser
### VSCode
### JavaScript APIs


## 12.0.0

### CLI
Expand Down Expand Up @@ -130,7 +131,10 @@ details.
a configuration file in your workspace/project. If Rome doesn't find a `rome.json` file, it won't
emit diagnostics.

<<<<<<< HEAD

=======
>>>>>>> 9abb92e (release: 12.0.0 (#4002))
## 11.0.0

### CLI
Expand Down
1 change: 1 addition & 0 deletions crates/rome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ define_categories! {
"lint/nursery/noUselessCatch": "https://docs.rome.tools/lint/rules/noUselessCatch",
"lint/nursery/noParameterAssign": "https://docs.rome.tools/lint/rules/noParameterAssign",
"lint/nursery/noNamespace": "https://docs.rome.tools/lint/rules/noNamespace",
"lint/nursery/noConfusingArrow": "https://docs.rome.tools/lint/rules/noConfusingArrow",
// Insert new nursery rule here
"lint/nursery/noRedeclare": "https://docs.rome.tools/lint/rules/noRedeclare",
"lint/nursery/useNamespaceKeyword": "https://docs.rome.tools/lint/rules/useNamespaceKeyword",
Expand Down
3 changes: 2 additions & 1 deletion crates/rome_js_analyze/src/analyzers/nursery.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 96 additions & 0 deletions crates/rome_js_analyze/src/analyzers/nursery/no_confusing_arrow.rs
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,
// })
//}
}
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;
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 │


```


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; };
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; };

```


Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载