-
Notifications
You must be signed in to change notification settings - Fork 181
feat: add no-import-prefix rule #1361
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6754763
feat: add no-import-prefix rule
marvinhagemeister 8b0a1d3
Merge branch 'main' into no-import-prefix
dsherret 8f4e1a1
remove rules
dsherret 51660dc
add workspace tag
dsherret 4173633
fix
dsherret eafa4dc
update lint rule tags
dsherret 8b1b5b1
update message
dsherret 43383fa
update message again
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
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "enum": ["fresh", "jsr", "jsx", "react", "recommended"] | ||
| "enum": ["fresh", "jsr", "jsx", "react", "recommended", "workspace"] | ||
| } |
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,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()) { | ||
| 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()) { | ||
| 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 | ||
| }], | ||
| } | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.