1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2020 the Deno authors. All rights reserved. MIT license.
use super::Context;
use super::LintRule;
use regex::Regex;
use swc_common::comments::Comment;
use swc_common::comments::CommentKind;
use swc_common::Span;

pub struct BanUntaggedTodo;

impl BanUntaggedTodo {
  fn report(&self, context: &mut Context, span: Span) {
    context.add_diagnostic_with_hint(
      span,
      "ban-untagged-todo",
      "TODO should be tagged with (@username) or (#issue)",
      "Add a user tag, e.g. @djones, or issue reference, e.g. #123, to the TODO comment"
    );
  }
}

impl LintRule for BanUntaggedTodo {
  fn new() -> Box<Self> {
    Box::new(BanUntaggedTodo)
  }

  fn code(&self) -> &'static str {
    "ban-untagged-todo"
  }

  fn lint_module(
    &self,
    context: &mut Context,
    _module: &swc_ecmascript::ast::Module,
  ) {
    let mut violated_comment_spans = Vec::new();

    violated_comment_spans.extend(
      context.leading_comments.values().flatten().filter_map(|c| {
        if check_comment(c) {
          Some(c.span)
        } else {
          None
        }
      }),
    );
    violated_comment_spans.extend(
      context
        .trailing_comments
        .values()
        .flatten()
        .filter_map(|c| if check_comment(c) { Some(c.span) } else { None }),
    );

    for span in violated_comment_spans {
      self.report(context, span);
    }
  }

  fn docs(&self) -> &'static str {
    r#"Requires TODOs to be annotated with either a user tag (@user) or an issue reference (#issue).

TODOs without reference to a user or an issue become stale with no easy way to get more information.

### Valid:
```typescript
// TODO Improve calc engine (@djones)
export function calcValue(): number { }
```
```typescript
// TODO Improve calc engine (#332)
export function calcValue(): number { }
```

### Invalid:
```typescript
// TODO Improve calc engine
export function calcValue(): number { }
```"#
  }
}

/// Returns `true` if the comment should be reported.
fn check_comment(comment: &Comment) -> bool {
  if comment.kind != CommentKind::Line {
    return false;
  }

  let text = comment.text.to_lowercase();
  let text = text.trim_start();

  if !text.starts_with("todo") {
    return false;
  }

  lazy_static! {
    static ref TODO_RE: Regex = Regex::new(r#"todo\((#|@)\S+\)"#).unwrap();
  }

  if TODO_RE.is_match(text) {
    return false;
  }

  true
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::test_util::*;

  #[test]
  fn ban_ts_ignore() {
    assert_lint_ok_n::<BanUntaggedTodo>(vec![
      r#"
// TODO(#1234)
const b = "b";
      "#,
      r#"
// TODO(@someusername)
const c = "c";
      "#,
    ]);
    assert_lint_err_on_line::<BanUntaggedTodo>(
      r#"
// TODO
function foo() {
  // pass
}
      "#,
      2,
      0,
    );
    assert_lint_err_on_line::<BanUntaggedTodo>(
      r#"
// TODO(username)
const a = "a";
      "#,
      2,
      0,
    );
  }
}