From 8e03ce774ae11a56ed63ccc5dba2acacd26165a3 Mon Sep 17 00:00:00 2001 From: Daniel Frederico Lins Leite Date: Wed, 9 Nov 2022 16:56:18 +0000 Subject: [PATCH] fix(rome_js_analyze): useValidAnchor considering all possible expressions (#3599) * useValidAnchor considering all possible expressions --- .../src/analyzers/a11y/use_valid_anchor.rs | 80 +++++++++++-------- .../tests/specs/a11y/useValidAnchor.jsx | 6 ++ .../tests/specs/a11y/useValidAnchor.jsx.snap | 6 ++ 3 files changed, 59 insertions(+), 33 deletions(-) diff --git a/crates/rome_js_analyze/src/analyzers/a11y/use_valid_anchor.rs b/crates/rome_js_analyze/src/analyzers/a11y/use_valid_anchor.rs index da1e8b2e2e3..b8d61d6b621 100644 --- a/crates/rome_js_analyze/src/analyzers/a11y/use_valid_anchor.rs +++ b/crates/rome_js_analyze/src/analyzers/a11y/use_valid_anchor.rs @@ -268,47 +268,61 @@ fn is_invalid_anchor(anchor_attribute: &JsxAttribute) -> Option { let expression = attribute_value.expression().ok()?; - // href={null} - if let JsAnyExpression::JsAnyLiteralExpression( - JsAnyLiteralExpression::JsNullLiteralExpression(null), - ) = expression - { - return Some(UseValidAnchorState::IncorrectHref( - null.syntax().text_trimmed_range(), - )); - } else if let JsAnyExpression::JsIdentifierExpression(identifier) = expression { - let text = identifier.name().ok()?.value_token().ok()?; - // href={undefined} - if text.text_trimmed() == "undefined" { + + match expression { + // href={null} + JsAnyExpression::JsAnyLiteralExpression( + JsAnyLiteralExpression::JsNullLiteralExpression(null), + ) => { return Some(UseValidAnchorState::IncorrectHref( - text.text_trimmed_range(), + null.syntax().text_trimmed_range(), )); } - } else if let JsAnyExpression::JsAnyLiteralExpression( - JsAnyLiteralExpression::JsStringLiteralExpression(string_literal), - ) = expression - { - let text = string_literal.inner_string_text().ok()?; - if text == "#" { - return Some(UseValidAnchorState::IncorrectHref( - string_literal.syntax().text_trimmed_range(), - )); + JsAnyExpression::JsIdentifierExpression(identifier) => { + let text = identifier.name().ok()?.value_token().ok()?; + // href={undefined} + if text.text_trimmed() == "undefined" { + return Some(UseValidAnchorState::IncorrectHref( + text.text_trimmed_range(), + )); + } } - } else if let JsAnyExpression::JsTemplate(template) = expression { - let mut iter = template.elements().iter(); - if let Some(JsAnyTemplateElement::JsTemplateChunkElement(element)) = iter.next() { - let template_token = element.template_chunk_token().ok()?; - let text = template_token.text_trimmed(); - if text == "#" || text.contains("javascript:") { + JsAnyExpression::JsAnyLiteralExpression( + JsAnyLiteralExpression::JsStringLiteralExpression(string_literal), + ) => { + let text = string_literal.inner_string_text().ok()?; + if text == "#" { return Some(UseValidAnchorState::IncorrectHref( - template_token.text_trimmed_range(), + string_literal.syntax().text_trimmed_range(), )); } } - } else { - return Some(UseValidAnchorState::IncorrectHref( - expression.syntax().text_trimmed_range(), - )); + JsAnyExpression::JsTemplate(template) => { + let mut iter = template.elements().iter(); + if let Some(JsAnyTemplateElement::JsTemplateChunkElement(element)) = iter.next() + { + let template_token = element.template_chunk_token().ok()?; + let text = template_token.text_trimmed(); + if text == "#" || text.contains("javascript:") { + return Some(UseValidAnchorState::IncorrectHref( + template_token.text_trimmed_range(), + )); + } + } + } + JsAnyExpression::ImportMeta(_) + | JsAnyExpression::JsClassExpression(_) + | JsAnyExpression::JsImportCallExpression(_) + | JsAnyExpression::JsObjectExpression(_) + | JsAnyExpression::JsSuperExpression(_) + | JsAnyExpression::JsUnaryExpression(_) + | JsAnyExpression::JsxTagExpression(_) + | JsAnyExpression::NewTarget(_) => { + return Some(UseValidAnchorState::IncorrectHref( + expression.syntax().text_trimmed_range(), + )); + } + _ => {} } } JsxAnyAttributeValue::JsxAnyTag(_) => {} diff --git a/crates/rome_js_analyze/tests/specs/a11y/useValidAnchor.jsx b/crates/rome_js_analyze/tests/specs/a11y/useValidAnchor.jsx index ed5a39f3eb9..8bb127dcaf5 100644 --- a/crates/rome_js_analyze/tests/specs/a11y/useValidAnchor.jsx +++ b/crates/rome_js_analyze/tests/specs/a11y/useValidAnchor.jsx @@ -17,4 +17,10 @@ + Download + Download + Download + Download + Download + Download \ No newline at end of file diff --git a/crates/rome_js_analyze/tests/specs/a11y/useValidAnchor.jsx.snap b/crates/rome_js_analyze/tests/specs/a11y/useValidAnchor.jsx.snap index a086044b69e..550b0c7087b 100644 --- a/crates/rome_js_analyze/tests/specs/a11y/useValidAnchor.jsx.snap +++ b/crates/rome_js_analyze/tests/specs/a11y/useValidAnchor.jsx.snap @@ -23,6 +23,12 @@ expression: useValidAnchor.jsx + Download + Download + Download + Download + Download + Download ```