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
#![deny(warnings)]
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
pub mod diagnostic;
pub mod js_regex;
pub mod linter;
pub mod rules;
mod scopes;
pub mod swc_util;
#[cfg(test)]
mod test_util;
#[cfg(test)]
mod lint_tests {
use crate::diagnostic::LintDiagnostic;
use crate::linter::*;
use crate::rules::get_recommended_rules;
use crate::test_util::assert_diagnostic;
fn lint(
source: &str,
unknown_rules: bool,
unused_dir: bool,
) -> Vec<LintDiagnostic> {
let mut linter = LinterBuilder::default()
.lint_unknown_rules(unknown_rules)
.lint_unused_ignore_directives(unused_dir)
.rules(get_recommended_rules())
.build();
linter
.lint("lint_test.ts".to_string(), source.to_string())
.expect("Failed to lint")
}
#[test]
fn warn_unknown_rules() {
let diagnostics = lint(
r#"
// deno-lint-ignore some-rule
function foo() {
// deno-lint-ignore some-rule-2 some-rule-3
let bar_foo = true
}
"#,
true,
false,
);
assert_diagnostic(&diagnostics[0], "ban-unknown-rule-code", 2, 1);
assert_diagnostic(&diagnostics[1], "ban-unknown-rule-code", 4, 3);
}
#[test]
fn ignore_unknown_rules() {
let diagnostics = lint(
r#"
// deno-lint-ignore some-rule
function foo() {
// pass
}
"#,
false,
false,
);
assert_eq!(diagnostics.len(), 0);
}
#[test]
fn warn_unused_dir() {
let diagnostics = lint(
r#"
// deno-lint-ignore no-explicit-any
function bar(p: boolean) {
// deno-lint-ignore no-misused-new eqeqeq
let foo_bar = false
}
"#,
false,
true,
);
assert_diagnostic(&diagnostics[0], "ban-unused-ignore", 2, 1);
assert_diagnostic(&diagnostics[1], "ban-unused-ignore", 4, 3);
}
#[test]
fn ignore_unused_dir() {
let diagnostics = lint(
r#"
// deno-lint-ignore no-explicit-any
function bar(p: boolean) {
// pass
}
"#,
false,
false,
);
assert_eq!(diagnostics.len(), 0);
}
}