refactor(linter): use "parsed pattern" in no_div_regex rule. (#5417)

Part of #5416, Paves the road for upcoming refactors by adding the `oxc_regular_expression` dependency and a helper method for ease of access.
This commit is contained in:
rzvxa 2024-09-04 14:00:47 +00:00
parent aff2c71423
commit fdb8857630
4 changed files with 46 additions and 13 deletions

1
Cargo.lock generated
View file

@ -1639,6 +1639,7 @@ dependencies = [
"oxc_index",
"oxc_macros",
"oxc_parser",
"oxc_regular_expression",
"oxc_resolver",
"oxc_semantic",
"oxc_span",

View file

@ -11,6 +11,7 @@ use std::{
};
use oxc_allocator::CloneIn;
use oxc_regular_expression::ast::Pattern;
use oxc_span::{cmp::ContentEq, Atom, Span};
use oxc_syntax::number::NumberBase;
@ -137,6 +138,28 @@ impl<'a> RegExpPattern<'a> {
Self::Pattern(pat) => pat.span.source_text(source_text),
}
}
/// # Panics
/// If `self` is anything but `RegExpPattern::Pattern`.
pub fn require_pattern(&self) -> &Pattern<'a> {
if let Some(it) = self.as_pattern() {
it
} else {
unreachable!(
"Required `{}` to be `{}`",
stringify!(RegExpPattern),
stringify!(Pattern)
);
}
}
pub fn as_pattern(&self) -> Option<&Pattern<'a>> {
if let Self::Pattern(it) = self {
Some(it.as_ref())
} else {
None
}
}
}
impl<'a> fmt::Display for RegExpPattern<'a> {

View file

@ -20,18 +20,19 @@ workspace = true
doctest = false
[dependencies]
oxc_allocator = { workspace = true }
oxc_parser = { workspace = true }
oxc_span = { workspace = true, features = ["schemars", "serialize"] }
oxc_ast = { workspace = true }
oxc_cfg = { workspace = true }
oxc_diagnostics = { workspace = true }
oxc_index = { workspace = true }
oxc_macros = { workspace = true }
oxc_semantic = { workspace = true }
oxc_syntax = { workspace = true }
oxc_codegen = { workspace = true }
oxc_resolver = { workspace = true }
oxc_allocator = { workspace = true }
oxc_parser = { workspace = true }
oxc_span = { workspace = true, features = ["schemars", "serialize"] }
oxc_ast = { workspace = true }
oxc_cfg = { workspace = true }
oxc_diagnostics = { workspace = true }
oxc_index = { workspace = true }
oxc_macros = { workspace = true }
oxc_semantic = { workspace = true }
oxc_syntax = { workspace = true }
oxc_codegen = { workspace = true }
oxc_resolver = { workspace = true }
oxc_regular_expression = { workspace = true }
rayon = { workspace = true }
bitflags = { workspace = true }

View file

@ -1,6 +1,7 @@
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_regular_expression::ast::{CharacterKind, Term};
use oxc_span::Span;
use crate::{context::LintContext, rule::Rule, AstNode};
@ -36,7 +37,14 @@ declare_oxc_lint!(
impl Rule for NoDivRegex {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::RegExpLiteral(lit) = node.kind() {
if lit.regex.pattern.source_text(ctx.source_text()).starts_with('=') {
let Some(pattern) = lit.regex.pattern.as_pattern() else { return };
if pattern
.body
.body
.first()
.and_then(|it| it.body.first())
.is_some_and(|it| matches!(it, Term::Character(ch) if ch.kind == CharacterKind::Symbol && ch.value == '=' as u32))
{
ctx.diagnostic_with_fix(no_div_regex_diagnostic(lit.span), |fixer| {
let span = Span::sized(lit.span.start + 1, 1);
fixer.replace(span, "[=]")