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
use super::Context;
use super::LintRule;
use swc_ecmascript::ast::Expr;
use swc_ecmascript::ast::UnaryExpr;
use swc_ecmascript::ast::UnaryOp;
use swc_ecmascript::visit::noop_visit_type;
use swc_ecmascript::visit::Node;
use swc_ecmascript::visit::Visit;
use std::sync::Arc;
pub struct NoDeleteVar;
impl LintRule for NoDeleteVar {
fn new() -> Box<Self> {
Box::new(NoDeleteVar)
}
fn code(&self) -> &'static str {
"no-delete-var"
}
fn lint_module(
&self,
context: Arc<Context>,
module: &swc_ecmascript::ast::Module,
) {
let mut visitor = NoDeleteVarVisitor::new(context);
visitor.visit_module(module, module);
}
}
struct NoDeleteVarVisitor {
context: Arc<Context>,
}
impl NoDeleteVarVisitor {
pub fn new(context: Arc<Context>) -> Self {
Self { context }
}
}
impl Visit for NoDeleteVarVisitor {
noop_visit_type!();
fn visit_unary_expr(&mut self, unary_expr: &UnaryExpr, _parent: &dyn Node) {
if unary_expr.op != UnaryOp::Delete {
return;
}
if let Expr::Ident(_) = *unary_expr.arg {
self.context.add_diagnostic(
unary_expr.span,
"no-delete-var",
"Variables shouldn't be deleted",
);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::*;
#[test]
fn no_delete_var_test() {
assert_lint_err::<NoDeleteVar>(
r#"var someVar = "someVar"; delete someVar;"#,
25,
);
}
}