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
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2020 the Deno authors. All rights reserved. MIT license.
use super::Context;
use super::LintRule;
use swc_common::Span;
use swc_ecmascript::ast::CallExpr;
use swc_ecmascript::ast::Expr;
use swc_ecmascript::ast::ExprOrSuper;
use swc_ecmascript::ast::NewExpr;
use swc_ecmascript::visit::noop_visit_type;
use swc_ecmascript::visit::Node;
use swc_ecmascript::visit::Visit;

pub struct NoObjCalls;

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

  fn tags(&self) -> &[&'static str] {
    &["recommended"]
  }

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

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

struct NoObjCallsVisitor<'c> {
  context: &'c mut Context,
}

impl<'c> NoObjCallsVisitor<'c> {
  fn new(context: &'c mut Context) -> Self {
    Self { context }
  }

  fn check_callee(&mut self, callee_name: impl AsRef<str>, span: Span) {
    let callee_name = callee_name.as_ref();
    match callee_name {
      "Math" | "JSON" | "Reflect" | "Atomics" => {
        self.context.add_diagnostic(
          span,
          "no-obj-calls",
          format!("`{}` call as function is not allowed", callee_name),
        );
      }
      _ => {}
    }
  }
}

impl<'c> Visit for NoObjCallsVisitor<'c> {
  noop_visit_type!();

  fn visit_call_expr(&mut self, call_expr: &CallExpr, _parent: &dyn Node) {
    if let ExprOrSuper::Expr(expr) = &call_expr.callee {
      if let Expr::Ident(ident) = expr.as_ref() {
        self.check_callee(&ident.sym, call_expr.span);
      }
    }
  }

  fn visit_new_expr(&mut self, new_expr: &NewExpr, _parent: &dyn Node) {
    if let Expr::Ident(ident) = &*new_expr.callee {
      self.check_callee(&ident.sym, new_expr.span);
    }
  }
}

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

  #[test]
  fn test_no_call_math() {
    assert_lint_err::<NoObjCalls>(r#"Math();"#, 0)
  }

  #[test]
  fn test_no_new_math() {
    assert_lint_err::<NoObjCalls>(r#"new Math();"#, 0)
  }

  #[test]
  fn test_no_call_json() {
    assert_lint_err::<NoObjCalls>(r#"JSON();"#, 0)
  }

  #[test]
  fn test_no_new_json() {
    assert_lint_err::<NoObjCalls>(r#"new JSON();"#, 0)
  }

  #[test]
  fn test_no_call_reflect() {
    assert_lint_err::<NoObjCalls>(r#"Reflect();"#, 0)
  }

  #[test]
  fn test_no_new_reflect() {
    assert_lint_err::<NoObjCalls>(r#"new Reflect();"#, 0)
  }

  #[test]
  fn test_no_call_atomicst() {
    assert_lint_err::<NoObjCalls>(r#"Atomics();"#, 0)
  }

  #[test]
  fn test_no_new_atomics() {
    assert_lint_err::<NoObjCalls>(r#"new Atomics();"#, 0)
  }

  #[test]
  fn test_math_func_ok() {
    assert_lint_ok::<NoObjCalls>("Math.PI * 2 * 3;");
  }

  #[test]
  fn test_new_json_ok() {
    assert_lint_ok::<NoObjCalls>("JSON.parse(\"{}\");");
  }

  #[test]
  fn test_reflect_get_ok() {
    assert_lint_ok::<NoObjCalls>("Reflect.get({ x: 1, y: 2 }, \"x\");");
  }

  #[test]
  fn test_atomic_load_ok() {
    assert_lint_ok::<NoObjCalls>("Atomics.load(foo, 0);");
  }
}