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 655
feat(rome_js_analyze): rule noVoid
#4619
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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.
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,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. | ||
/// | ||
/// > 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." | ||
} | ||
)) | ||
} | ||
} |
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,7 @@ | ||
void 0; | ||
function f() { | ||
return void 0; | ||
} | ||
var foo = void 0; | ||
void(0); | ||
|
83 changes: 83 additions & 0 deletions
83
crates/rome_js_analyze/tests/specs/nursery/noVoid/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,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. | ||
|
||
|
||
``` | ||
|
||
|
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,4 @@ | ||
var foo = bar(); | ||
foo.void(); | ||
foo.void = bar; | ||
delete foo; |
14 changes: 14 additions & 0 deletions
14
crates/rome_js_analyze/tests/specs/nursery/noVoid/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,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; | ||
|
||
``` | ||
|
||
|
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
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.