-
-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): eslint: no-void (#2162)
Rule Detail: [link](https://github.com/eslint/eslint/blob/main/lib/rules/no-void.js)
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,97 @@ | ||
use crate::{context::LintContext, rule::Rule, AstNode}; | ||
|
||
use oxc_ast::AstKind; | ||
use oxc_diagnostics::{ | ||
miette::{self, Diagnostic}, | ||
thiserror::Error, | ||
}; | ||
use oxc_macros::declare_oxc_lint; | ||
use oxc_span::Span; | ||
use oxc_syntax::operator::UnaryOperator; | ||
|
||
#[derive(Debug, Error, Diagnostic)] | ||
#[error("eslint(no-void): Disallow `void` operators")] | ||
#[diagnostic(severity(warning), help("Expected 'undefined' and instead saw 'void'."))] | ||
struct NoVoidDiagnostic(#[label] pub Span); | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct NoVoid { | ||
pub allow_as_statement: bool, | ||
} | ||
|
||
declare_oxc_lint!( | ||
/// ### What it does | ||
/// | ||
/// Disallow `void` operators. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```javascript | ||
/// // error | ||
/// void 0; | ||
/// var foo = void 0; | ||
/// | ||
/// // success | ||
/// "var foo = bar()", | ||
/// "foo.void()", | ||
/// "foo.void = bar", | ||
/// ``` | ||
NoVoid, | ||
restriction, | ||
); | ||
|
||
impl Rule for NoVoid { | ||
fn from_configuration(value: serde_json::Value) -> Self { | ||
let allow_as_statement = value | ||
.get(0) | ||
.and_then(|config| config.get("allowAsStatement")) | ||
.and_then(serde_json::Value::as_bool) | ||
.unwrap_or(false); | ||
|
||
Self { allow_as_statement } | ||
} | ||
|
||
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { | ||
let AstKind::UnaryExpression(unary_expr) = node.kind() else { | ||
return; | ||
}; | ||
|
||
if let Some(kind) = ctx.nodes().parent_kind(node.id()) { | ||
if self.allow_as_statement && matches!(kind, AstKind::ExpressionStatement(_)) { | ||
return; | ||
} | ||
}; | ||
|
||
if unary_expr.operator == UnaryOperator::Void { | ||
ctx.diagnostic(NoVoidDiagnostic(Span { | ||
start: unary_expr.span.start, | ||
end: unary_expr.span.start + 4, | ||
})); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
use crate::tester::Tester; | ||
|
||
let pass = vec![ | ||
("var foo = bar()", None), | ||
("foo.void()", None), | ||
("foo.void = bar", None), | ||
("delete foo;", None), | ||
("void 0", Some(serde_json::json!([{ "allowAsStatement": true }]))), | ||
("void(0)", Some(serde_json::json!([{ "allowAsStatement": true }]))), | ||
]; | ||
|
||
let fail = vec![ | ||
("void 0", None), | ||
("void 0", Some(serde_json::json!([{}]))), | ||
("void 0", Some(serde_json::json!([{ "allowAsStatement": false }]))), | ||
("void(0)", None), | ||
("var foo = void 0", None), | ||
("var foo = void 0", Some(serde_json::json!([{ "allowAsStatement": true }]))), | ||
]; | ||
|
||
Tester::new(NoVoid::NAME, pass, fail).test_and_snapshot(); | ||
} |
This file contains 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,48 @@ | ||
--- | ||
source: crates/oxc_linter/src/tester.rs | ||
assertion_line: 150 | ||
expression: no_void | ||
--- | ||
⚠ eslint(no-void): Disallow `void` operators | ||
╭─[no_void.tsx:1:1] | ||
1 │ void 0 | ||
· ──── | ||
╰──── | ||
help: Expected 'undefined' and instead saw 'void'. | ||
|
||
⚠ eslint(no-void): Disallow `void` operators | ||
╭─[no_void.tsx:1:1] | ||
1 │ void 0 | ||
· ──── | ||
╰──── | ||
help: Expected 'undefined' and instead saw 'void'. | ||
|
||
⚠ eslint(no-void): Disallow `void` operators | ||
╭─[no_void.tsx:1:1] | ||
1 │ void 0 | ||
· ──── | ||
╰──── | ||
help: Expected 'undefined' and instead saw 'void'. | ||
|
||
⚠ eslint(no-void): Disallow `void` operators | ||
╭─[no_void.tsx:1:1] | ||
1 │ void(0) | ||
· ──── | ||
╰──── | ||
help: Expected 'undefined' and instead saw 'void'. | ||
|
||
⚠ eslint(no-void): Disallow `void` operators | ||
╭─[no_void.tsx:1:1] | ||
1 │ var foo = void 0 | ||
· ──── | ||
╰──── | ||
help: Expected 'undefined' and instead saw 'void'. | ||
|
||
⚠ eslint(no-void): Disallow `void` operators | ||
╭─[no_void.tsx:1:1] | ||
1 │ var foo = void 0 | ||
· ──── | ||
╰──── | ||
help: Expected 'undefined' and instead saw 'void'. | ||
|
||
|