Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/rules/no_import_prefix.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Ensure that all dependencies are declared in either `deno.json` or
`package.json`.

### Invalid:

```ts
import foo from "https://deno.land/std/path/mod.ts";
import foo from "jsr:@std/path";
import foo from "npm:preact";
```

### Valid:

```ts
// Mapped in `deno.json` or `package.json`
import foo from "@std/path";
```
1 change: 1 addition & 0 deletions schemas/rules.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"no-implicit-declare-namespace-export",
"no-import-assertions",
"no-import-assign",
"no-import-prefix",
"no-inferrable-types",
"no-inner-declarations",
"no-invalid-regexp",
Expand Down
2 changes: 2 additions & 0 deletions src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub mod no_global_assign;
pub mod no_implicit_declare_namespace_export;
pub mod no_import_assertions;
pub mod no_import_assign;
pub mod no_import_prefix;
pub mod no_inferrable_types;
pub mod no_inner_declarations;
pub mod no_invalid_regexp;
Expand Down Expand Up @@ -295,6 +296,7 @@ fn get_all_rules_raw() -> Vec<Box<dyn LintRule>> {
),
Box::new(no_import_assertions::NoImportAssertions),
Box::new(no_import_assign::NoImportAssign),
Box::new(no_import_prefix::NoImportPrefix),
Box::new(no_inferrable_types::NoInferrableTypes),
Box::new(no_inner_declarations::NoInnerDeclarations),
Box::new(no_invalid_regexp::NoInvalidRegexp),
Expand Down
136 changes: 136 additions & 0 deletions src/rules/no_import_prefix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// 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;

#[derive(Debug)]
pub struct NoImportPrefix;

const CODE: &str = "no-import-prefix";
const MESSAGE: &str =
"Inline 'npm:*', 'jsr:*' or 'http:*' dependency not allowed";
const HINT: &str = "Add it to the 'imports' section in 'deno.json' instead";

impl LintRule for NoImportPrefix {
fn tags(&self) -> &'static [&'static str] {
&[]
}

fn code(&self) -> &'static str {
CODE
}

fn lint_program_with_ast_view(
&self,
context: &mut Context,
program: Program<'_>,
) {
NoImportPrefixHandler.traverse(program, context);
}

#[cfg(feature = "docs")]
fn docs(&self) -> &'static str {
include_str!("../../docs/rules/no_import_prefix.md")
}
}

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
}],
}
}
}
5 changes: 5 additions & 0 deletions www/static/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@
"recommended"
]
},
{
"code": "no-import-prefix",
"docs": "Ensure that all dependencies are declared in either `deno.json` or\n`package.json`.\n\n### Invalid:\n\n```ts\nimport foo from \"https://deno.land/std/path/mod.ts\";\nimport foo from \"jsr:@std/path\";\nimport foo from \"npm:preact\";\n```\n\n### Valid:\n\n```ts\n// Mapped in `deno.json` or `package.json`\nimport foo from \"@std/path\";\n```\n",
"tags": []
},
{
"code": "no-inferrable-types",
"docs": "Disallows easily inferrable types\n\nVariable initializations to JavaScript primitives (and `null`) are obvious in\ntheir type. Specifying their type can add additional verbosity to the code. For\nexample, with `const x: number = 5`, specifying `number` is unnecessary as it is\nobvious that `5` is a number.\n\n### Invalid:\n\n```typescript\nconst a: bigint = 10n;\nconst b: bigint = BigInt(10);\nconst c: boolean = true;\nconst d: boolean = !0;\nconst e: number = 10;\nconst f: number = Number(\"1\");\nconst g: number = Infinity;\nconst h: number = NaN;\nconst i: null = null;\nconst j: RegExp = /a/;\nconst k: RegExp = RegExp(\"a\");\nconst l: RegExp = new RegExp(\"a\");\nconst m: string = \"str\";\nconst n: string = `str`;\nconst o: string = String(1);\nconst p: symbol = Symbol(\"a\");\nconst q: undefined = undefined;\nconst r: undefined = void someValue;\n\nclass Foo {\n prop: number = 5;\n}\n\nfunction fn(s: number = 5, t: boolean = true) {}\n```\n\n### Valid:\n\n```typescript\nconst a = 10n;\nconst b = BigInt(10);\nconst c = true;\nconst d = !0;\nconst e = 10;\nconst f = Number(\"1\");\nconst g = Infinity;\nconst h = NaN;\nconst i = null;\nconst j = /a/;\nconst k = RegExp(\"a\");\nconst l = new RegExp(\"a\");\nconst m = \"str\";\nconst n = `str`;\nconst o = String(1);\nconst p = Symbol(\"a\");\nconst q = undefined;\nconst r = void someValue;\n\nclass Foo {\n prop = 5;\n}\n\nfunction fn(s = 5, t = true) {}\n```\n",
Expand Down
Loading