Skip to content

Commit

Permalink
perf
Browse files Browse the repository at this point in the history
  • Loading branch information
baseballyama committed Dec 27, 2024
1 parent 3503d6b commit 87df5bf
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions crates/oxc_linter/src/rules/eslint/no_octal_escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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};

Expand Down Expand Up @@ -43,12 +44,8 @@ declare_oxc_lint!(
impl Rule for NoOctalEscape {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::StringLiteral(literal) = node.kind() {
let octal_escape_pattern =
Regex::new(r"^(?:[^\\]|\\.)*?\\([0-3][0-7]{1,2}|[4-7][0-7]|(08|09)|[1-7])")
.unwrap();

if let Some(raw) = &literal.raw {
if let Some(captures) = octal_escape_pattern.captures(raw) {
if let Some(captures) = get_octal_escape_pattern().captures(raw) {
if let Some(sequence) = captures.get(1) {
ctx.diagnostic(no_octal_escape_diagnostic(literal.span, sequence.as_str()));
}
Expand All @@ -58,6 +55,14 @@ 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()
})
}

#[test]
fn test() {
use crate::tester::Tester;
Expand Down

0 comments on commit 87df5bf

Please sign in to comment.