fix(linter): no-extra-boolean-cast false positive

This commit is contained in:
Boshen 2023-08-20 16:17:44 +08:00
parent e44b6b3eee
commit 77bc9135f0
No known key found for this signature in database
GPG key ID: 9C7A8C8AB22BEBD1

View file

@ -66,33 +66,21 @@ impl Rule for NoExtraBooleanCast {
}
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::CallExpression(expr) = node.kind() {
if expr
.callee
.without_parenthesized()
.get_identifier_reference()
.is_some_and(|ident| ident.name != "Boolean")
match node.kind() {
AstKind::CallExpression(expr)
if expr.callee.is_specific_id("Boolean")
&& is_flagged_ctx(node, ctx, self.enforce_for_logical_operands) =>
{
return;
}
if is_flagged_ctx(node, ctx, self.enforce_for_logical_operands) {
ctx.diagnostic(NoExtraBooleanCastDiagnostic(expr.span));
}
}
let Some(parent) = get_real_parent(node, ctx) else { return };
if let (AstKind::UnaryExpression(expr), AstKind::UnaryExpression(parent_expr)) =
(node.kind(), parent.kind())
{
match (expr.operator, parent_expr.operator) {
(UnaryOperator::LogicalNot, UnaryOperator::LogicalNot)
if is_flagged_ctx(parent, ctx, self.enforce_for_logical_operands) =>
AstKind::UnaryExpression(unary) if unary.operator == UnaryOperator::LogicalNot => {
let Some(parent) = get_real_parent(node, ctx) else { return };
if matches!(parent.kind(), AstKind::UnaryExpression(p) if p.operator == UnaryOperator::LogicalNot && is_flagged_ctx(parent, ctx, self.enforce_for_logical_operands))
{
ctx.diagnostic(NoExtraDoubleNegationCastDiagnostic(parent_expr.span));
ctx.diagnostic(NoExtraDoubleNegationCastDiagnostic(unary.span));
}
_ => (),
}
_ => {}
}
}
}
@ -299,6 +287,8 @@ fn test() {
Some(serde_json::json!([{ "enforceForLogicalOperands": true }])),
),
("if (!!foo ?? bar) {}", Some(serde_json::json!([{ "enforceForLogicalOperands": true }]))),
("if (x.y()) {}", Some(serde_json::json!([{ "enforceForLogicalOperands": true }]))),
("if (x.y()) {}", Some(serde_json::json!([{ "enforceForLogicalOperands": false }]))),
];
let fail = vec![