Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
refactor(rome_js_analyze): relax noConfusingArrow (#4593)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Jun 21, 2023
1 parent 3d61b99 commit fb2d3e6
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 47 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ parameter decorators:

The code action now removes any whitespace between the parameter name and its initialization.

- Relax [noConfusingArrow](https://docs.rome.tools/lint/rules/noconfusingarrow/)

All arrow functions that enclose its parameter with parenthesis are allowed.
Thus, the following snippet no longer trigger the rule:

```js
var x = (a) => 1 ? 2 : 3;
```

The following snippet still triggers the rule:

```js
var x = a => 1 ? 2 : 3;
```

- The rules [`useExhaustiveDependencies`](https://docs.rome.tools/lint/rules/useexhaustivedependencies/) and [`useHookAtTopLevel`](https://docs.rome.tools/lint/rules/usehookattoplevel/) accept a different shape of options

Old configuration
Expand Down
17 changes: 10 additions & 7 deletions crates/rome_js_analyze/src/analyzers/nursery/no_confusing_arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ declare_rule! {
/// ```js,expect_diagnostic
/// var x = a => 1 ? 2 : 3;
/// ```
/// ```js,expect_diagnostic
/// var x = (a) => 1 ? 2 : 3;
/// ```
///
/// ## Valid
///
/// ```js
/// var x = (a) => 1 ? 2 : 3;
///
/// 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; };
///
/// var x = (a) => { return 1 ? 2 : 3; };
/// ```
///
pub(crate) NoConfusingArrow {
Expand All @@ -47,7 +47,10 @@ impl Rule for NoConfusingArrow {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let arrow_fn = ctx.query();

if arrow_fn.parameters().ok()?.as_js_parameters().is_some() {
// Don't report arrow functions that enclose its parameters with parenthesis.
return None;
}
arrow_fn
.body()
.ok()?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
var x = a => 1 ? 2 : 3;
var x = (a) => 1 ? 2 : 3;
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
assertion_line: 100
expression: invalid.js
---
# Input
```js
var x = a => 1 ? 2 : 3;
var x = (a) => 1 ? 2 : 3;

```

Expand All @@ -17,21 +17,7 @@ invalid.js:1:11 lint/nursery/noConfusingArrow ━━━━━━━━━━━
> 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 │
2 │
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/* should not generate diagnostics */

var x = a => (1 ? 2 : 3);

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

var x = a => { return 1 ? 2 : 3; };
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
assertion_line: 100
expression: valid.js
---
# Input
```js
/* should not generate diagnostics */

var x = a => (1 ? 2 : 3);

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

var x = a => { return 1 ? 2 : 3; };

```
Expand Down
23 changes: 6 additions & 17 deletions website/src/pages/lint/rules/noConfusingArrow.md

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

0 comments on commit fb2d3e6

Please sign in to comment.