/
no_with.rs
57 lines (46 loc) · 1.15 KB
/
no_with.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
// 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 as ast_view;
use deno_ast::SourceRanged;
#[derive(Debug)]
pub struct NoWith;
const CODE: &str = "no-with";
const MESSAGE: &str = "`with` statement is not allowed";
impl LintRule for NoWith {
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<'_>,
) {
NoWithHandler.traverse(program, context);
}
#[cfg(feature = "docs")]
fn docs(&self) -> &'static str {
include_str!("../../docs/rules/no_with.md")
}
}
struct NoWithHandler;
impl Handler for NoWithHandler {
fn with_stmt(&mut self, with_stmt: &ast_view::WithStmt, ctx: &mut Context) {
ctx.add_diagnostic(with_stmt.range(), CODE, MESSAGE);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_with_invalid() {
assert_lint_err! {
NoWith,
"with (someVar) { console.log('asdf'); }": [{ col: 0, message: MESSAGE }],
}
}
}