Skip to content

Commit

Permalink
use lazy_static
Browse files Browse the repository at this point in the history
  • Loading branch information
baseballyama committed Dec 28, 2024
1 parent 080507f commit b303064
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions crates/oxc_linter/src/rules/eslint/no_octal_escape.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::{context::LintContext, rule::Rule, AstNode};
use lazy_static::lazy_static;
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use regex::Regex;
use std::sync::OnceLock;

use crate::{context::LintContext, rule::Rule, AstNode};

fn no_octal_escape_diagnostic(span: Span, sequence: &str) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Don't use octal: '\\{sequence}'. Use '\\u....' instead."))
Expand Down Expand Up @@ -45,7 +44,7 @@ impl Rule for NoOctalEscape {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::StringLiteral(literal) = node.kind() {
if let Some(raw) = &literal.raw {
if let Some(captures) = get_octal_escape_pattern().captures(raw) {
if let Some(captures) = CUSTOM_ERROR_REGEX_PATTERN.captures(raw) {
if let Some(sequence) = captures.get(1) {
ctx.diagnostic(no_octal_escape_diagnostic(literal.span, sequence.as_str()));
}
Expand All @@ -55,12 +54,9 @@ impl Rule for NoOctalEscape {
}
}

static OCTAL_ESCAPE_PATTERN: OnceLock<Regex> = OnceLock::new();

fn get_octal_escape_pattern() -> &'static Regex {
OCTAL_ESCAPE_PATTERN.get_or_init(|| {
Regex::new(r"^(?:[^\\]|\\.)*?\\([0-3][0-7]{1,2}|[4-7][0-7]|(08|09)|[1-7])").unwrap()
})
lazy_static! {
static ref CUSTOM_ERROR_REGEX_PATTERN: Regex =
Regex::new(r"^(?:[A-Z][\da-z]*)*Error$").unwrap();
}

#[test]
Expand Down

0 comments on commit b303064

Please sign in to comment.