Skip to content

Commit

Permalink
Fix no-octal rule (denoland#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
disizali committed Jun 4, 2020
1 parent bbfccf2 commit 8ed884e
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/rules/no_octal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ impl NoOctalVisitor {

impl Visit for NoOctalVisitor {
fn visit_number(&mut self, literal_num: &Number, _parent: &dyn Node) {
lazy_static! {
static ref OCTAL: regex::Regex = regex::Regex::new(r"^0[0-9]").unwrap();
}

let raw_number = self
.context
.source_map
.span_to_snippet(literal_num.span)
.expect("error in loading snippet");

if raw_number != "0" && raw_number.starts_with('0') {
if OCTAL.is_match(&raw_number) {
self.context.add_diagnostic(
literal_num.span,
"no-octal",
Expand All @@ -66,17 +70,7 @@ mod tests {
}

#[test]
fn test_new_normal_number() {
assert_lint_ok::<NoOctal>("7");
}

#[test]
fn test_string_octal_number() {
assert_lint_ok::<NoOctal>("\"07\"");
}

#[test]
fn test_zero_number() {
assert_lint_ok::<NoOctal>("0");
fn test_octals_valid() {
assert_lint_ok_n::<NoOctal>(vec!["7", "\"07\"", "0x08", "-0.01"]);
}
}

0 comments on commit 8ed884e

Please sign in to comment.