Skip to content

Commit 5148cd6

Browse files
authored
fix(jsx-boolean-value): add code fix (#1419)
fix: add code fix for jsx-boolean-value
1 parent 7e361b3 commit 5148cd6

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

src/rules/jsx_boolean_value.rs

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22

33
use super::{Context, LintRule};
4+
use crate::diagnostic::{LintFix, LintFixChange};
45
use crate::handler::{Handler, Traverse};
56
use crate::tags::Tags;
67
use crate::{tags, Program};
7-
use deno_ast::view::{Expr, JSXAttr, JSXAttrValue, JSXExpr, Lit};
8-
use deno_ast::SourceRanged;
8+
use deno_ast::swc::parser::token::Token;
9+
use deno_ast::view::{AssignOp, Expr, JSXAttr, JSXAttrValue, JSXExpr, Lit};
10+
use deno_ast::{SourceRange, SourceRanged, SourceRangedForSpanned};
911

1012
#[derive(Debug)]
1113
pub struct JSXBooleanValue;
@@ -33,6 +35,7 @@ impl LintRule for JSXBooleanValue {
3335
const MESSAGE: &str =
3436
"Passing 'true' to boolean attributes is the same as not passing it`";
3537
const HINT: &str = "Remove the attribute value";
38+
const FIX_DESC: &str = HINT;
3639

3740
struct JSXBooleanValueHandler;
3841

@@ -41,8 +44,33 @@ impl Handler for JSXBooleanValueHandler {
4144
if let Some(value) = node.value {
4245
if let JSXAttrValue::JSXExprContainer(expr) = value {
4346
if let JSXExpr::Expr(Expr::Lit(Lit::Bool(lit_bool))) = expr.expr {
44-
if lit_bool.value() {
45-
ctx.add_diagnostic_with_hint(value.range(), CODE, MESSAGE, HINT);
47+
if lit_bool.value()
48+
&& lit_bool.leading_comments_fast(ctx.program()).is_empty()
49+
&& lit_bool.trailing_comments_fast(ctx.program()).is_empty()
50+
{
51+
let mut fixes = Vec::with_capacity(1);
52+
if let Some(token) = expr.previous_token_fast(ctx.program()) {
53+
if token.token == Token::AssignOp(AssignOp::Assign) {
54+
let start_pos = token
55+
.previous_token_fast(ctx.program())
56+
.map(|t| t.end())
57+
.unwrap_or(token.start());
58+
fixes.push(LintFix {
59+
description: FIX_DESC.into(),
60+
changes: vec![LintFixChange {
61+
new_text: "".into(),
62+
range: SourceRange::new(start_pos, expr.end()),
63+
}],
64+
});
65+
}
66+
}
67+
ctx.add_diagnostic_with_fixes(
68+
value.range(),
69+
CODE,
70+
MESSAGE,
71+
Some(HINT.into()),
72+
fixes,
73+
);
4674
}
4775
}
4876
}
@@ -63,6 +91,8 @@ mod tests {
6391
filename: "file:///foo.jsx",
6492
// non derived classes.
6593
"<Foo foo={false} />",
94+
"<Foo foo={/* some comment */ true} />",
95+
"<Foo foo={true /* some comment */} />",
6696
"<Foo foo />",
6797
};
6898
}
@@ -77,6 +107,20 @@ mod tests {
77107
col: 9,
78108
message: MESSAGE,
79109
hint: HINT,
110+
fix: (FIX_DESC, "<Foo foo />"),
111+
}
112+
],
113+
};
114+
115+
assert_lint_err! {
116+
JSXBooleanValue,
117+
filename: "file:///foo.jsx",
118+
"<Foo foo = { true } />": [
119+
{
120+
col: 11,
121+
message: MESSAGE,
122+
hint: HINT,
123+
fix: (FIX_DESC, "<Foo foo />"),
80124
}
81125
],
82126
};

0 commit comments

Comments
 (0)