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
use super::Context;
use super::LintRule;
use swc_ecmascript::ast::CallExpr;
use swc_ecmascript::ast::Expr;
use swc_ecmascript::ast::ExprOrSuper;
use swc_ecmascript::visit::noop_visit_type;
use swc_ecmascript::visit::Node;
use swc_ecmascript::visit::Visit;
const BANNED_PROPERTIES: &[&str] =
&["hasOwnProperty", "isPrototypeOf", "propertyIsEnumberable"];
pub struct NoPrototypeBuiltins;
impl LintRule for NoPrototypeBuiltins {
fn new() -> Box<Self> {
Box::new(NoPrototypeBuiltins)
}
fn tags(&self) -> &[&'static str] {
&["recommended"]
}
fn code(&self) -> &'static str {
"no-prototype-builtins"
}
fn lint_module(
&self,
context: &mut Context,
module: &swc_ecmascript::ast::Module,
) {
let mut visitor = NoPrototypeBuiltinsVisitor::new(context);
visitor.visit_module(module, module);
}
}
struct NoPrototypeBuiltinsVisitor<'c> {
context: &'c mut Context,
}
impl<'c> NoPrototypeBuiltinsVisitor<'c> {
fn new(context: &'c mut Context) -> Self {
Self { context }
}
}
impl<'c> Visit for NoPrototypeBuiltinsVisitor<'c> {
noop_visit_type!();
fn visit_call_expr(&mut self, call_expr: &CallExpr, _parent: &dyn Node) {
let member_expr = match &call_expr.callee {
ExprOrSuper::Expr(boxed_expr) => match &**boxed_expr {
Expr::Member(member_expr) => {
if member_expr.computed {
return;
}
member_expr
}
_ => return,
},
ExprOrSuper::Super(_) => return,
};
if let Expr::Ident(ident) = &*member_expr.prop {
let prop_name = ident.sym.as_ref();
if BANNED_PROPERTIES.contains(&prop_name) {
self.context.add_diagnostic(
call_expr.span,
"no-prototype-builtins",
format!(
"Access to Object.prototype.{} is not allowed from target object",
prop_name
),
);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::*;
#[test]
fn no_prototype_builtins_ok() {
assert_lint_ok::<NoPrototypeBuiltins>(
r#"
Object.prototype.hasOwnProperty.call(foo, "bar");
Object.prototype.isPrototypeOf.call(foo, "bar");
Object.prototype.propertyIsEnumberable.call(foo, "bar");
Object.prototype.hasOwnProperty.apply(foo, ["bar"]);
Object.prototype.isPrototypeOf.apply(foo, ["bar"]);
Object.prototype.propertyIsEnumberable.apply(foo, ["bar"]);
hasOwnProperty(foo, "bar");
isPrototypeOf(foo, "bar");
propertyIsEnumberable(foo, "bar");
({}.hasOwnProperty.call(foo, "bar"));
({}.isPrototypeOf.call(foo, "bar"));
({}.propertyIsEnumberable.call(foo, "bar"));
({}.hasOwnProperty.apply(foo, ["bar"]));
({}.isPrototypeOf.apply(foo, ["bar"]));
({}.propertyIsEnumberable.apply(foo, ["bar"]));
"#,
);
}
#[test]
fn no_prototype_builtins() {
assert_lint_err::<NoPrototypeBuiltins>(r#"foo.hasOwnProperty("bar");"#, 0);
assert_lint_err::<NoPrototypeBuiltins>(r#"foo.isPrototypeOf("bar");"#, 0);
assert_lint_err::<NoPrototypeBuiltins>(
r#"foo.propertyIsEnumberable("bar");"#,
0,
);
assert_lint_err::<NoPrototypeBuiltins>(
r#"foo.bar.baz.hasOwnProperty("bar");"#,
0,
);
}
}