1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2020 the Deno authors. All rights reserved. MIT license.
use super::Context;
use super::LintRule;
use crate::swc_ecma_ast;
use crate::swc_ecma_ast::WithStmt;
use swc_ecma_visit::Node;
use swc_ecma_visit::Visit;

pub struct NoWith;

impl LintRule for NoWith {
  fn new() -> Box<Self> {
    Box::new(NoWith)
  }

  fn code(&self) -> &'static str {
    "no-with"
  }

  fn lint_module(&self, context: Context, module: swc_ecma_ast::Module) {
    let mut visitor = NoWithVisitor::new(context);
    visitor.visit_module(&module, &module);
  }
}

struct NoWithVisitor {
  context: Context,
}

impl NoWithVisitor {
  pub fn new(context: Context) -> Self {
    Self { context }
  }
}

impl Visit for NoWithVisitor {
  fn visit_with_stmt(&mut self, with_stmt: &WithStmt, _parent: &dyn Node) {
    self.context.add_diagnostic(
      with_stmt.span,
      "no-with",
      "`with` statement is not allowed",
    );
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::test_util::*;

  #[test]
  fn no_with() {
    assert_lint_err::<NoWith>("with (someVar) { console.log('asdf'); }", 0)
  }
}