forked from denoland/deno_lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno_import_assertions.rs
More file actions
148 lines (136 loc) · 3.86 KB
/
no_import_assertions.rs
File metadata and controls
148 lines (136 loc) · 3.86 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::tags::{self, Tags};
use crate::Program;
use deno_ast::swc::parser::token::{IdentLike, KnownIdent, Token, Word};
use deno_ast::view as ast_view;
use deno_ast::{SourceRanged, SourceRangedForSpanned};
use if_chain::if_chain;
#[derive(Debug)]
pub struct NoImportAssertions;
const CODE: &str = "no-import-assertions";
const MESSAGE: &str =
"The `assert` keyword is deprecated for import attributes";
const HINT: &str = "Instead use the `with` keyword";
impl LintRule for NoImportAssertions {
fn tags(&self) -> Tags {
&[tags::RECOMMENDED]
}
fn code(&self) -> &'static str {
CODE
}
fn lint_program_with_ast_view(
&self,
context: &mut Context,
program: Program<'_>,
) {
NoImportAssertionsHandler.traverse(program, context);
}
}
struct NoImportAssertionsHandler;
impl Handler for NoImportAssertionsHandler {
fn import_decl(
&mut self,
import_decl: &ast_view::ImportDecl,
ctx: &mut Context,
) {
if_chain! {
if let Some(with) = import_decl.with;
if let Some(prev_token_and_span) = with.start().previous_token_fast(ctx.program());
if let Token::Word(word) = &prev_token_and_span.token;
if let Word::Ident(ident_like) = word;
if let IdentLike::Known(known_ident) = ident_like;
if matches!(known_ident, KnownIdent::Assert);
then {
ctx.add_diagnostic_with_hint(
prev_token_and_span.span.range(),
CODE,
MESSAGE,
HINT,
);
}
}
}
fn call_expr(&mut self, call_expr: &ast_view::CallExpr, ctx: &mut Context) {
if_chain! {
if matches!(call_expr.callee, ast_view::Callee::Import(_));
if let Some(expr_or_spread) = call_expr.args.get(1);
if let ast_view::Expr::Object(object_lit) = expr_or_spread.expr;
then {
for prop_or_spread in object_lit.props.iter() {
if_chain! {
if let ast_view::PropOrSpread::Prop(prop) = prop_or_spread;
if let ast_view::Prop::KeyValue(key_value_prop) = prop;
then {
match key_value_prop.key {
ast_view::PropName::Ident(ident) => {
if ident.sym().as_ref() == "assert" {
ctx.add_diagnostic_with_hint(
ident.range(),
CODE,
MESSAGE,
HINT,
);
}
},
ast_view::PropName::Str(str) => {
if str.value().as_str() == Some("assert") {
ctx.add_diagnostic_with_hint(
str.range(),
CODE,
MESSAGE,
HINT,
);
}
}
_ => (),
}
}
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_import_assertions_valid() {
assert_lint_ok! {
NoImportAssertions,
r#"import foo from './foo.js';"#,
r#"import foo from './foo.js' with { bar: 'bar' };"#,
r#"import('./foo.js');"#,
r#"import('./foo.js', { with: { bar: 'bar' } });"#,
r#"import('./foo.js', { "with": { bar: 'bar' } });"#,
};
}
#[test]
fn no_import_assertions_invalid() {
assert_lint_err! {
NoImportAssertions,
MESSAGE,
HINT,
r#"import foo from './foo.js' assert { bar: 'bar' };"#: [
{
line: 1,
col: 27,
},
],
r#"import('./foo.js', { assert: { bar: 'bar' } });"#: [
{
line: 1,
col: 21,
},
],
r#"import('./foo.js', { "assert": { bar: 'bar' } });"#: [
{
line: 1,
col: 21,
},
],
};
}
}