use super::Context;
use super::LintRule;
use swc_ecmascript::ast::Regex;
use swc_ecmascript::visit::noop_visit_type;
use swc_ecmascript::visit::Node;
use swc_ecmascript::visit::Visit;
use std::sync::Arc;
pub struct NoEmptyCharacterClass;
impl LintRule for NoEmptyCharacterClass {
fn new() -> Box<Self> {
Box::new(NoEmptyCharacterClass)
}
fn code(&self) -> &'static str {
"no-empty-character-class"
}
fn lint_module(
&self,
context: Arc<Context>,
module: &swc_ecmascript::ast::Module,
) {
let mut visitor = NoEmptyCharacterClassVisitor::new(context);
visitor.visit_module(module, module);
}
}
struct NoEmptyCharacterClassVisitor {
context: Arc<Context>,
}
impl NoEmptyCharacterClassVisitor {
fn new(context: Arc<Context>) -> Self {
Self { context }
}
}
impl Visit for NoEmptyCharacterClassVisitor {
noop_visit_type!();
fn visit_regex(&mut self, regex: &Regex, _parent: &dyn Node) {
let raw_regex = self
.context
.source_map
.span_to_snippet(regex.span)
.expect("error in loading snippet");
lazy_static! {
static ref RULE_REGEX: regex::Regex = regex::Regex::new(
r"(?u)^/([^\\\[]|\\.|\[([^\\\]]|\\.)+\])*/[gimuys]*$"
)
.unwrap();
}
if !RULE_REGEX.is_match(&raw_regex) {
self.context.add_diagnostic(
regex.span,
"no-empty-character-class",
"empty character class in RegExp is not allowed",
);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::*;
#[test]
fn no_empty_character_class() {
assert_lint_err::<NoEmptyCharacterClass>(r#"const foo = /^abc[]/;"#, 12);
assert_lint_err::<NoEmptyCharacterClass>(r#"const foo = /foo[]bar/;"#, 12);
assert_lint_err::<NoEmptyCharacterClass>(r#"const foo = /[]]/;"#, 12);
assert_lint_err::<NoEmptyCharacterClass>(r#"const foo = /\[[]/;"#, 12);
assert_lint_err::<NoEmptyCharacterClass>(
r#"const foo = /\\[\\[\\]a-z[]/;"#,
12,
);
}
#[test]
fn no_empty_character_class_match() {
assert_lint_err::<NoEmptyCharacterClass>(r#"/^abc[]/.test("abcdefg");"#, 0);
assert_lint_err::<NoEmptyCharacterClass>(
r#"if (foo.match(/^abc[]/)) {}"#,
14,
);
}
#[test]
fn no_empty_character_class_test() {
assert_lint_err::<NoEmptyCharacterClass>(
r#""abcdefg".match(/^abc[]/);"#,
16,
);
assert_lint_err::<NoEmptyCharacterClass>(
r#"if (/^abc[]/.test(foo)) {}"#,
4,
);
}
#[test]
fn no_empty_character_class_valid() {
assert_lint_ok::<NoEmptyCharacterClass>(
r#"
const foo = /^abc[a-zA-Z]/;
const regExp = new RegExp("^abc[]");
const foo = /^abc/;
const foo = /[\\[]/;
const foo = /[\\]]/;
const foo = /[a-zA-Z\\[]/;
const foo = /[[]/;
const foo = /[\\[a-z[]]/;
const foo = /[\-\[\]\/\{\}\(\)\*\+\?\.\\^\$\|]/g;
const foo = /\[/g;
const foo = /\]/i;
"#,
);
}
}