-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add jsx-no-unescaped-entities rule
- Loading branch information
1 parent
26e9df1
commit a2a2bea
Showing
5 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Using unescaped entities is often a coding mistake where the developer wanted to | ||
pass a JSX element instead. This rule ensures an explicit text form must be | ||
used. | ||
|
||
### Invalid: | ||
|
||
```tsx | ||
<div>></div>; | ||
``` | ||
|
||
### Valid: | ||
|
||
```tsx | ||
<div>></div> | ||
<div>{">"}</div> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
use super::{Context, LintRule}; | ||
use crate::diagnostic::{LintFix, LintFixChange}; | ||
use crate::handler::{Handler, Traverse}; | ||
use crate::tags::{self, Tags}; | ||
use crate::Program; | ||
use deno_ast::view::{JSXElement, JSXElementChild}; | ||
use deno_ast::SourceRanged; | ||
|
||
#[derive(Debug)] | ||
pub struct JSXNoUnescapedEntities; | ||
|
||
const CODE: &str = "jsx-no-unescaped-entities"; | ||
|
||
impl LintRule for JSXNoUnescapedEntities { | ||
fn tags(&self) -> Tags { | ||
&[tags::RECOMMENDED, tags::REACT, tags::JSX, tags::FRESH] | ||
} | ||
|
||
fn code(&self) -> &'static str { | ||
CODE | ||
} | ||
|
||
fn lint_program_with_ast_view( | ||
&self, | ||
context: &mut Context, | ||
program: Program, | ||
) { | ||
JSXNoUnescapedEntitiesHandler.traverse(program, context); | ||
} | ||
|
||
#[cfg(feature = "docs")] | ||
fn docs(&self) -> &'static str { | ||
include_str!("../../docs/rules/jsx_no_unescaped_entities.md") | ||
} | ||
} | ||
|
||
const MESSAGE: &str = "Found one or more unescaped entities in JSX text"; | ||
const HINT: &str = "Escape the '\">} characters respectively"; | ||
|
||
struct JSXNoUnescapedEntitiesHandler; | ||
|
||
impl Handler for JSXNoUnescapedEntitiesHandler { | ||
fn jsx_element(&mut self, node: &JSXElement, ctx: &mut Context) { | ||
for child in node.children { | ||
if let JSXElementChild::JSXText(jsx_text) = child { | ||
let text = jsx_text.raw().as_str(); | ||
let new_text = text | ||
.replace('>', ">") | ||
.replace('"', """) | ||
.replace('\'', "'") | ||
.replace('}', "}"); | ||
|
||
if text != new_text { | ||
ctx.add_diagnostic_with_fixes( | ||
node.range(), | ||
CODE, | ||
MESSAGE, | ||
Some(HINT.to_string()), | ||
vec![LintFix { | ||
description: "Escape entities in the text node".into(), | ||
changes: vec![LintFixChange { | ||
new_text: new_text.into(), | ||
range: child.range(), | ||
}], | ||
}], | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn jsx_no_unescaped_entities_valid() { | ||
assert_lint_ok! { | ||
JSXNoUnescapedEntities, | ||
filename: "file:///foo.jsx", | ||
r#"<div>></div>"#, | ||
r#"<div>{">"}</div>"#, | ||
}; | ||
} | ||
|
||
#[test] | ||
fn jsx_no_unescaped_entities_invalid() { | ||
assert_lint_err! { | ||
JSXNoUnescapedEntities, | ||
filename: "file:///foo.jsx", | ||
r#"<div>'">}</div>"#: [ | ||
{ | ||
col: 0, | ||
message: MESSAGE, | ||
hint: HINT, | ||
fix: ( | ||
"Escape entities in the text node", | ||
"<div>'">}</div>" | ||
) | ||
} | ||
] | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters