-
-
Notifications
You must be signed in to change notification settings - Fork 454
/
no_sparse_arrays.rs
110 lines (95 loc) · 3.88 KB
/
no_sparse_arrays.rs
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
use miette::{miette, LabeledSpan};
use oxc_ast::{ast::ArrayExpressionElement, AstKind};
use oxc_diagnostics::{
miette::{self, Diagnostic, Severity},
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{context::LintContext, rule::Rule, AstNode};
#[derive(Debug, Error, Diagnostic)]
#[error("eslint(no-sparse-arrays): Unexpected comma in middle of array")]
#[diagnostic(severity(warning), help("remove the comma or insert `undefined`"))]
struct NoSparseArraysDiagnostic(#[label] pub Span);
#[derive(Debug, Default, Clone)]
pub struct NoSparseArrays;
declare_oxc_lint!(
/// ### What it does
///
/// Disallow sparse arrays.
///
/// ### Why is this bad?
///
/// The confusion around sparse arrays is enough that it’s recommended to avoid using them unless you are certain that they are useful in your code.
///
/// ### Example
/// ```javascript
/// var items = [,,];
/// var colors = [ "red",, "blue" ];
/// ```
NoSparseArrays,
correctness
);
impl Rule for NoSparseArrays {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::ArrayExpression(array_expr) = node.kind() {
let violations = array_expr
.elements
.iter()
.filter_map(|el| match el {
ArrayExpressionElement::Elision(span) => Some(span),
_ => None,
})
.map(|span| {
LabeledSpan::at(
(span.start as usize)..(span.start as usize),
"unexpected comma",
)
})
.collect::<Vec<_>>();
if !violations.is_empty() {
if violations.len() < 10 {
ctx.diagnostic(miette!(
severity = Severity::Warning,
labels = violations,
help = "remove the comma or insert `undefined`",
"eslint(no-sparse-arrays): Unexpected comma in middle of array"
));
} else {
let span = if (array_expr.span.end - array_expr.span.start) < 50 {
LabeledSpan::at(array_expr.span, "the array here")
} else {
LabeledSpan::at(
(array_expr.span.start as usize)..(array_expr.span.start as usize),
"the array starting here",
)
};
ctx.diagnostic(miette!(
severity = Severity::Warning,
labels = vec![span],
help = "remove the comma or insert `undefined`",
"eslint(no-sparse-arrays): {} unexpected commas in middle of array",
violations.len()
));
}
}
}
}
}
#[test]
fn test() {
use crate::tester::Tester;
let pass = vec!["var a = [ 1, 2, ]"];
let fail = vec![
"var a = [,];",
"var a = [ 1,, 2];",
"var a = [ 1,,,, 2];",
"var a = [ 1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 2];",
"var a = [ 1, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , 2];",
"var a = [ 1, , , , , , , , , , , , , , , , , , , , , , , , , , hello, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , 2];",
"var a = [ 1, , , , , , , , , , , , , , , , , , , , , , , , , ,
hello, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , 2];",
];
Tester::new(NoSparseArrays::NAME, pass, fail).test_and_snapshot();
}