Skip to content

Commit

Permalink
Use lazy_static! for regex evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
radu2147 committed Oct 30, 2024
1 parent 1c6df46 commit 3e32476
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions crates/oxc_linter/src/rules/react/jsx_no_script_url.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::context::ContextHost;
use crate::{context::LintContext, rule::Rule, AstNode};
use lazy_static::lazy_static;
use oxc_ast::ast::JSXAttributeItem;
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
Expand All @@ -16,7 +17,10 @@ fn jsx_no_script_url_diagnostic(span: Span) -> OxcDiagnostic {
.with_label(span)
}

const IS_JAVA_SCRIPT_PROTOCOL: &str = r"(j|J)[\r\n\t]*(a|A)[\r\n\t]*(v|V)[\r\n\t]*(a|A)[\r\n\t]*(s|S)[\r\n\t]*(c|C)[\r\n\t]*(r|R)[\r\n\t]*(i|I)[\r\n\t]*(p|P)[\r\n\t]*(t|T)[\r\n\t]*:";
lazy_static! {
static ref JAVASCRIPT_REGEX: Regex =
Regex::new(r"(j|J)[\r\n\t]*(a|A)[\r\n\t]*(v|V)[\r\n\t]*(a|A)[\r\n\t]*(s|S)[\r\n\t]*(c|C)[\r\n\t]*(r|R)[\r\n\t]*(i|I)[\r\n\t]*(p|P)[\r\n\t]*(t|T)[\r\n\t]*:").unwrap();
}

#[derive(Debug, Default, Clone)]
pub struct JsxNoScriptUrl(Box<JsxNoScriptUrlConfig>);
Expand Down Expand Up @@ -96,9 +100,8 @@ impl Rule for JsxNoScriptUrl {
return;
};
if prop_value.as_string_literal().is_some_and(|val| {
let re = Regex::new(IS_JAVA_SCRIPT_PROTOCOL).unwrap();
link_props.contains(&attr.name.get_identifier().name.to_string())
&& re.captures(&val.value).is_some()
&& JAVASCRIPT_REGEX.captures(&val.value).is_some()
}) {
ctx.diagnostic(jsx_no_script_url_diagnostic(attr.span()));
}
Expand All @@ -111,12 +114,11 @@ impl Rule for JsxNoScriptUrl {
return;
};
if prop_value.as_string_literal().is_some_and(|val| {
let re = Regex::new(IS_JAVA_SCRIPT_PROTOCOL).unwrap();
check_is_link_attribute(
component_name.as_str(),
attr.name.get_identifier().name.to_string(),
ctx,
) && re.captures(&val.value).is_some()
) && JAVASCRIPT_REGEX.captures(&val.value).is_some()
}) {
ctx.diagnostic(jsx_no_script_url_diagnostic(attr.span()));
}
Expand Down

0 comments on commit 3e32476

Please sign in to comment.