-
-
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-with (#2741)
Relates to #479 Rule detail: https://eslint.org/docs/latest/rules/no-with
- Loading branch information
Showing
3 changed files
with
68 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,56 @@ | ||
use oxc_ast::AstKind; | ||
use oxc_diagnostics::{ | ||
miette::{self, Diagnostic}, | ||
thiserror::Error, | ||
}; | ||
use oxc_macros::declare_oxc_lint; | ||
use oxc_span::Span; | ||
|
||
use crate::{context::LintContext, rule::Rule, AstNode}; | ||
|
||
#[derive(Debug, Error, Diagnostic)] | ||
#[error("eslint(no-with): Unexpected use of `with` statement.")] | ||
#[diagnostic(severity(warning), help("Do not use the `with` statement."))] | ||
struct NoWithDiagnostic(#[label] pub Span); | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct NoWith; | ||
|
||
declare_oxc_lint!( | ||
/// ### What it does | ||
/// Disallow `with` statements | ||
/// | ||
/// ### Why is this bad? | ||
/// The with statement is potentially problematic because it adds members of an object to the current scope, making it impossible to tell what a variable inside the block actually refers to. | ||
/// | ||
/// ### Example | ||
/// ```javascript | ||
/// with (point) { | ||
/// r = Math.sqrt(x * x + y * y); // is r a member of point? | ||
/// } | ||
/// ``` | ||
NoWith, | ||
correctness | ||
); | ||
|
||
impl Rule for NoWith { | ||
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { | ||
if let AstKind::WithStatement(with_statement) = node.kind() { | ||
ctx.diagnostic(NoWithDiagnostic(Span::new( | ||
with_statement.span.start, | ||
with_statement.span.start + 4, | ||
))); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
use crate::tester::Tester; | ||
|
||
let pass = vec!["foo.bar()"]; | ||
|
||
let fail = vec!["with(foo) { bar() }"]; | ||
|
||
Tester::new(NoWith::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,10 @@ | ||
--- | ||
source: crates/oxc_linter/src/tester.rs | ||
expression: no_with | ||
--- | ||
⚠ eslint(no-with): Unexpected use of `with` statement. | ||
╭─[no_with.tsx:1:1] | ||
1 │ with(foo) { bar() } | ||
· ──── | ||
╰──── | ||
help: Do not use the `with` statement. |