fix(linter): fix panic in no_unescaped_entities (#1103)

This commit is contained in:
Boshen 2023-10-30 15:52:32 +08:00 committed by GitHub
parent a455c81db6
commit b4739e5540
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View file

@ -50,8 +50,7 @@ impl Rule for NoUnescapedEntities {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::JSXText(jsx_text) = node.kind() {
let source = jsx_text.span.source_text(ctx.source_text());
for (i, char) in source.chars().enumerate() {
for (i, char) in source.char_indices() {
if let Some(escapes) = DEFAULTS.get(&char) {
#[allow(clippy::cast_possible_truncation)]
ctx.diagnostic(NoUnescapedEntitiesDiagnostic(
@ -181,6 +180,7 @@ fn test() {
// });
// ",
r#"<script>window.foo = "bar"</script>"#,
r#"<script>测试 " 测试</script>"#,
];
Tester::new_without_config(NoUnescapedEntities::NAME, pass, fail).test_and_snapshot();

View file

@ -46,4 +46,10 @@ expression: no_unescaped_entities
· ─
╰────
⚠ eslint-plugin-react(no-unescaped-entities): `"` can be escaped with &quot; or &ldquo; or &#34; or &rdquo;
╭─[no_unescaped_entities.tsx:1:1]
1 │ <script>测试 " 测试</script>
· ─
╰────