-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lint): Add lint for usage of node globals (with autofix) (#25048)
From upgrading `deno_lint`. Previously if you had a node project that used a bunch of node globals (`process.env`, etc), you would have to fix the errors by hand. This PR includes a new lint that detects usages of node globals (`process`, `setImmediate`, `Buffer`, etc.) and provides an autofix to import the correct value. For instance: ```ts // main.ts const _foo = process.env.FOO; ``` `deno lint` gives you ```ts error[no-node-globals]: NodeJS globals are not available in Deno --> /home/foo.ts:1:14 | 1 | const _foo = process.env.FOO; | ^^^^^^^ = hint: Add `import process from "node:process";` docs: https://lint.deno.land/rules/no-node-globals Found 1 problem (1 fixable via --fix) Checked 1 file ``` And `deno lint --fix` adds the import for you: ```ts // main.ts import process from "node:process"; const _foo = process.env.FOO; ```
- Loading branch information
1 parent
e8d57cd
commit 5ec3c5c
Showing
7 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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
23 changes: 23 additions & 0 deletions
23
tests/specs/lint/node_globals_no_duplicate_imports/__test__.jsonc
This file contains 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,23 @@ | ||
{ | ||
"tempDir": true, | ||
"steps": [ | ||
{ | ||
"args": "run main.ts", | ||
"output": "error.out", | ||
"exitCode": 1 | ||
}, | ||
{ | ||
"args": "lint main.ts", | ||
"output": "lint.out", | ||
"exitCode": 1 | ||
}, | ||
{ | ||
"args": "lint --fix main.ts", | ||
"output": "Checked 1 file\n" | ||
}, | ||
{ | ||
"args": "run --allow-env main.ts", | ||
"output": "" | ||
} | ||
] | ||
} |
This file contains 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,4 @@ | ||
error: Uncaught (in promise) ReferenceError: process is not defined | ||
const _foo = process.env.FOO; | ||
^ | ||
at [WILDCARD]main.ts:3:14 |
22 changes: 22 additions & 0 deletions
22
tests/specs/lint/node_globals_no_duplicate_imports/lint.out
This file contains 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,22 @@ | ||
error[no-node-globals]: NodeJS globals are not available in Deno | ||
--> [WILDCARD]main.ts:3:14 | ||
| | ||
3 | const _foo = process.env.FOO; | ||
| ^^^^^^^ | ||
= hint: Add `import process from "node:process";` | ||
|
||
docs: https://lint.deno.land/rules/no-node-globals | ||
|
||
|
||
error[no-node-globals]: NodeJS globals are not available in Deno | ||
--> [WILDCARD]main.ts:7:14 | ||
| | ||
7 | const _bar = process.env.BAR; | ||
| ^^^^^^^ | ||
= hint: Add `import process from "node:process";` | ||
|
||
docs: https://lint.deno.land/rules/no-node-globals | ||
|
||
|
||
Found 2 problems (2 fixable via --fix) | ||
Checked 1 file |
This file contains 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,7 @@ | ||
import {} from "node:console"; | ||
|
||
const _foo = process.env.FOO; | ||
|
||
import {} from "node:assert"; | ||
|
||
const _bar = process.env.BAR; |