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

refactor(rome_js_analyze): relax noConfusingArrow #4593

Merged
merged 1 commit into from
Jun 21, 2023
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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,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.