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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::swc_util::StringRepr;
use crate::Program;
use deno_ast::view::{CallExpr, Callee, Expr, ParenExpr, VarDeclarator};
use deno_ast::{SourceRange, SourceRanged};
#[derive(Debug)]
pub struct NoEval;
const CODE: &str = "no-eval";
const MESSAGE: &str = "`eval` call is not allowed";
const HINT: &str = "Remove the use of `eval`";
impl LintRule for NoEval {
fn code(&self) -> &'static str {
CODE
}
fn lint_program_with_ast_view(
&self,
context: &mut Context,
program: Program,
) {
NoEvalHandler.traverse(program, context);
}
}
struct NoEvalHandler;
impl NoEvalHandler {
fn maybe_add_diagnostic(
&mut self,
source: &dyn StringRepr,
range: SourceRange,
ctx: &mut Context,
) {
if source.string_repr().as_deref() == Some("eval") {
self.add_diagnostic(range, ctx);
}
}
fn add_diagnostic(&mut self, range: SourceRange, ctx: &mut Context) {
ctx.add_diagnostic_with_hint(range, CODE, MESSAGE, HINT);
}
fn handle_paren_callee(&mut self, p: &ParenExpr, ctx: &mut Context) {
match p.expr {
// Nested paren callee ((eval))('var foo = 0;')
Expr::Paren(paren) => self.handle_paren_callee(paren, ctx),
// Single argument callee: (eval)('var foo = 0;')
Expr::Ident(ident) => {
self.maybe_add_diagnostic(ident, ident.range(), ctx)
}
// Multiple arguments callee: (0, eval)('var foo = 0;')
Expr::Seq(seq) => {
for expr in seq.exprs {
if let Expr::Ident(ident) = expr {
self.maybe_add_diagnostic(*ident, ident.range(), ctx)
}
}
}
_ => {}
}
}
}
impl Handler for NoEvalHandler {
fn var_declarator(&mut self, v: &VarDeclarator, ctx: &mut Context) {
if let Some(Expr::Ident(ident)) = &v.init {
self.maybe_add_diagnostic(*ident, v.range(), ctx);
}
}
fn call_expr(&mut self, call_expr: &CallExpr, ctx: &mut Context) {
if let Callee::Expr(expr) = &call_expr.callee {
match expr {
Expr::Ident(ident) => {
self.maybe_add_diagnostic(*ident, call_expr.range(), ctx)
}
Expr::Paren(paren) => self.handle_paren_callee(paren, ctx),
_ => {}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_eval_valid() {
assert_lint_ok! {
NoEval,
"foo.eval('bar');",
}
}
#[test]
fn no_eval_invalid() {
assert_lint_err! {
NoEval,
"eval('123');": [{col: 0, message: MESSAGE, hint: HINT}],
"(0, eval)('var a = 0');": [{col: 4, message: MESSAGE, hint: HINT}],
"((eval))('var a = 0');": [{col: 2, message: MESSAGE, hint: HINT}],
"var foo = eval;": [{col: 4, message: MESSAGE, hint: HINT}],
// TODO (see: https://github.com/denoland/deno_lint/pull/490)
// "this.eval("123");": [{col: 0, message: MESSAGE, hint: HINT}],
// "var foo = this.eval;": [{col: 0, message: MESSAGE, hint: HINT}],
// "(function(exe){ exe('foo') })(eval);": [{col: 0, message: MESSAGE, hint: HINT}],
//
// "(0, window.eval)('foo');": [{col: 0, message: MESSAGE, hint: HINT}],
// "(0, window['eval'])('foo');": [{col: 0, message: MESSAGE, hint: HINT}],
// "var foo = window.eval;": [{col: 0, message: MESSAGE, hint: HINT}],
// "window.eval('foo');": [{col: 0, message: MESSAGE, hint: HINT}],
// "window.window.eval('foo');": [{col: 0, message: MESSAGE, hint: HINT}],
// "window.window['eval']('foo');": [{col: 0, message: MESSAGE, hint: HINT}],
//
// "var foo = globalThis.eval;": [{col: 0, message: MESSAGE, hint: HINT}],
// "globalThis.eval('foo')": [{col: 0, message: MESSAGE, hint: HINT}],
// "globalThis.globalThis.eval('foo')": [{col: 0, message: MESSAGE, hint: HINT}],
// "globalThis.globalThis['eval']('foo')": [{col: 0, message: MESSAGE, hint: HINT}],
// "(0, globalThis.eval)('foo')": [{col: 0, message: MESSAGE, hint: HINT}],
// "(0, globalThis['eval'])('foo')": [{col: 0, message: MESSAGE, hint: HINT}],
}
}
}