forked from denoland/deno_lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno_import_prefix.rs
More file actions
132 lines (119 loc) · 3.14 KB
/
no_import_prefix.rs
File metadata and controls
132 lines (119 loc) · 3.14 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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::tags::{Tags, WORKSPACE};
use crate::Program;
use deno_ast::view::{CallExpr, Callee, Expr, ImportDecl, Lit};
use deno_ast::SourceRanged;
#[derive(Debug)]
pub struct NoImportPrefix;
const CODE: &str = "no-import-prefix";
const MESSAGE: &str =
"Inline 'npm:', 'jsr:' or 'https:' dependency not allowed";
const HINT: &str = "Add it as a dependency in a deno.json or package.json instead and reference it here via its bare specifier";
impl LintRule for NoImportPrefix {
fn tags(&self) -> Tags {
&[WORKSPACE]
}
fn code(&self) -> &'static str {
CODE
}
fn lint_program_with_ast_view(
&self,
context: &mut Context,
program: Program<'_>,
) {
NoImportPrefixHandler.traverse(program, context);
}
}
struct NoImportPrefixHandler;
impl Handler for NoImportPrefixHandler {
fn import_decl(&mut self, node: &ImportDecl, ctx: &mut Context) {
if is_non_bare(&node.src.value().to_string_lossy()) {
ctx.add_diagnostic_with_hint(node.src.range(), CODE, MESSAGE, HINT);
}
}
fn call_expr(&mut self, node: &CallExpr, ctx: &mut Context) {
if let Callee::Import(_) = node.callee {
if let Some(arg) = node.args.first() {
if let Expr::Lit(Lit::Str(lit)) = arg.expr {
if is_non_bare(&lit.value().to_string_lossy()) {
ctx.add_diagnostic_with_hint(arg.range(), CODE, MESSAGE, HINT);
}
}
}
}
}
}
fn is_non_bare(s: &str) -> bool {
s.starts_with("npm:")
|| s.starts_with("jsr:")
|| s.starts_with("http:")
|| s.starts_with("https:")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_with_valid() {
assert_lint_ok! {
NoImportPrefix,
r#"import foo from "foo";"#,
r#"import foo from "@foo/bar";"#,
r#"import foo from "./foo";"#,
r#"import foo from "../foo";"#,
r#"import foo from "~/foo";"#,
r#"import("foo")"#,
r#"import("@foo/bar")"#,
r#"import("./foo")"#,
r#"import("../foo")"#,
r#"import("~/foo")"#,
}
}
#[test]
fn no_with_invalid() {
assert_lint_err! {
NoImportPrefix,
r#"import foo from "jsr:@foo/foo";"#: [{
col: 16,
message: MESSAGE,
hint: HINT
}],
r#"import foo from "npm:foo";"#: [{
col: 16,
message: MESSAGE,
hint: HINT
}],
r#"import foo from "http://example.com/foo";"#: [{
col: 16,
message: MESSAGE,
hint: HINT
}],
r#"import foo from "https://example.com/foo";"#: [{
col: 16,
message: MESSAGE,
hint: HINT
}],
r#"import("jsr:@foo/foo");"#: [{
col: 7,
message: MESSAGE,
hint: HINT
}],
r#"import("npm:foo");"#: [{
col: 7,
message: MESSAGE,
hint: HINT
}],
r#"import("http://example.com/foo");"#: [{
col: 7,
message: MESSAGE,
hint: HINT
}],
r#"import("https://example.com/foo");"#: [{
col: 7,
message: MESSAGE,
hint: HINT
}],
}
}
}