From e0ec31026b4f4a80a9b76136ba3e4353bfc685a3 Mon Sep 17 00:00:00 2001 From: Yuiki <10101661+Yuiki@users.noreply.github.com> Date: Sat, 25 Nov 2023 00:01:12 +0900 Subject: [PATCH] refactor: use as_static_value in extract_literal_string --- .../analyzers/nursery/use_regex_literals.rs | 45 +++++-------------- 1 file changed, 10 insertions(+), 35 deletions(-) diff --git a/crates/biome_js_analyze/src/analyzers/nursery/use_regex_literals.rs b/crates/biome_js_analyze/src/analyzers/nursery/use_regex_literals.rs index a1e99aaecc4b..5b54fcb7c68b 100644 --- a/crates/biome_js_analyze/src/analyzers/nursery/use_regex_literals.rs +++ b/crates/biome_js_analyze/src/analyzers/nursery/use_regex_literals.rs @@ -6,13 +6,11 @@ use biome_diagnostics::Applicability; use biome_js_factory::make::js_regex_literal_expression; use biome_js_semantic::SemanticModel; use biome_js_syntax::{ - global_identifier, AnyJsCallArgument, AnyJsExpression, AnyJsLiteralExpression, - AnyJsTemplateElement, JsCallArguments, JsCallExpression, JsComputedMemberExpression, + global_identifier, static_value::StaticValue, AnyJsCallArgument, AnyJsExpression, + AnyJsLiteralExpression, JsCallArguments, JsCallExpression, JsComputedMemberExpression, JsNewExpression, JsSyntaxKind, JsSyntaxToken, }; -use biome_rowan::{ - declare_node_union, AstNode, AstNodeList, AstSeparatedList, BatchMutationExt, SyntaxError, -}; +use biome_rowan::{declare_node_union, AstNode, AstSeparatedList, BatchMutationExt, SyntaxError}; use crate::{semantic_services::Semantic, JsRuleAction}; @@ -209,36 +207,13 @@ fn extract_literal_string(from: AnyJsCallArgument) -> Option { let AnyJsCallArgument::AnyJsExpression(expr) = from else { return None; }; - match expr.omit_parentheses() { - AnyJsExpression::AnyJsLiteralExpression(expr) => { - let expr = expr.as_js_string_literal_expression()?; - let text = expr.inner_string_text().ok()?; - Some(text.to_string()) - } - AnyJsExpression::JsTemplateExpression(expr) => { - let elements = expr.elements(); - if !elements - .iter() - .all(|elem| matches!(elem, AnyJsTemplateElement::JsTemplateChunkElement(_))) - { - return None; - } - - let text = elements - .iter() - .filter_map(|elem| { - elem.as_js_template_chunk_element() - .unwrap() - .template_chunk_token() - .ok() - }) - .map(|elem| elem.text().replace('\n', "\\n")) - .collect::(); - - Some(text) - } - _ => None, - } + expr.omit_parentheses() + .as_static_value() + .and_then(|value| match value { + StaticValue::String(_) => Some(value), + _ => None, + }) + .map(|value| value.text().to_string().replace('\n', "\\n")) } fn extract_inner_text(expr: &JsComputedMemberExpression) -> Option {