From 3d2bcf106bb7f8b530b7bdd03b61ebd254c5333b Mon Sep 17 00:00:00 2001 From: ud2 Date: Wed, 14 Aug 2024 05:13:32 +0800 Subject: [PATCH] fix(prefer-primordials): allow private identifier in `in` expression --- src/rules/prefer_primordials.rs | 48 +++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/src/rules/prefer_primordials.rs b/src/rules/prefer_primordials.rs index d9b4e2049..adba7efad 100644 --- a/src/rules/prefer_primordials.rs +++ b/src/rules/prefer_primordials.rs @@ -708,20 +708,24 @@ impl Handler for PreferPrimordialsHandler { fn bin_expr(&mut self, bin_expr: &ast_view::BinExpr, ctx: &mut Context) { use ast_view::BinaryOp; - if matches!(bin_expr.op(), BinaryOp::InstanceOf) { - ctx.add_diagnostic_with_hint( - bin_expr.range(), - CODE, - PreferPrimordialsMessage::InstanceOf, - PreferPrimordialsHint::InstanceOf, - ); - } else if matches!(bin_expr.op(), BinaryOp::In) { - ctx.add_diagnostic_with_hint( - bin_expr.range(), - CODE, - PreferPrimordialsMessage::In, - PreferPrimordialsHint::In, - ); + match bin_expr.op() { + BinaryOp::InstanceOf => { + ctx.add_diagnostic_with_hint( + bin_expr.range(), + CODE, + PreferPrimordialsMessage::InstanceOf, + PreferPrimordialsHint::InstanceOf, + ); + } + BinaryOp::In if !bin_expr.left.is::() => { + ctx.add_diagnostic_with_hint( + bin_expr.range(), + CODE, + PreferPrimordialsMessage::In, + PreferPrimordialsHint::In, + ); + } + _ => {} } } } @@ -908,6 +912,15 @@ function foo(): Array {} r#" type p = Promise; "#, + r#" +class A { + #brand; + + static is(obj) { + return #brand in obj; + } +} + "#, }; } @@ -1289,6 +1302,13 @@ new DataView(new ArrayBuffer(10)).byteOffset; hint: PreferPrimordialsHint::In, }, ], + r#"a in A"#: [ + { + col: 0, + message: PreferPrimordialsMessage::In, + hint: PreferPrimordialsHint::In, + }, + ], r#"a instanceof A"#: [ { col: 0,