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
use super::Context;
use super::LintRule;
use crate::swc_ecma_ast;
use crate::swc_ecma_ast::CallExpr;
use crate::swc_ecma_ast::Expr;
use crate::swc_ecma_ast::ExprOrSuper;
use swc_ecma_visit::Node;
use swc_ecma_visit::Visit;
pub const BANNED_PROPERTIES: &[&str] =
&["hasOwnProperty", "isPrototypeOf", "propertyIsEnumberable"];
pub struct NoPrototypeBuiltins;
impl LintRule for NoPrototypeBuiltins {
fn new() -> Box<Self> {
Box::new(NoPrototypeBuiltins)
}
fn code(&self) -> &'static str {
"no-prototype-builtins"
}
fn lint_module(&self, context: Context, module: swc_ecma_ast::Module) {
let mut visitor = NoPrototypeBuiltinsVisitor::new(context);
visitor.visit_module(&module, &module);
}
}
struct NoPrototypeBuiltinsVisitor {
context: Context,
}
impl NoPrototypeBuiltinsVisitor {
pub fn new(context: Context) -> Self {
Self { context }
}
}
impl Visit for NoPrototypeBuiltinsVisitor {
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.to_string();
if BANNED_PROPERTIES.contains(&prop_name.as_str()) {
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,
);
}
}