diff --git a/crates/oxc_linter/src/rules.rs b/crates/oxc_linter/src/rules.rs index be3f7c3d70471..7f5d474262c98 100644 --- a/crates/oxc_linter/src/rules.rs +++ b/crates/oxc_linter/src/rules.rs @@ -291,6 +291,7 @@ mod react { pub mod no_direct_mutation_state; pub mod no_find_dom_node; pub mod no_is_mounted; + pub mod no_namespace; pub mod no_render_return_value; pub mod no_set_state; pub mod no_string_refs; @@ -870,6 +871,7 @@ oxc_macros::declare_all_lint_rules! { react::jsx_no_undef, react::jsx_no_useless_fragment, react::jsx_props_no_spread_multi, + react::no_namespace, react::no_array_index_key, react::no_children_prop, react::no_danger_with_children, diff --git a/crates/oxc_linter/src/rules/react/no_namespace.rs b/crates/oxc_linter/src/rules/react/no_namespace.rs new file mode 100644 index 0000000000000..5386fb293996a --- /dev/null +++ b/crates/oxc_linter/src/rules/react/no_namespace.rs @@ -0,0 +1,133 @@ +use oxc_ast::{ + AstKind, + ast::{Argument, JSXElementName}, +}; +use oxc_diagnostics::OxcDiagnostic; +use oxc_macros::declare_oxc_lint; +use oxc_span::Span; + +use crate::{AstNode, context::LintContext, rule::Rule, utils::is_create_element_call}; + +fn no_namespace_diagnostic(span: Span, component_name: &str) -> OxcDiagnostic { + let message = format!( + r"React component {component_name} must not be in a namespace, as React does not support them." + ); + + OxcDiagnostic::warn(message).with_label(span) +} + +#[derive(Debug, Default, Clone)] +pub struct NoNamespace; + +declare_oxc_lint!( + /// ### What it does + /// Enforce that namespaces are not used in React elements. + /// + /// ### Why is this bad? + /// Namespaces in React elements, such as svg:circle, are not supported by React. + /// + /// ### Examples + /// + /// Examples of **incorrect** code for this rule: + /// ```jsx + /// + /// + /// ``` + /// + /// Examples of **correct** code for this rule: + /// ```jsx + /// + /// + /// ``` + NoNamespace, + react, + suspicious, +); + +impl Rule for NoNamespace { + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { + match node.kind() { + AstKind::JSXOpeningElement(element) => { + if let JSXElementName::NamespacedName(namespaced_name) = &element.name { + let component_name_with_ns = format!( + "{}:{}", + namespaced_name.namespace.name, namespaced_name.property.name + ); + + ctx.diagnostic(no_namespace_diagnostic( + namespaced_name.span, + &component_name_with_ns, + )); + } + } + AstKind::CallExpression(call_expr) => { + if is_create_element_call(call_expr) { + let Some(Argument::StringLiteral(str_lit)) = call_expr.arguments.first() else { + return; + }; + + if str_lit.value.contains(':') { + ctx.diagnostic(no_namespace_diagnostic(str_lit.span, &str_lit.value)); + } + } + } + _ => {} + } + } +} + +#[test] +fn test() { + use crate::tester::Tester; + + let pass = vec![ + "", + r#"React.createElement("testcomponent")"#, + "", + r#"React.createElement("testComponent")"#, + "", + r#"React.createElement("test_component")"#, + "", + r#"React.createElement("TestComponent")"#, + "", + r#"React.createElement("object.testcomponent")"#, + "", + r#"React.createElement("object.testComponent")"#, + "", + r#"React.createElement("object.test_component")"#, + "", + r#"React.createElement("object.TestComponent")"#, + "", + r#"React.createElement("Object.testcomponent")"#, + "", + r#"React.createElement("Object.testComponent")"#, + "", + r#"React.createElement("Object.test_component")"#, + "", + r#"React.createElement("Object.TestComponent")"#, + "React.createElement(null)", + "React.createElement(true)", + "React.createElement({})", + ]; + + let fail = vec![ + "", + r#"React.createElement("ns:testcomponent")"#, + "", + r#"React.createElement("ns:testComponent")"#, + "", + r#"React.createElement("ns:test_component")"#, + "", + r#"React.createElement("ns:TestComponent")"#, + "", + r#"React.createElement("Ns:testcomponent")"#, + "", + r#"React.createElement("Ns:testComponent")"#, + "", + r#"React.createElement("Ns:test_component")"#, + "", + r#"React.createElement("Ns:TestComponent")"#, + ]; + + Tester::new(NoNamespace::NAME, NoNamespace::PLUGIN, pass, fail).test_and_snapshot(); +} diff --git a/crates/oxc_linter/src/snapshots/react_no_namespace.snap b/crates/oxc_linter/src/snapshots/react_no_namespace.snap new file mode 100644 index 0000000000000..3229bd30a94ba --- /dev/null +++ b/crates/oxc_linter/src/snapshots/react_no_namespace.snap @@ -0,0 +1,98 @@ +--- +source: crates/oxc_linter/src/tester.rs +--- + ⚠ eslint-plugin-react(no-namespace): React component ns:testcomponent must not be in a namespace, as React does not support them. + ╭─[no_namespace.tsx:1:2] + 1 │ + · ──────────────── + ╰──── + + ⚠ eslint-plugin-react(no-namespace): React component ns:testcomponent must not be in a namespace, as React does not support them. + ╭─[no_namespace.tsx:1:21] + 1 │ React.createElement("ns:testcomponent") + · ────────────────── + ╰──── + + ⚠ eslint-plugin-react(no-namespace): React component ns:testComponent must not be in a namespace, as React does not support them. + ╭─[no_namespace.tsx:1:2] + 1 │ + · ──────────────── + ╰──── + + ⚠ eslint-plugin-react(no-namespace): React component ns:testComponent must not be in a namespace, as React does not support them. + ╭─[no_namespace.tsx:1:21] + 1 │ React.createElement("ns:testComponent") + · ────────────────── + ╰──── + + ⚠ eslint-plugin-react(no-namespace): React component ns:test_component must not be in a namespace, as React does not support them. + ╭─[no_namespace.tsx:1:2] + 1 │ + · ───────────────── + ╰──── + + ⚠ eslint-plugin-react(no-namespace): React component ns:test_component must not be in a namespace, as React does not support them. + ╭─[no_namespace.tsx:1:21] + 1 │ React.createElement("ns:test_component") + · ─────────────────── + ╰──── + + ⚠ eslint-plugin-react(no-namespace): React component ns:TestComponent must not be in a namespace, as React does not support them. + ╭─[no_namespace.tsx:1:2] + 1 │ + · ──────────────── + ╰──── + + ⚠ eslint-plugin-react(no-namespace): React component ns:TestComponent must not be in a namespace, as React does not support them. + ╭─[no_namespace.tsx:1:21] + 1 │ React.createElement("ns:TestComponent") + · ────────────────── + ╰──── + + ⚠ eslint-plugin-react(no-namespace): React component Ns:testcomponent must not be in a namespace, as React does not support them. + ╭─[no_namespace.tsx:1:2] + 1 │ + · ──────────────── + ╰──── + + ⚠ eslint-plugin-react(no-namespace): React component Ns:testcomponent must not be in a namespace, as React does not support them. + ╭─[no_namespace.tsx:1:21] + 1 │ React.createElement("Ns:testcomponent") + · ────────────────── + ╰──── + + ⚠ eslint-plugin-react(no-namespace): React component Ns:testComponent must not be in a namespace, as React does not support them. + ╭─[no_namespace.tsx:1:2] + 1 │ + · ──────────────── + ╰──── + + ⚠ eslint-plugin-react(no-namespace): React component Ns:testComponent must not be in a namespace, as React does not support them. + ╭─[no_namespace.tsx:1:21] + 1 │ React.createElement("Ns:testComponent") + · ────────────────── + ╰──── + + ⚠ eslint-plugin-react(no-namespace): React component Ns:test_component must not be in a namespace, as React does not support them. + ╭─[no_namespace.tsx:1:2] + 1 │ + · ───────────────── + ╰──── + + ⚠ eslint-plugin-react(no-namespace): React component Ns:test_component must not be in a namespace, as React does not support them. + ╭─[no_namespace.tsx:1:21] + 1 │ React.createElement("Ns:test_component") + · ─────────────────── + ╰──── + + ⚠ eslint-plugin-react(no-namespace): React component Ns:TestComponent must not be in a namespace, as React does not support them. + ╭─[no_namespace.tsx:1:2] + 1 │ + · ──────────────── + ╰──── + + ⚠ eslint-plugin-react(no-namespace): React component Ns:TestComponent must not be in a namespace, as React does not support them. + ╭─[no_namespace.tsx:1:21] + 1 │ React.createElement("Ns:TestComponent") + · ────────────────── + ╰────