Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions docs/rules/node_builtin_specifier.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Enforces the use of the `node:` specifier for Node built-in modules.

Deno requires Node built-in modules to be imported with the `node:` specifier.

### Invalid:

```typescript
import * as path from "path";
import * as fs from "fs";
import * as fsPromises from "fs/promises";
```

### Valid:

```typescript
import * as path from "node:path";
import * as fs from "node:fs";
import * as fsPromises from "node:fs/promises";
```
2 changes: 2 additions & 0 deletions src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub mod no_var;
pub mod no_window;
pub mod no_window_prefix;
pub mod no_with;
pub mod node_builtin_specifier;
pub mod prefer_as_const;
pub mod prefer_ascii;
pub mod prefer_const;
Expand Down Expand Up @@ -330,6 +331,7 @@ fn get_all_rules_raw() -> Vec<Box<dyn LintRule>> {
Box::new(no_window::NoWindow),
Box::new(no_window_prefix::NoWindowPrefix),
Box::new(no_with::NoWith),
Box::new(node_builtin_specifier::NodeBuiltinsSpecifier),
Box::new(prefer_as_const::PreferAsConst),
Box::new(prefer_ascii::PreferAscii),
Box::new(prefer_const::PreferConst),
Expand Down
231 changes: 231 additions & 0 deletions src/rules/node_builtin_specifier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
// Copyright 2020-2021 the Deno authors. All rights reserved. MIT license.
use super::Context;
use super::LintRule;
use crate::diagnostic::LintFix;
use crate::diagnostic::LintFixChange;
use crate::handler::Handler;
use crate::handler::Traverse;
use crate::Program;

use deno_ast::view as ast_view;
use deno_ast::SourceRange;
use deno_ast::SourceRanged;

#[derive(Debug)]
pub struct NodeBuiltinsSpecifier;

const CODE: &str = "node-builtin-specifier";
Copy link
Member

Choose a reason for hiding this comment

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

Maybe require-node-prefix or require-node-scheme or something like that?

const MESSAGE: &str = "built-in Node modules need the \"node:\" specifier";
const HINT: &str = "Add \"node:\" prefix in front of the import specifier";
const FIX_DESC: &str = "Add \"node:\" prefix";

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

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

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

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

struct NodeBuiltinsSpecifierGlobalHandler;

impl NodeBuiltinsSpecifierGlobalHandler {
fn add_diagnostic(&self, ctx: &mut Context, src: &str, range: SourceRange) {
let specifier = format!(r#""node:{}""#, src);

ctx.add_diagnostic_with_fixes(
range,
CODE,
MESSAGE,
Some(HINT.to_string()),
vec![LintFix {
description: FIX_DESC.into(),
changes: vec![LintFixChange {
new_text: specifier.into(),
range,
}],
}],
);
}
}

impl Handler for NodeBuiltinsSpecifierGlobalHandler {
fn import_decl(&mut self, decl: &ast_view::ImportDecl, ctx: &mut Context) {
let src = decl.src.inner.value.as_str();
if is_bare_node_builtin(src) {
self.add_diagnostic(ctx, src, decl.src.range());
}
}

fn call_expr(&mut self, expr: &ast_view::CallExpr, ctx: &mut Context) {
if let ast_view::Callee::Import(_) = expr.callee {
if let Some(src_expr) = expr.args.first() {
if let ast_view::Expr::Lit(lit) = src_expr.expr {
if let ast_view::Lit::Str(str_value) = lit {
let src = str_value.inner.value.as_str();
if is_bare_node_builtin(src) {
self.add_diagnostic(ctx, src, lit.range());
}
}
}
}
}
}
}

// Should match https://nodejs.org/api/module.html#modulebuiltinmodules
fn is_bare_node_builtin(src: &str) -> bool {
matches!(
src,
"assert"
| "assert/strict"
| "async_hooks"
| "buffer"
| "child_process"
| "cluster"
| "console"
| "constants"
| "crypto"
| "dgram"
| "diagnostics_channel"
| "dns"
| "dns/promises"
| "domain"
| "events"
| "fs"
| "fs/promises"
| "http"
| "http2"
| "https"
| "inspector"
| "inspector/promises"
| "module"
| "net"
| "os"
| "path"
| "path/posix"
| "path/win32"
| "perf_hooks"
| "process"
| "punycode"
| "querystring"
| "readline"
| "readline/promises"
| "repl"
| "stream"
| "stream/consumers"
| "stream/promises"
| "stream/web"
| "string_decoder"
| "sys"
| "timers"
| "timers/promises"
| "tls"
| "trace_events"
| "tty"
| "url"
| "util"
| "util/types"
| "v8"
| "vm"
| "wasi"
| "worker_threads"
| "zlib"
)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn node_specifier_valid() {
assert_lint_ok! {
NodeBuiltinsSpecifier,
r#"import "node:path";"#,
r#"import "node:fs";"#,
r#"import "node:fs/promises";"#,

r#"import * as fs from "node:fs";"#,
r#"import * as fsPromises from "node:fs/promises";"#,
r#"import fsPromises from "node:fs/promises";"#,

r#"await import("node:fs");"#,
r#"await import("node:fs/promises");"#,
};
}

#[test]
fn node_specifier_invalid() {
assert_lint_err! {
NodeBuiltinsSpecifier,
MESSAGE,
HINT,
r#"import "path";"#: [
{
col: 7,
fix: (FIX_DESC, r#"import "node:path";"#),
}
],
r#"import "fs";"#: [
{
col: 7,
fix: (FIX_DESC, r#"import "node:fs";"#),
}
],
r#"import "fs/promises";"#: [
{
col: 7,
fix: (FIX_DESC, r#"import "node:fs/promises";"#),
}
],

r#"import * as fs from "fs";"#: [
{
col: 20,
fix: (FIX_DESC, r#"import * as fs from "node:fs";"#),
}
],
r#"import * as fsPromises from "fs/promises";"#: [
{
col: 28,
fix: (FIX_DESC, r#"import * as fsPromises from "node:fs/promises";"#),
}
],
r#"import fsPromises from "fs/promises";"#: [
{
col: 23,
fix: (FIX_DESC, r#"import fsPromises from "node:fs/promises";"#),
}
],

r#"await import("fs");"#: [
{
col: 13,
fix: (FIX_DESC, r#"await import("node:fs");"#),
}
],
r#"await import("fs/promises");"#: [
{
col: 13,
fix: (FIX_DESC, r#"await import("node:fs/promises");"#),
}
]
};
}
}
7 changes: 7 additions & 0 deletions www/static/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,13 @@
"recommended"
]
},
{
"code": "node-builtin-specifier",
"docs": "Enforces the use of the `node:` specifier for Node built-in modules.\n\nDeno requires Node built-in modules to be imported with the `node:` specifier.\n\n### Invalid:\n\n```typescript\nimport * as path from \"path\";\nimport * as fs from \"fs\";\nimport * as fsPromises from \"fs/promises\";\n```\n\n### Valid:\n\n```typescript\nimport * as path from \"node:path\";\nimport * as fs from \"node:fs\";\nimport * as fsPromises from \"node:fs/promises\";\n```\n",
"tags": [
"recommended"
]
},
{
"code": "prefer-as-const",
"docs": "Recommends using const assertion (`as const`) over explicitly specifying literal\ntypes or using type assertion.\n\nWhen declaring a new variable of a primitive literal type, there are three ways:\n\n1. adding an explicit type annotation\n2. using normal type assertion (like `as \"foo\"`, or `<\"foo\">`)\n3. using const assertion (`as const`)\n\nThis lint rule suggests using const assertion because it will generally lead to\na safer code. For more details about const assertion, see\n[the official handbook](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions).\n\n### Invalid:\n\n```typescript\nlet a: 2 = 2; // type annotation\nlet b = 2 as 2; // type assertion\nlet c = <2> 2; // type assertion\nlet d = { foo: 1 as 1 }; // type assertion\n```\n\n### Valid:\n\n```typescript\nlet a = 2 as const;\nlet b = 2 as const;\nlet c = 2 as const;\nlet d = { foo: 1 as const };\n\nlet x = 2;\nlet y: string = \"hello\";\nlet z: number = someVariable;\n```\n",
Expand Down