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
use super::Context;
use super::LintRule;
use swc_ecmascript::visit::noop_visit_type;
use swc_ecmascript::visit::Node;
use swc_ecmascript::visit::Visit;
pub struct NoSparseArrays;
impl LintRule for NoSparseArrays {
fn new() -> Box<Self> {
Box::new(NoSparseArrays)
}
fn code(&self) -> &'static str {
"no-sparse-arrays"
}
fn lint_module(
&self,
context: &mut Context,
module: &swc_ecmascript::ast::Module,
) {
let mut visitor = NoSparseArraysVisitor::new(context);
visitor.visit_module(module, module);
}
}
struct NoSparseArraysVisitor<'c> {
context: &'c mut Context,
}
impl<'c> NoSparseArraysVisitor<'c> {
fn new(context: &'c mut Context) -> Self {
Self { context }
}
}
impl<'c> Visit for NoSparseArraysVisitor<'c> {
noop_visit_type!();
fn visit_array_lit(
&mut self,
array_lit: &swc_ecmascript::ast::ArrayLit,
_parent: &dyn Node,
) {
if array_lit.elems.iter().any(|e| e.is_none()) {
self.context.add_diagnostic(
array_lit.span,
"no-sparse-arrays",
"Sparse arrays are not allowed",
);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::*;
#[test]
fn no_sparse_arrays_test() {
assert_lint_ok::<NoSparseArrays>("const sparseArray1 = [1,null,3];");
assert_lint_err::<NoSparseArrays>("const sparseArray = [1,,3];", 20);
}
}