Skip to content

Commit

Permalink
fix(jsx-curly-braces): ignore values that require escaping (#1420)
Browse files Browse the repository at this point in the history
* fix(jsx-curly-braces): ignore values that require escaping

* chore: fix regex

* chore: add more cases

* chore: clippy
  • Loading branch information
marvinhagemeister authored Feb 21, 2025
1 parent 5148cd6 commit 85e100a
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/rules/jsx_curly_braces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ use deno_ast::view::{
NodeTrait,
};
use deno_ast::SourceRanged;
use once_cell::sync::Lazy;
use regex::Regex;

#[derive(Debug)]
pub struct JSXCurlyBraces;

const CODE: &str = "jsx-curly-braces";

static IGNORE_CHARS: Lazy<Regex> = Lazy::new(|| Regex::new(r"[{}<>]").unwrap());

impl LintRule for JSXCurlyBraces {
fn tags(&self) -> Tags {
&[tags::RECOMMENDED, tags::REACT, tags::JSX]
Expand Down Expand Up @@ -81,6 +85,11 @@ impl Handler for JSXCurlyBracesHandler {

if let JSXElementChild::JSXExprContainer(child_expr) = child {
if let JSXExpr::Expr(Expr::Lit(Lit::Str(lit_str))) = child_expr.expr {
// Ignore entities which would require escaping.
if IGNORE_CHARS.is_match(lit_str.inner.value.as_str()) {
continue;
}

// Allowed if this node is at the end of a line
// <div>{" "}
// </div>
Expand Down Expand Up @@ -174,6 +183,14 @@ mod tests {
foo{" "}
<span />
</div>"#,
r#"<div>foo{"<"}</div>"#,
r#"<div>foo{">"}</div>"#,
r#"<div>foo{"}"}</div>"#,
r#"<div>foo{"{"}</div>"#,
r#"<div>foo{"foo <"}</div>"#,
r#"<div>foo{"foo >"}</div>"#,
r#"<div>foo{"foo }"}</div>"#,
r#"<div>foo{"foo {"}</div>"#,
};
}

Expand Down

0 comments on commit 85e100a

Please sign in to comment.