-
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-danger-with-children rule (#1349)
* feat: add jsx-no-danger-with-children rule * fix: clippy * fix: update schema * fix: update docs * remove code comment
- Loading branch information
1 parent
dcdbb1a
commit 1e76fc0
Showing
5 changed files
with
115 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 JSX children together with `dangerouslySetInnerHTML` is invalid as they | ||
will be ignored. | ||
|
||
### Invalid: | ||
|
||
```tsx | ||
<div dangerouslySetInnerHTML={{ __html: "<h1>hello</h1>" }}> | ||
<h1>this will never be rendered</h1> | ||
</div>; | ||
``` | ||
|
||
### Valid: | ||
|
||
```tsx | ||
<div dangerouslySetInnerHTML={{ __html: "<h1>hello</h1>" }} />; | ||
``` |
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,86 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
use super::{Context, LintRule}; | ||
use crate::handler::{Handler, Traverse}; | ||
use crate::tags::{self, Tags}; | ||
use crate::Program; | ||
use deno_ast::view::{JSXAttrName, JSXAttrOrSpread, JSXElement}; | ||
use deno_ast::SourceRanged; | ||
|
||
#[derive(Debug)] | ||
pub struct JSXNoDangerWithChildren; | ||
|
||
const CODE: &str = "jsx-no-danger-with-children"; | ||
|
||
impl LintRule for JSXNoDangerWithChildren { | ||
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, | ||
) { | ||
JSXNoDangerWithChildrenHandler.traverse(program, context); | ||
} | ||
|
||
#[cfg(feature = "docs")] | ||
fn docs(&self) -> &'static str { | ||
include_str!("../../docs/rules/jsx_no_danger_with_children.md") | ||
} | ||
} | ||
|
||
const MESSAGE: &str = | ||
"Using JSX children together with 'dangerouslySetInnerHTML' is invalid"; | ||
const HINT: &str = "Remove the JSX children"; | ||
|
||
struct JSXNoDangerWithChildrenHandler; | ||
|
||
impl Handler for JSXNoDangerWithChildrenHandler { | ||
fn jsx_element(&mut self, node: &JSXElement, ctx: &mut Context) { | ||
for attr in node.opening.attrs { | ||
if let JSXAttrOrSpread::JSXAttr(attr) = attr { | ||
if let JSXAttrName::Ident(id) = attr.name { | ||
if id.sym() == "dangerouslySetInnerHTML" && !node.children.is_empty() | ||
{ | ||
ctx.add_diagnostic_with_hint(node.range(), CODE, MESSAGE, HINT); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn jsx_no_danger_with_children_valid() { | ||
assert_lint_ok! { | ||
JSXNoDangerWithChildren, | ||
filename: "file:///foo.jsx", | ||
r#"<div dangerouslySetInnerHTML={{ __html: "foo" }} />"#, | ||
}; | ||
} | ||
|
||
#[test] | ||
fn jsx_no_danger_with_children_invalid() { | ||
assert_lint_err! { | ||
JSXNoDangerWithChildren, | ||
filename: "file:///foo.jsx", | ||
r#"<div dangerouslySetInnerHTML={{ __html: "foo" }}>foo</div>"#: [ | ||
{ | ||
col: 0, | ||
message: MESSAGE, | ||
hint: HINT | ||
} | ||
] | ||
}; | ||
} | ||
} |
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