|
| 1 | +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. |
| 2 | + |
| 3 | +use super::{Context, LintRule}; |
| 4 | +use crate::handler::{Handler, Traverse}; |
| 5 | +use crate::tags::{Tags, RECOMMENDED}; |
| 6 | +use crate::Program; |
| 7 | +use deno_ast::view::{CallExpr, Callee, Expr, ImportDecl, Lit}; |
| 8 | +use deno_ast::SourceRanged; |
| 9 | + |
| 10 | +#[derive(Debug)] |
| 11 | +pub struct NoUnversionedImport; |
| 12 | + |
| 13 | +const CODE: &str = "no-unversioned-import"; |
| 14 | +const MESSAGE: &str = "Missing version in specifier"; |
| 15 | +const HINT: &str = "Add a version requirement after the package name"; |
| 16 | + |
| 17 | +impl LintRule for NoUnversionedImport { |
| 18 | + fn tags(&self) -> Tags { |
| 19 | + &[RECOMMENDED] |
| 20 | + } |
| 21 | + |
| 22 | + fn code(&self) -> &'static str { |
| 23 | + CODE |
| 24 | + } |
| 25 | + |
| 26 | + fn lint_program_with_ast_view( |
| 27 | + &self, |
| 28 | + context: &mut Context, |
| 29 | + program: Program<'_>, |
| 30 | + ) { |
| 31 | + NoUnversionedImportHandler.traverse(program, context); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +struct NoUnversionedImportHandler; |
| 36 | + |
| 37 | +impl Handler for NoUnversionedImportHandler { |
| 38 | + fn import_decl(&mut self, node: &ImportDecl, ctx: &mut Context) { |
| 39 | + if is_unversioned(node.src.value()) { |
| 40 | + ctx.add_diagnostic_with_hint(node.src.range(), CODE, MESSAGE, HINT); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + fn call_expr(&mut self, node: &CallExpr, ctx: &mut Context) { |
| 45 | + if let Callee::Import(_) = node.callee { |
| 46 | + if let Some(arg) = node.args.first() { |
| 47 | + if let Expr::Lit(Lit::Str(lit)) = arg.expr { |
| 48 | + if is_unversioned(lit.value()) { |
| 49 | + ctx.add_diagnostic_with_hint(arg.range(), CODE, MESSAGE, HINT); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +fn is_unversioned(s: &str) -> bool { |
| 58 | + if let Some(req_ref) = get_package_req_ref(s) { |
| 59 | + req_ref.req.version_req.version_text() == "*" |
| 60 | + } else { |
| 61 | + false |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +fn get_package_req_ref( |
| 66 | + s: &str, |
| 67 | +) -> Option<deno_semver::package::PackageReqReference> { |
| 68 | + if let Ok(req_ref) = deno_semver::npm::NpmPackageReqReference::from_str(s) { |
| 69 | + Some(req_ref.into_inner()) |
| 70 | + } else if let Ok(req_ref) = |
| 71 | + deno_semver::jsr::JsrPackageReqReference::from_str(s) |
| 72 | + { |
| 73 | + Some(req_ref.into_inner()) |
| 74 | + } else { |
| 75 | + None |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod tests { |
| 81 | + use super::*; |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn no_with_valid() { |
| 85 | + assert_lint_ok! { |
| 86 | + NoUnversionedImport, |
| 87 | + r#"import foo from "foo";"#, |
| 88 | + r#"import foo from "@foo/bar";"#, |
| 89 | + r#"import foo from "./foo";"#, |
| 90 | + r#"import foo from "../foo";"#, |
| 91 | + r#"import foo from "~/foo";"#, |
| 92 | + r#"import foo from "npm:[email protected]";"#, |
| 93 | + r#"import foo from "npm:foo@latest";"#, |
| 94 | + r#"import foo from "npm:foo@^1.2.3";"#, |
| 95 | + r#"import foo from "npm:@foo/[email protected]";"#, |
| 96 | + r#"import foo from "npm:@foo/bar@^1.2.3";"#, |
| 97 | + r#"import foo from "jsr:@foo/[email protected]";"#, |
| 98 | + r#"import foo from "jsr:@foo/bar@^1.2.3";"#, |
| 99 | + r#"import("foo")"#, |
| 100 | + r#"import("@foo/bar")"#, |
| 101 | + r#"import("./foo")"#, |
| 102 | + r#"import("../foo")"#, |
| 103 | + r#"import("~/foo")"#, |
| 104 | + r#"import("npm:[email protected]")"#, |
| 105 | + r#"import("npm:foo@^1.2.3")"#, |
| 106 | + r#"import("npm:@foo/[email protected]")"#, |
| 107 | + r#"import("npm:@foo/bar@^1.2.3")"#, |
| 108 | + r#"import("jsr:@foo/[email protected]")"#, |
| 109 | + r#"import("jsr:@foo/bar@^1.2.3")"#, |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn no_with_invalid() { |
| 115 | + assert_lint_err! { |
| 116 | + NoUnversionedImport, |
| 117 | + r#"import foo from "jsr:@foo/foo";"#: [{ |
| 118 | + col: 16, |
| 119 | + message: MESSAGE, |
| 120 | + hint: HINT |
| 121 | + }], |
| 122 | + r#"import foo from "npm:foo";"#: [{ |
| 123 | + col: 16, |
| 124 | + message: MESSAGE, |
| 125 | + hint: HINT |
| 126 | + }], |
| 127 | + r#"import foo from "npm:@foo/bar";"#: [{ |
| 128 | + col: 16, |
| 129 | + message: MESSAGE, |
| 130 | + hint: HINT |
| 131 | + }], |
| 132 | + r#"import("jsr:@foo/foo");"#: [{ |
| 133 | + col: 7, |
| 134 | + message: MESSAGE, |
| 135 | + hint: HINT |
| 136 | + }], |
| 137 | + r#"import("npm:foo");"#: [{ |
| 138 | + col: 7, |
| 139 | + message: MESSAGE, |
| 140 | + hint: HINT |
| 141 | + }], |
| 142 | + r#"import("npm:@foo/bar");"#: [{ |
| 143 | + col: 7, |
| 144 | + message: MESSAGE, |
| 145 | + hint: HINT |
| 146 | + }], |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments