-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathreact_no_danger.rs
More file actions
92 lines (80 loc) · 1.9 KB
/
react_no_danger.rs
File metadata and controls
92 lines (80 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// 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::{JSXAttr, JSXAttrName};
use deno_ast::SourceRanged;
#[derive(Debug)]
pub struct ReactNoDanger;
const CODE: &str = "react-no-danger";
impl LintRule for ReactNoDanger {
fn tags(&self) -> Tags {
&[tags::REACT, tags::FRESH]
}
fn code(&self) -> &'static str {
CODE
}
fn lint_program_with_ast_view(
&self,
context: &mut Context,
program: Program,
) {
NoDangerHandler.traverse(program, context);
}
}
const MESSAGE: &str = "Do not use `dangerouslySetInnerHTML`";
const HINT: &str = "Remove this attribute";
struct NoDangerHandler;
impl Handler for NoDangerHandler {
fn jsx_attr(&mut self, node: &JSXAttr, ctx: &mut Context) {
if let JSXAttrName::Ident(name) = node.name {
if name.sym() == "dangerouslySetInnerHTML" {
ctx.add_diagnostic_with_hint(node.range(), CODE, MESSAGE, HINT);
}
}
}
}
// most tests are taken from ESlint, commenting those
// requiring code path support
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_danger_valid() {
assert_lint_ok! {
ReactNoDanger,
filename: "file:///foo.jsx",
// non derived classes.
r#"<div />"#,
};
}
#[test]
fn no_danger_invalid() {
assert_lint_err! {
ReactNoDanger,
filename: "file:///foo.jsx",
"<div dangerouslySetInnerHTML />": [
{
col: 5,
message: MESSAGE,
hint: HINT,
}
],
r#"<div dangerouslySetInnerHTML="" />"#: [
{
col: 5,
message: MESSAGE,
hint: HINT,
}
],
"<div dangerouslySetInnerHTML={{}} />": [
{
col: 5,
message: MESSAGE,
hint: HINT,
}
]
};
}
}