-
Notifications
You must be signed in to change notification settings - Fork 162
/
no_array_constructor.rs
131 lines (116 loc) · 3.16 KB
/
no_array_constructor.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::Program;
use deno_ast::view::{CallExpr, Callee, Expr, ExprOrSpread, NewExpr};
use deno_ast::{SourceRange, SourceRanged};
#[derive(Debug)]
pub struct NoArrayConstructor;
const CODE: &str = "no-array-constructor";
const MESSAGE: &str = "Array Constructor is not allowed";
const HINT: &str = "Use array literal notation (e.g. []) or single argument specifying array size only (e.g. new Array(5)";
impl LintRule for NoArrayConstructor {
fn tags(&self) -> &'static [&'static str] {
&["recommended"]
}
fn code(&self) -> &'static str {
CODE
}
fn lint_program_with_ast_view(
&self,
context: &mut Context,
program: Program,
) {
NoArrayConstructorHandler.traverse(program, context);
}
#[cfg(feature = "docs")]
fn docs(&self) -> &'static str {
include_str!("../../docs/rules/no_array_constructor.md")
}
}
fn check_args(
args: Vec<&ExprOrSpread>,
range: SourceRange,
context: &mut Context,
) {
if args.len() != 1 {
context.add_diagnostic_with_hint(range, CODE, MESSAGE, HINT);
}
}
struct NoArrayConstructorHandler;
impl Handler for NoArrayConstructorHandler {
fn new_expr(&mut self, new_expr: &NewExpr, context: &mut Context) {
if let Expr::Ident(ident) = &new_expr.callee {
let name = ident.inner.as_ref();
if name != "Array" {
return;
}
if new_expr.type_args.is_some() {
return;
}
match &new_expr.args {
Some(args) => {
check_args(args.to_vec(), new_expr.range(), context);
}
None => check_args(vec![], new_expr.range(), context),
};
}
}
fn call_expr(&mut self, call_expr: &CallExpr, context: &mut Context) {
if let Callee::Expr(Expr::Ident(ident)) = &call_expr.callee {
let name = ident.inner.as_ref();
if name != "Array" {
return;
}
if call_expr.type_args.is_some() {
return;
}
check_args((*call_expr.args).to_vec(), call_expr.range(), context);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_array_constructor_valid() {
assert_lint_ok! {
NoArrayConstructor,
"Array(x)",
"Array(9)",
"Array.foo()",
"foo.Array()",
"new Array(x)",
"new Array(9)",
"new foo.Array()",
"new Array.foo",
"new Array<Foo>(1, 2, 3);",
"new Array<Foo>()",
"Array<Foo>(1, 2, 3);",
"Array<Foo>();",
};
}
#[test]
fn no_array_constructor_invalid() {
assert_lint_err! {
NoArrayConstructor,
"new Array": [{ col: 0, message: MESSAGE, hint: HINT }],
"new Array()": [{ col: 0, message: MESSAGE, hint: HINT }],
"new Array(x, y)": [{ col: 0, message: MESSAGE, hint: HINT }],
"new Array(0, 1, 2)": [{ col: 0, message: MESSAGE, hint: HINT }],
// nested
r#"
const a = new class {
foo() {
let arr = new Array();
}
}();
"#: [{ line: 4, col: 14, message: MESSAGE, hint: HINT }],
r#"
const a = (() => {
let arr = new Array();
})();
"#: [{ line: 3, col: 12, message: MESSAGE, hint: HINT }],
}
}
}