From ae9de2deca3307eb1c25853bd45fe7de935b4225 Mon Sep 17 00:00:00 2001 From: msdlisper <1170167213@qq.com> Date: Fri, 19 Jan 2024 23:58:59 +0800 Subject: [PATCH] refactor(linter): perfect the scope linter --- crates/oxc_cli/Cargo.toml | 18 +++--- crates/oxc_linter/src/rules/jsx_a11y/scope.rs | 58 +++++++++++-------- crates/oxc_linter/src/snapshots/scope.snap | 8 +++ crates/oxc_wasm/Cargo.toml | 6 +- 4 files changed, 55 insertions(+), 35 deletions(-) diff --git a/crates/oxc_cli/Cargo.toml b/crates/oxc_cli/Cargo.toml index 6ff953196370e..233f912fdcdb9 100644 --- a/crates/oxc_cli/Cargo.toml +++ b/crates/oxc_cli/Cargo.toml @@ -34,15 +34,15 @@ path = "src/format/main.rs" test = false [dependencies] -oxc_allocator = { workspace = true } -oxc_diagnostics = { workspace = true } -oxc_linter = { workspace = true } -oxc_parser = { workspace = true } -oxc_prettier = { workspace = true } -oxc_span = { workspace = true } -glob = { workspace = true } -lazy_static = { workspace = true } -regex = { workspace = true } +oxc_allocator = { workspace = true } +oxc_diagnostics = { workspace = true } +oxc_linter = { workspace = true } +oxc_parser = { workspace = true } +oxc_prettier = { workspace = true } +oxc_span = { workspace = true } +glob = { workspace = true } +lazy_static = { workspace = true } +regex = { workspace = true } ignore = { workspace = true, features = ["simd-accel"] } miette = { workspace = true } diff --git a/crates/oxc_linter/src/rules/jsx_a11y/scope.rs b/crates/oxc_linter/src/rules/jsx_a11y/scope.rs index a683fa922ca5f..719505958ee25 100644 --- a/crates/oxc_linter/src/rules/jsx_a11y/scope.rs +++ b/crates/oxc_linter/src/rules/jsx_a11y/scope.rs @@ -1,7 +1,4 @@ -use oxc_ast::{ - ast::{JSXAttributeItem, JSXElementName}, - AstKind, -}; +use oxc_ast::{ast::JSXAttributeItem, AstKind}; use oxc_diagnostics::{ miette::{self, Diagnostic}, thiserror::Error, @@ -9,7 +6,13 @@ use oxc_diagnostics::{ use oxc_macros::declare_oxc_lint; use oxc_span::Span; -use crate::{context::LintContext, rule::Rule, utils::has_jsx_prop_lowercase, AstNode}; +use crate::{ + context::LintContext, + globals::HTML_TAG, + rule::Rule, + utils::{get_element_type, has_jsx_prop_lowercase}, + AstNode, +}; #[derive(Debug, Error, Diagnostic)] #[error("eslint-plugin-jsx-a11y(scope): The scope prop can only be used on elements")] @@ -60,12 +63,15 @@ impl Rule for Scope { } }; - let JSXElementName::Identifier(identifier) = &jsx_el.name else { + let Some(element_type) = get_element_type(ctx, jsx_el) else { return; }; - let name = identifier.name.as_str(); - if name == "th" { + if element_type == "th" { + return; + } + + if !HTML_TAG.contains(&element_type) { return; } @@ -77,24 +83,30 @@ impl Rule for Scope { fn test() { use crate::tester::Tester; + fn settings() -> serde_json::Value { + serde_json::json!({ + "jsx-a11y": { + "components": { + "Foo": "div", + "TableHeader": "th" + } + } + }) + } + let pass = vec![ - (r"
;", None), - (r"
;", None), - (r"", None), - (r"", None), - (r"", None), - (r"", None), - // TODO aria-query like parts is needed - // (r"", None), - // TODO: When polymorphic components are supported - // (r"", None) + (r"
;", None, None), + (r"
;", None, None), + (r"", None, None), + (r"", None, None), + (r"", None, None), + (r"", None, None), + (r"", None, None), + (r"", None, Some(settings())), ]; - let fail = vec![ - (r"
", None), - // TODO: When polymorphic components are supported - // (r";", None), - ]; + let fail = + vec![(r"
", None, None), (r";", None, Some(settings()))]; Tester::new(Scope::NAME, pass, fail).with_jsx_a11y_plugin(true).test_and_snapshot(); } diff --git a/crates/oxc_linter/src/snapshots/scope.snap b/crates/oxc_linter/src/snapshots/scope.snap index 49467bc6dd80b..60a9609761adb 100644 --- a/crates/oxc_linter/src/snapshots/scope.snap +++ b/crates/oxc_linter/src/snapshots/scope.snap @@ -1,5 +1,6 @@ --- source: crates/oxc_linter/src/tester.rs +assertion_line: 143 expression: scope --- ⚠ eslint-plugin-jsx-a11y(scope): The scope prop can only be used on elements @@ -9,4 +10,11 @@ expression: scope ╰──── help: Must use scope prop only on elements + ⚠ eslint-plugin-jsx-a11y(scope): The scope prop can only be used on elements + ╭─[scope.tsx:1:1] + 1 │ ; + · ─────────── + ╰──── + help: Must use scope prop only on elements + diff --git a/crates/oxc_wasm/Cargo.toml b/crates/oxc_wasm/Cargo.toml index ddb645ec0fe40..3811cd5466d0e 100644 --- a/crates/oxc_wasm/Cargo.toml +++ b/crates/oxc_wasm/Cargo.toml @@ -23,9 +23,9 @@ default = ["console_error_panic_hook"] [dependencies] oxc = { workspace = true, features = ["serde", "semantic", "transformer", "minifier", "codegen"] } -oxc_linter = { workspace = true } -oxc_prettier = { workspace = true } -serde = { workspace = true } +oxc_linter = { workspace = true } +oxc_prettier = { workspace = true } +serde = { workspace = true } wasm-bindgen = { version = "0.2" } serde-wasm-bindgen = "0.6.3"