Skip to content

Commit 42dcfd2

Browse files
authored
feat(linter/react/no-unescaped-entities): implement suggestion (#27292)
this PR implements suggestion for `react/no-unescaped-entities`, issue #1022
1 parent 176b4b9 commit 42dcfd2

2 files changed

Lines changed: 107 additions & 9 deletions

File tree

‎crates/oxc_linter/src/rules/react/no_unescaped_entities.rs‎

Lines changed: 90 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ use oxc_span::Span;
66
use crate::{
77
AstNode,
88
context::{ContextHost, LintContext},
9+
fixer::RuleFixer,
910
rule::Rule,
1011
};
1112

1213
static ESCAPED_DOUBLE_QUOTE: &str = "" or “ or " or ”";
1314
static ESCAPED_SINGLE_QUOTE: &str = "' or ‘ or ' or ’";
1415

16+
const DOUBLE_QUOTE_ALTERNATIVES: [&str; 4] = [""", "“", """, "”"];
17+
const SINGLE_QUOTE_ALTERNATIVES: [&str; 4] = ["'", "‘", "'", "’"];
18+
1519
fn no_unescaped_entities_diagnostic(span: Span, unescaped: char) -> OxcDiagnostic {
1620
let escaped = if unescaped == '"' { ESCAPED_DOUBLE_QUOTE } else { ESCAPED_SINGLE_QUOTE };
1721
OxcDiagnostic::warn(format!("`{unescaped}` can be escaped with {escaped}")).with_label(span)
@@ -48,7 +52,7 @@ declare_oxc_lint!(
4852
NoUnescapedEntities,
4953
react,
5054
pedantic,
51-
pending,
55+
suggestion,
5256
version = "0.0.15",
5357
short_description = "This rule prevents characters that you may have meant as JSX escape characters from being accidentally injected as a text node in JSX statements.",
5458
);
@@ -58,14 +62,20 @@ impl Rule for NoUnescapedEntities {
5862
if let AstKind::JSXText(jsx_text) = node.kind() {
5963
let source = jsx_text.raw.unwrap().as_str();
6064
for (i, &byte) in source.as_bytes().iter().enumerate() {
61-
if matches!(byte, b'\'' | b'\"') {
62-
#[expect(clippy::cast_possible_truncation)]
63-
let start = jsx_text.span.start + i as u32;
64-
ctx.diagnostic(no_unescaped_entities_diagnostic(
65-
Span::sized(start, 1),
66-
byte as char,
67-
));
68-
}
65+
let alternatives = match byte {
66+
b'"' => DOUBLE_QUOTE_ALTERNATIVES,
67+
b'\'' => SINGLE_QUOTE_ALTERNATIVES,
68+
_ => continue,
69+
};
70+
#[expect(clippy::cast_possible_truncation)]
71+
let span = Span::sized(jsx_text.span.start + i as u32, 1);
72+
let fixer = RuleFixer::new(FixKind::Suggestion, ctx);
73+
ctx.diagnostic_with_suggestions(
74+
no_unescaped_entities_diagnostic(span, byte as char),
75+
alternatives.map(|alt| {
76+
fixer.replace(span, alt).with_message(format!("Replace with `{alt}`"))
77+
}),
78+
);
6979
}
7080
}
7181
}
@@ -184,6 +194,77 @@ fn test() {
184194
r#"<script>测试 " 测试</script>"#,
185195
];
186196

197+
let fix = vec![
198+
(
199+
"
200+
var Hello = createReactClass({
201+
render: function() {
202+
return <div>'</div>;
203+
}
204+
});
205+
",
206+
(
207+
"
208+
var Hello = createReactClass({
209+
render: function() {
210+
return <div>&apos;</div>;
211+
}
212+
});
213+
",
214+
"
215+
var Hello = createReactClass({
216+
render: function() {
217+
return <div>&lsquo;</div>;
218+
}
219+
});
220+
",
221+
"
222+
var Hello = createReactClass({
223+
render: function() {
224+
return <div>&#39;</div>;
225+
}
226+
});
227+
",
228+
"
229+
var Hello = createReactClass({
230+
render: function() {
231+
return <div>&rsquo;</div>;
232+
}
233+
});
234+
",
235+
),
236+
),
237+
(
238+
r#"
239+
<script>window.foo = "bar"</script>
240+
"#,
241+
(
242+
"
243+
<script>window.foo = &quot;bar&quot;</script>
244+
",
245+
"
246+
<script>window.foo = &ldquo;bar&ldquo;</script>
247+
",
248+
"
249+
<script>window.foo = &#34;bar&#34;</script>
250+
",
251+
"
252+
<script>window.foo = &rdquo;bar&rdquo;</script>
253+
",
254+
),
255+
),
256+
(
257+
r#"<script>测试 " 测试</script>"#,
258+
(
259+
"<script>测试 &quot; 测试</script>",
260+
"<script>测试 &ldquo; 测试</script>",
261+
"<script>测试 &#34; 测试</script>",
262+
"<script>测试 &rdquo; 测试</script>",
263+
),
264+
),
265+
];
266+
187267
Tester::new(NoUnescapedEntities::NAME, NoUnescapedEntities::PLUGIN, pass, fail)
268+
.expect_fix(fix)
188269
.test_and_snapshot();
189270
}

‎crates/oxc_linter/src/tester.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,23 @@ impl<S: Into<String>> From<(S, (S, S, S))> for ExpectFixTestCase {
213213
}
214214
}
215215

216+
impl<S: Into<String>> From<(S, (S, S, S, S))> for ExpectFixTestCase {
217+
fn from(value: (S, (S, S, S, S))) -> Self {
218+
Self {
219+
source: value.0.into(),
220+
expected: vec![
221+
ExpectFix { expected: value.1.0.into(), kind: ExpectFixKind::Any },
222+
ExpectFix { expected: value.1.1.into(), kind: ExpectFixKind::Any },
223+
ExpectFix { expected: value.1.2.into(), kind: ExpectFixKind::Any },
224+
ExpectFix { expected: value.1.3.into(), kind: ExpectFixKind::Any },
225+
],
226+
rule_config: None,
227+
path: None,
228+
eslint_config: None,
229+
}
230+
}
231+
}
232+
216233
impl<S, F> From<(S, S, Option<Value>, F)> for ExpectFixTestCase
217234
where
218235
S: Into<String>,

0 commit comments

Comments
 (0)