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

feat(rome_js_analyze): rule noVoid #4619

Merged
merged 4 commits into from
Jun 26, 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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ multiple files:
This rule proposes turning function expressions into arrow functions.
Function expressions that use `this` are ignored.

- [`noDuplicateJsonKeys`](https://docs.rome.tools/lint/rules/noDuplicateJsonKeys/)
- Add [`noDuplicateJsonKeys`](https://docs.rome.tools/lint/rules/noDuplicateJsonKeys/)

This rule disallow duplicate keys in a JSON object.

- Add [`noVoid`](https://docs.rome.tools/lint/rules/novoid/)

This rules disallow the use of `void`.

#### Other changes

Expand Down
7 changes: 4 additions & 3 deletions crates/rome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ define_categories! {
"lint/nursery/noStaticOnlyClass": "https://docs.rome.tools/lint/rules/noStaticOnlyClass",
"lint/nursery/noDuplicateJsonKeys": "https://docs.rome.tools/lint/rules/noDuplicateJsonKeys",
"lint/nursery/useNamingConvention": "https://docs.rome.tools/lint/rules/useNamingConvention",
"lint/nursery/noGlobalIsNan": "https://docs.rome.tools/lint/rules/noGlobalIsNan",
"lint/nursery/noGlobalIsFinite": "https://docs.rome.tools/lint/rules/noGlobalIsFinite",
"lint/nursery/useArrowFunction": "https://docs.rome.tools/lint/rules/useArrowFunction",
"lint/nursery/noGlobalIsNan": "https://docs.rome.tools/lint/rules/noGlobalIsNan",
"lint/nursery/noGlobalIsFinite": "https://docs.rome.tools/lint/rules/noGlobalIsFinite",
"lint/nursery/useArrowFunction": "https://docs.rome.tools/lint/rules/useArrowFunction",
"lint/nursery/noVoid": "https://docs.rome.tools/lint/rules/noVoid",
// Insert new nursery rule here


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.

58 changes: 58 additions & 0 deletions crates/rome_js_analyze/src/analyzers/nursery/no_void.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use rome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic};
use rome_console::markup;
use rome_js_syntax::JsUnaryExpression;
use rome_rowan::AstNode;

declare_rule! {
/// Disallow the use of `void` operators, which is not a familiar operator.
///
ematipico marked this conversation as resolved.
Show resolved Hide resolved
/// > The `void` operator is often used merely to obtain the undefined primitive value,
/// > usually using `void(0)` (which is equivalent to `void 0`). In these cases, the global variable `undefined` can be used.
///
/// Source: https://eslint.org/docs/latest/rules/no-void
///
/// ## Examples
///
/// ### Invalid
///
/// ```js,expect_diagnostic
/// void 0;
/// ```
///
pub(crate) NoVoid {
version: "next",
name: "noVoid",
recommended: false,
}
}

impl Rule for NoVoid {
type Query = Ast<JsUnaryExpression>;
type State = ();
type Signals = Option<Self::State>;
type Options = ();

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let expression = ctx.query();
if expression.is_void().ok()? {
Some(())
} else {
None
}
}

fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> {
let node = ctx.query();
Some(RuleDiagnostic::new(
rule_category!(),
node.range(),
markup! {
"The use of "<Emphasis>"void"</Emphasis>" is not allowed."
ematipico marked this conversation as resolved.
Show resolved Hide resolved
},
).note(
markup!{
"If you use "<Emphasis>"void"</Emphasis>" to alter the return type of a function or return `undefined`, use the global `undefined` instead."
}
))
}
}
7 changes: 7 additions & 0 deletions crates/rome_js_analyze/tests/specs/nursery/noVoid/invalid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
void 0;
function f() {
return void 0;
}
var foo = void 0;
void(0);

83 changes: 83 additions & 0 deletions crates/rome_js_analyze/tests/specs/nursery/noVoid/invalid.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: invalid.js
---
# Input
```js
void 0;
function f() {
return void 0;
}
var foo = void 0;
void(0);


```

# Diagnostics
```
invalid.js:1:1 lint/nursery/noVoid ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! The use of void is not allowed.

> 1 │ void 0;
│ ^^^^^^
2 │ function f() {
3 │ return void 0;

i If you use void to alter the return type of a function or return `undefined`, use the global `undefined` instead.


```

```
invalid.js:3:9 lint/nursery/noVoid ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! The use of void is not allowed.

1 │ void 0;
2 │ function f() {
> 3 │ return void 0;
│ ^^^^^^
4 │ }
5 │ var foo = void 0;

i If you use void to alter the return type of a function or return `undefined`, use the global `undefined` instead.


```

```
invalid.js:5:11 lint/nursery/noVoid ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! The use of void is not allowed.

3 │ return void 0;
4 │ }
> 5 │ var foo = void 0;
│ ^^^^^^
6 │ void(0);
7 │

i If you use void to alter the return type of a function or return `undefined`, use the global `undefined` instead.


```

```
invalid.js:6:1 lint/nursery/noVoid ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! The use of void is not allowed.

4 │ }
5 │ var foo = void 0;
> 6 │ void(0);
│ ^^^^^^^
7 │

i If you use void to alter the return type of a function or return `undefined`, use the global `undefined` instead.


```


4 changes: 4 additions & 0 deletions crates/rome_js_analyze/tests/specs/nursery/noVoid/valid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var foo = bar();
foo.void();
foo.void = bar;
delete foo;
14 changes: 14 additions & 0 deletions crates/rome_js_analyze/tests/specs/nursery/noVoid/valid.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: valid.js
---
# Input
```js
var foo = bar();
foo.void();
foo.void = bar;
delete foo;

```


6 changes: 6 additions & 0 deletions crates/rome_js_syntax/src/expr_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ impl JsUnaryExpression {
})
}

pub fn is_void(&self) -> SyntaxResult<bool> {
let operator = self.operator()?;

Ok(matches!(operator, JsUnaryOperator::Void))
}

/// This function checks that `JsUnaryExpression` is a signed numeric literal:
/// ```js
/// +123
Expand Down
Loading