Skip to content

Commit

Permalink
refactor(linter): use "parsed pattern" in no_div_regex rule. (#5417)
Browse files Browse the repository at this point in the history
Part of #5416, Paves the road for upcoming refactors by adding the `oxc_regular_expression` dependency and a helper method for ease of access.
  • Loading branch information
rzvxa committed Sep 4, 2024
1 parent ba4b68c commit df7eddd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions crates/oxc_ast/src/ast_impl/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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> {
Expand Down
25 changes: 13 additions & 12 deletions crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
10 changes: 9 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_div_regex.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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, "[=]")
Expand Down

0 comments on commit df7eddd

Please sign in to comment.