mirror of
https://github.com/danbulant/oxc
synced 2026-05-22 05:38:54 +00:00
feat(linter): eslint/no-with (#2741)
Relates to #479 Rule detail: https://eslint.org/docs/latest/rules/no-with
This commit is contained in:
parent
81752b2790
commit
91e8a71214
3 changed files with 68 additions and 0 deletions
|
|
@ -103,6 +103,7 @@ mod eslint {
|
||||||
pub mod no_useless_rename;
|
pub mod no_useless_rename;
|
||||||
pub mod no_var;
|
pub mod no_var;
|
||||||
pub mod no_void;
|
pub mod no_void;
|
||||||
|
pub mod no_with;
|
||||||
pub mod require_yield;
|
pub mod require_yield;
|
||||||
pub mod use_isnan;
|
pub mod use_isnan;
|
||||||
pub mod valid_typeof;
|
pub mod valid_typeof;
|
||||||
|
|
@ -417,6 +418,7 @@ oxc_macros::declare_all_lint_rules! {
|
||||||
eslint::no_useless_rename,
|
eslint::no_useless_rename,
|
||||||
eslint::no_var,
|
eslint::no_var,
|
||||||
eslint::no_void,
|
eslint::no_void,
|
||||||
|
eslint::no_with,
|
||||||
eslint::require_yield,
|
eslint::require_yield,
|
||||||
eslint::use_isnan,
|
eslint::use_isnan,
|
||||||
eslint::valid_typeof,
|
eslint::valid_typeof,
|
||||||
|
|
|
||||||
56
crates/oxc_linter/src/rules/eslint/no_with.rs
Normal file
56
crates/oxc_linter/src/rules/eslint/no_with.rs
Normal file
|
|
@ -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();
|
||||||
|
}
|
||||||
10
crates/oxc_linter/src/snapshots/no_with.snap
Normal file
10
crates/oxc_linter/src/snapshots/no_with.snap
Normal file
|
|
@ -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.
|
||||||
Loading…
Reference in a new issue