Skip to content

Commit

Permalink
fix: Check for use of __proto__: null in default parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Aug 14, 2024
1 parent 419c197 commit 310105a
Showing 1 changed file with 53 additions and 22 deletions.
75 changes: 53 additions & 22 deletions src/rules/prefer_primordials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ enum PreferPrimordialsMessage {
UnsafeIntrinsic,
#[display(fmt = "Use null [[prototype]] object in the define property")]
DefineProperty,
#[display(fmt = "Use null [[prototype]] object in the default parameter")]
ObjectAssignInDefaultParameter,
#[display(fmt = "Don't use iterator protocol directly")]
Iterator,
#[display(fmt = "Don't use RegExp literal directly")]
Expand Down Expand Up @@ -337,6 +339,28 @@ const GETTER_TARGETS: &[&str] = &[
// "length",
];

fn is_null_proto(object_lit: &ast_view::ObjectLit) -> bool {
for prop_or_spread in object_lit.props {
if_chain! {
if let ast_view::PropOrSpread::Prop(prop) = prop_or_spread;
if let ast_view::Prop::KeyValue(key_value_prop) = prop;
if matches!(key_value_prop.value, ast_view::Expr::Lit(ast_view::Lit::Null(..)));
then {
if let ast_view::PropName::Ident(ident) = key_value_prop.key {
if ident.sym().as_ref() == "__proto__" {
return true
}
} else if let ast_view::PropName::Str(str) = key_value_prop.key {
if str.inner.value.as_ref() == "__proto__" {
return true
}
}
}
}
}
false
}

struct PreferPrimordialsHandler;

impl Handler for PreferPrimordialsHandler {
Expand Down Expand Up @@ -370,28 +394,6 @@ impl Handler for PreferPrimordialsHandler {
scope.var(&ident.inner.to_id()).is_some()
}

fn is_null_proto(object_lit: &ast_view::ObjectLit) -> bool {
for prop_or_spread in object_lit.props {
if_chain! {
if let ast_view::PropOrSpread::Prop(prop) = prop_or_spread;
if let ast_view::Prop::KeyValue(key_value_prop) = prop;
if matches!(key_value_prop.value, ast_view::Expr::Lit(ast_view::Lit::Null(..)));
then {
if let ast_view::PropName::Ident(ident) = key_value_prop.key {
if ident.sym().as_ref() == "__proto__" {
return true
}
} else if let ast_view::PropName::Str(str) = key_value_prop.key {
if str.inner.value.as_ref() == "__proto__" {
return true
}
}
}
}
}
false
}

if inside_var_decl_lhs_or_member_expr_or_prop_or_type_ref(
ident.as_node(),
ident.as_node(),
Expand Down Expand Up @@ -561,6 +563,22 @@ impl Handler for PreferPrimordialsHandler {
}
}

fn param(&mut self, param: &ast_view::Param, ctx: &mut Context) {
if_chain!{
if let ast_view::Pat::Assign(assign_pat) = param.pat;
if let ast_view::Expr::Object(object_lit) = assign_pat.right;
if !is_null_proto(object_lit);
then {
ctx.add_diagnostic_with_hint(
object_lit.range(),
CODE,
PreferPrimordialsMessage::ObjectAssignInDefaultParameter,
PreferPrimordialsHint::NullPrototypeObjectLiteral,
);
}
}
}

fn expr_or_spread(
&mut self,
expr_or_spread: &ast_view::ExprOrSpread,
Expand Down Expand Up @@ -773,6 +791,9 @@ ObjectDefineProperties(o, {
});
"#,
r#"
function foo(o = { __proto__: null }) {}
"#,
r#"
const { NumberParseInt } = primordials;
NumberParseInt("42");
"#,
Expand Down Expand Up @@ -1061,6 +1082,16 @@ ObjectDefineProperties(o, {
},
],
r#"
function foo(o = {}) {}
"#: [
{
line: 2,
col: 17,
message: PreferPrimordialsMessage::ObjectAssignInDefaultParameter,
hint: PreferPrimordialsHint::NullPrototypeObjectLiteral,
}
],
r#"
const { Number } = primordials;
Number.parseInt("10");
"#: [
Expand Down

0 comments on commit 310105a

Please sign in to comment.