-
Notifications
You must be signed in to change notification settings - Fork 162
/
no_obj_calls.rs
130 lines (115 loc) · 2.91 KB
/
no_obj_calls.rs
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::Program;
use deno_ast::view::{CallExpr, Callee, Expr, Ident, NewExpr};
use deno_ast::{SourceRange, SourceRanged};
#[derive(Debug)]
pub struct NoObjCalls;
const CODE: &str = "no-obj-calls";
fn get_message(callee_name: &str) -> String {
format!("`{}` call as function is not allowed", callee_name)
}
impl LintRule for NoObjCalls {
fn tags(&self) -> &'static [&'static str] {
&["recommended"]
}
fn code(&self) -> &'static str {
CODE
}
fn lint_program_with_ast_view(
&self,
context: &mut Context,
program: Program,
) {
NoObjCallsHandler.traverse(program, context);
}
#[cfg(feature = "docs")]
fn docs(&self) -> &'static str {
include_str!("../../docs/rules/no_obj_calls.md")
}
}
struct NoObjCallsHandler;
fn check_callee(callee: &Ident, range: SourceRange, ctx: &mut Context) {
if matches!(
callee.sym().as_ref(),
"Math" | "JSON" | "Reflect" | "Atomics"
) && ctx.scope().var(&callee.to_id()).is_none()
{
ctx.add_diagnostic(
range,
"no-obj-calls",
get_message(callee.sym().as_ref()),
);
}
}
impl Handler for NoObjCallsHandler {
fn call_expr(&mut self, call_expr: &CallExpr, ctx: &mut Context) {
if let Callee::Expr(Expr::Ident(ident)) = call_expr.callee {
check_callee(ident, call_expr.range(), ctx);
}
}
fn new_expr(&mut self, new_expr: &NewExpr, ctx: &mut Context) {
if let Expr::Ident(ident) = new_expr.callee {
check_callee(ident, new_expr.range(), ctx);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_obj_calls_valid() {
assert_lint_ok! {
NoObjCalls,
"Math.PI * 2 * 3;",
r#"JSON.parse("{}");"#,
r#"Reflect.get({ x: 1, y: 2 }, "x");"#,
"Atomics.load(foo, 0);",
r#"
function f(Math: () => void) {
Math();
}
"#,
r#"
function f(JSON: () => void) {
JSON();
}
"#,
r#"
function f(Reflect: () => void) {
Reflect();
}
"#,
r#"
function f(Atomics: () => void) {
Atomics();
}
"#,
};
}
#[test]
fn no_obj_calls_invalid() {
assert_lint_err! {
NoObjCalls,
"Math();": [{col: 0, message: get_message("Math")}],
"new Math();": [{col: 0, message: get_message("Math")}],
"JSON();": [{col: 0, message: get_message("JSON")}],
"new JSON();": [{col: 0, message: get_message("JSON")}],
"Reflect();": [{col: 0, message: get_message("Reflect")}],
"new Reflect();": [{col: 0, message: get_message("Reflect")}],
"Atomics();": [{col: 0, message: get_message("Atomics")}],
"new Atomics();": [{col: 0, message: get_message("Atomics")}],
r#"
function f(Math: () => void) { Math(); }
const m = Math();
"#: [
{
col: 10,
line: 3,
message: get_message("Math"),
},
],
}
}
}