|
| 1 | +use oxc_ast::{ |
| 2 | + AstKind, |
| 3 | + ast::{Argument, JSXElementName}, |
| 4 | +}; |
| 5 | +use oxc_diagnostics::OxcDiagnostic; |
| 6 | +use oxc_macros::declare_oxc_lint; |
| 7 | +use oxc_span::Span; |
| 8 | + |
| 9 | +use crate::{AstNode, context::LintContext, rule::Rule, utils::is_create_element_call}; |
| 10 | + |
| 11 | +fn no_namespace_diagnostic(span: Span, component_name: &str) -> OxcDiagnostic { |
| 12 | + let message = format!( |
| 13 | + r"React component {component_name} must not be in a namespace, as React does not support them." |
| 14 | + ); |
| 15 | + |
| 16 | + OxcDiagnostic::warn(message).with_label(span) |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug, Default, Clone)] |
| 20 | +pub struct NoNamespace; |
| 21 | + |
| 22 | +declare_oxc_lint!( |
| 23 | + /// ### What it does |
| 24 | + /// Enforce that namespaces are not used in React elements. |
| 25 | + /// |
| 26 | + /// ### Why is this bad? |
| 27 | + /// Namespaces in React elements, such as svg:circle, are not supported by React. |
| 28 | + /// |
| 29 | + /// ### Examples |
| 30 | + /// |
| 31 | + /// Examples of **incorrect** code for this rule: |
| 32 | + /// ```jsx |
| 33 | + /// <ns:TestComponent /> |
| 34 | + /// <Ns:TestComponent /> |
| 35 | + /// ``` |
| 36 | + /// |
| 37 | + /// Examples of **correct** code for this rule: |
| 38 | + /// ```jsx |
| 39 | + /// <TestComponent /> |
| 40 | + /// <testComponent /> |
| 41 | + /// ``` |
| 42 | + NoNamespace, |
| 43 | + react, |
| 44 | + suspicious, |
| 45 | +); |
| 46 | + |
| 47 | +impl Rule for NoNamespace { |
| 48 | + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { |
| 49 | + match node.kind() { |
| 50 | + AstKind::JSXOpeningElement(element) => { |
| 51 | + if let JSXElementName::NamespacedName(namespaced_name) = &element.name { |
| 52 | + let component_name_with_ns = format!( |
| 53 | + "{}:{}", |
| 54 | + namespaced_name.namespace.name, namespaced_name.property.name |
| 55 | + ); |
| 56 | + |
| 57 | + ctx.diagnostic(no_namespace_diagnostic( |
| 58 | + namespaced_name.span, |
| 59 | + &component_name_with_ns, |
| 60 | + )); |
| 61 | + } |
| 62 | + } |
| 63 | + AstKind::CallExpression(call_expr) => { |
| 64 | + if is_create_element_call(call_expr) { |
| 65 | + let Some(Argument::StringLiteral(str_lit)) = call_expr.arguments.first() else { |
| 66 | + return; |
| 67 | + }; |
| 68 | + |
| 69 | + if str_lit.value.contains(':') { |
| 70 | + ctx.diagnostic(no_namespace_diagnostic(str_lit.span, &str_lit.value)); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + _ => {} |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#[test] |
| 80 | +fn test() { |
| 81 | + use crate::tester::Tester; |
| 82 | + |
| 83 | + let pass = vec![ |
| 84 | + "<testcomponent />", |
| 85 | + r#"React.createElement("testcomponent")"#, |
| 86 | + "<testComponent />", |
| 87 | + r#"React.createElement("testComponent")"#, |
| 88 | + "<test_component />", |
| 89 | + r#"React.createElement("test_component")"#, |
| 90 | + "<TestComponent />", |
| 91 | + r#"React.createElement("TestComponent")"#, |
| 92 | + "<object.testcomponent />", |
| 93 | + r#"React.createElement("object.testcomponent")"#, |
| 94 | + "<object.testComponent />", |
| 95 | + r#"React.createElement("object.testComponent")"#, |
| 96 | + "<object.test_component />", |
| 97 | + r#"React.createElement("object.test_component")"#, |
| 98 | + "<object.TestComponent />", |
| 99 | + r#"React.createElement("object.TestComponent")"#, |
| 100 | + "<Object.testcomponent />", |
| 101 | + r#"React.createElement("Object.testcomponent")"#, |
| 102 | + "<Object.testComponent />", |
| 103 | + r#"React.createElement("Object.testComponent")"#, |
| 104 | + "<Object.test_component />", |
| 105 | + r#"React.createElement("Object.test_component")"#, |
| 106 | + "<Object.TestComponent />", |
| 107 | + r#"React.createElement("Object.TestComponent")"#, |
| 108 | + "React.createElement(null)", |
| 109 | + "React.createElement(true)", |
| 110 | + "React.createElement({})", |
| 111 | + ]; |
| 112 | + |
| 113 | + let fail = vec![ |
| 114 | + "<ns:testcomponent />", |
| 115 | + r#"React.createElement("ns:testcomponent")"#, |
| 116 | + "<ns:testComponent />", |
| 117 | + r#"React.createElement("ns:testComponent")"#, |
| 118 | + "<ns:test_component />", |
| 119 | + r#"React.createElement("ns:test_component")"#, |
| 120 | + "<ns:TestComponent />", |
| 121 | + r#"React.createElement("ns:TestComponent")"#, |
| 122 | + "<Ns:testcomponent />", |
| 123 | + r#"React.createElement("Ns:testcomponent")"#, |
| 124 | + "<Ns:testComponent />", |
| 125 | + r#"React.createElement("Ns:testComponent")"#, |
| 126 | + "<Ns:test_component />", |
| 127 | + r#"React.createElement("Ns:test_component")"#, |
| 128 | + "<Ns:TestComponent />", |
| 129 | + r#"React.createElement("Ns:TestComponent")"#, |
| 130 | + ]; |
| 131 | + |
| 132 | + Tester::new(NoNamespace::NAME, NoNamespace::PLUGIN, pass, fail).test_and_snapshot(); |
| 133 | +} |
0 commit comments