-
-
Notifications
You must be signed in to change notification settings - Fork 518
/
Copy pathjsx_no_undef.rs
144 lines (130 loc) · 4.69 KB
/
jsx_no_undef.rs
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use oxc_ast::{
ast::{IdentifierReference, JSXElementName, JSXMemberExpression, JSXMemberExpressionObject},
AstKind,
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{context::LintContext, rule::Rule, AstNode};
fn jsx_no_undef_diagnostic(x0: &str, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Disallow undeclared variables in JSX")
.with_help(format!("'{x0}' is not defined."))
.with_label(span1)
}
#[derive(Debug, Default, Clone)]
pub struct JsxNoUndef;
declare_oxc_lint!(
/// ### What it does
/// Disallow undeclared variables in JSX
///
/// ### Why is this bad?
/// It is most likely a potential ReferenceError caused by a misspelling of a variable or parameter name.
///
/// ### Example
/// ```jsx
/// const A = () => <App />
/// const C = <B />
/// ```
JsxNoUndef,
correctness
);
fn get_resolvable_ident<'a>(node: &'a JSXElementName<'a>) -> Option<&'a IdentifierReference> {
match node {
JSXElementName::Identifier(_) | JSXElementName::NamespacedName(_) => None,
JSXElementName::IdentifierReference(ref ident) => Some(ident),
JSXElementName::MemberExpression(expr) => get_member_ident(expr),
}
}
fn get_member_ident<'a>(expr: &'a JSXMemberExpression<'a>) -> Option<&'a IdentifierReference> {
match &expr.object {
JSXMemberExpressionObject::Identifier(_) => None,
JSXMemberExpressionObject::IdentifierReference(ident) => Some(ident),
JSXMemberExpressionObject::MemberExpression(next_expr) => get_member_ident(next_expr),
}
}
impl Rule for JsxNoUndef {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::JSXOpeningElement(elem) = &node.kind() {
if let Some(ident) = get_resolvable_ident(&elem.name) {
let reference = ctx.symbols().get_reference(ident.reference_id().unwrap());
if reference.symbol_id().is_some() {
return;
}
let name = ident.name.as_str();
// TODO: Remove this check once we have `JSXMemberExpressionObject::ThisExpression`
if name == "this" {
return;
}
if ctx.globals().is_enabled(name) {
return;
}
ctx.diagnostic(jsx_no_undef_diagnostic(name, ident.span));
}
}
}
fn should_run(&self, ctx: &LintContext) -> bool {
ctx.source_type().is_jsx()
}
}
#[test]
fn test() {
use serde_json::json;
use crate::tester::Tester;
let pass = vec![
("var React, App; React.render(<App />);", None),
("var React; React.render(<img />);", None),
("var React; React.render(<x-gif />);", None),
("var React, app; React.render(<app.Foo />);", None),
("var React, app; React.render(<app.foo.Bar />);", None),
("var React; React.render(<Apppp:Foo />);", None),
(
r"
var React;
class Hello extends React.Component {
render() {
return <this.props.tag />
}
}
",
None,
),
// TODO: Text should be declared in globals ("var React; React.render(<Text />);", None),
(
r#"
import Text from "cool-module";
const TextWrapper = function (props) {
return (
<Text />
);
};
"#,
None,
),
("var App; var React; enum A { App }; React.render(<App />);", None),
("var React; enum A { App }; var App; React.render(<App />);", None),
("var React; import App = require('./app'); React.render(<App />);", None),
(
"
var React;
import { Foo } from './foo';
import App = Foo.App;
React.render(<App />);
",
None,
),
];
let fail = vec![
("var React; React.render(<App />);", None),
("var React; React.render(<Appp.Foo />);", None),
("var React; React.render(<appp.Foo />);", None),
("var React; React.render(<appp.foo.Bar />);", None),
("var React; React.render(<Foo />);", None),
("var React; Unknown; React.render(<Unknown />)", None),
("var React; { const App = null; }; React.render(<App />);", None),
("var React; enum A { App }; React.render(<App />);", None),
];
Tester::new(JsxNoUndef::NAME, pass, fail).test_and_snapshot();
let pass = vec![("let x = <A.B />;", None, Some(json!({ "globals": {"A": "readonly" } })))];
let fail = vec![("let x = <A.B />;", None, None)];
Tester::new(JsxNoUndef::NAME, pass, fail).test();
}