-
Notifications
You must be signed in to change notification settings - Fork 181
feat: add no-unversioned-import rule #1362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7519574
feat: add no-unversioned-import rule
marvinhagemeister cd7c600
Merge branch 'main' into no-unversioned-import
dsherret 3d8723c
remove
dsherret 5c7c94f
use deno_semver
dsherret 0f0a85f
update to be more precise
dsherret fa74ee3
format
dsherret File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| Ensure that inline dependency imports have a version specifier. | ||
|
|
||
| ### Invalid: | ||
|
|
||
| ```ts | ||
| import foo from "npm:chalk"; | ||
| import foo from "jsr:@std/path"; | ||
| ``` | ||
|
|
||
| ### Valid: | ||
|
|
||
| ```ts | ||
| import foo from "npm:[email protected]"; | ||
| import foo from "npm:chalk@^5.3.0"; | ||
| import foo from "jsr:@std/[email protected]"; | ||
| import foo from "jsr:@std/path@^1.0.8"; | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| // 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, ImportDecl, Lit}; | ||
| use deno_ast::SourceRanged; | ||
| use once_cell::sync::Lazy; | ||
| use regex::Regex; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct NoUnversionedImport; | ||
|
|
||
| const CODE: &str = "no-unversioned-import"; | ||
| const MESSAGE: &str = "Missing version in specifier"; | ||
| const HINT: &str = "Add a version at the end"; | ||
|
|
||
| impl LintRule for NoUnversionedImport { | ||
| fn tags(&self) -> &'static [&'static str] { | ||
| &[] | ||
| } | ||
|
|
||
| fn code(&self) -> &'static str { | ||
| CODE | ||
| } | ||
|
|
||
| fn lint_program_with_ast_view( | ||
| &self, | ||
| context: &mut Context, | ||
| program: Program<'_>, | ||
| ) { | ||
| NoUnversionedImportHandler.traverse(program, context); | ||
| } | ||
|
|
||
| #[cfg(feature = "docs")] | ||
| fn docs(&self) -> &'static str { | ||
| include_str!("../../docs/rules/no_unversioned_import.md") | ||
| } | ||
| } | ||
|
|
||
| struct NoUnversionedImportHandler; | ||
|
|
||
| impl Handler for NoUnversionedImportHandler { | ||
| fn import_decl(&mut self, node: &ImportDecl, ctx: &mut Context) { | ||
| if is_unversioned(node.src.value()) { | ||
| 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_unversioned(lit.value()) { | ||
| ctx.add_diagnostic_with_hint(arg.range(), CODE, MESSAGE, HINT); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static NPM_REG: Lazy<Regex> = | ||
| Lazy::new(|| Regex::new(r"^npm:(@.+\/[^@]+|[^@]+)$").unwrap()); | ||
| static JSR_REG: Lazy<Regex> = | ||
| Lazy::new(|| Regex::new(r"^jsr:@.+\/[^@]+$").unwrap()); | ||
|
|
||
| fn is_unversioned(s: &str) -> bool { | ||
| if s.starts_with("npm:") { | ||
| return NPM_REG.is_match(s); | ||
| } else if s.starts_with("jsr:") { | ||
| return JSR_REG.is_match(s); | ||
| } | ||
|
|
||
| false | ||
| } | ||
dsherret marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn no_with_valid() { | ||
| assert_lint_ok! { | ||
| NoUnversionedImport, | ||
| 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 from "npm:[email protected]";"#, | ||
| r#"import foo from "npm:foo@^1.2.3";"#, | ||
| r#"import foo from "npm:@foo/[email protected]";"#, | ||
| r#"import foo from "npm:@foo/bar@^1.2.3";"#, | ||
| r#"import foo from "jsr:@foo/[email protected]";"#, | ||
| r#"import foo from "jsr:@foo/bar@^1.2.3";"#, | ||
| r#"import("foo")"#, | ||
| r#"import("@foo/bar")"#, | ||
| r#"import("./foo")"#, | ||
| r#"import("../foo")"#, | ||
| r#"import("~/foo")"#, | ||
| r#"import("npm:[email protected]")"#, | ||
| r#"import("npm:foo@^1.2.3")"#, | ||
| r#"import("npm:@foo/[email protected]")"#, | ||
| r#"import("npm:@foo/bar@^1.2.3")"#, | ||
| r#"import("jsr:@foo/[email protected]")"#, | ||
| r#"import("jsr:@foo/bar@^1.2.3")"#, | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn no_with_invalid() { | ||
| assert_lint_err! { | ||
| NoUnversionedImport, | ||
| 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 "npm:@foo/bar";"#: [{ | ||
| 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("npm:@foo/bar");"#: [{ | ||
| col: 7, | ||
| message: MESSAGE, | ||
| hint: HINT | ||
| }], | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -576,6 +576,11 @@ | |
| "recommended" | ||
| ] | ||
| }, | ||
| { | ||
| "code": "no-unversioned-import", | ||
| "docs": "Ensure that inline dependency imports have a version specifier.\n\n### Invalid:\n\n```ts\nimport foo from \"npm:chalk\";\nimport foo from \"jsr:@std/path\";\n```\n\n### Valid:\n\n```ts\nimport foo from \"npm:[email protected]\";\nimport foo from \"npm:chalk@^5.3.0\";\nimport foo from \"jsr:@std/[email protected]\";\nimport foo from \"jsr:@std/path@^1.0.8\";\n```\n", | ||
| "tags": [] | ||
| }, | ||
| { | ||
| "code": "no-var", | ||
| "docs": "Enforces the use of block scoped variables over more error prone function scoped\nvariables. Block scoped variables are defined using `const` and `let` keywords.\n\n`const` and `let` keywords ensure the variables defined using these keywords are\nnot accessible outside their block scope. On the other hand, variables defined\nusing `var` keyword are only limited by their function scope.\n\n### Invalid:\n\n```typescript\nvar foo = \"bar\";\n```\n\n### Valid:\n\n```typescript\nconst foo = 1;\nlet bar = 2;\n```\n", | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted to denoland/docs#2500