diff options
author | Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> | 2024-08-15 13:43:04 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-15 20:43:04 +0000 |
commit | 5ec3c5c3a46ca95f355a4520676b85ac619ca102 (patch) | |
tree | f7c75855e744a73be6912c3bfc38469b86ba4581 /tests/specs | |
parent | e8d57cd3feb169c6a8e9cb11d96c0f2d0b7d50f8 (diff) |
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;
```
Diffstat (limited to 'tests/specs')
4 files changed, 56 insertions, 0 deletions
diff --git a/tests/specs/lint/node_globals_no_duplicate_imports/__test__.jsonc b/tests/specs/lint/node_globals_no_duplicate_imports/__test__.jsonc new file mode 100644 index 000000000..d8e00f47a --- /dev/null +++ b/tests/specs/lint/node_globals_no_duplicate_imports/__test__.jsonc @@ -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": "" + } + ] +} diff --git a/tests/specs/lint/node_globals_no_duplicate_imports/error.out b/tests/specs/lint/node_globals_no_duplicate_imports/error.out new file mode 100644 index 000000000..5671b17b4 --- /dev/null +++ b/tests/specs/lint/node_globals_no_duplicate_imports/error.out @@ -0,0 +1,4 @@ +error: Uncaught (in promise) ReferenceError: process is not defined +const _foo = process.env.FOO; + ^ + at [WILDCARD]main.ts:3:14 diff --git a/tests/specs/lint/node_globals_no_duplicate_imports/lint.out b/tests/specs/lint/node_globals_no_duplicate_imports/lint.out new file mode 100644 index 000000000..b396e71eb --- /dev/null +++ b/tests/specs/lint/node_globals_no_duplicate_imports/lint.out @@ -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 diff --git a/tests/specs/lint/node_globals_no_duplicate_imports/main.ts b/tests/specs/lint/node_globals_no_duplicate_imports/main.ts new file mode 100644 index 000000000..bff428d01 --- /dev/null +++ b/tests/specs/lint/node_globals_no_duplicate_imports/main.ts @@ -0,0 +1,7 @@ +import {} from "node:console"; + +const _foo = process.env.FOO; + +import {} from "node:assert"; + +const _bar = process.env.BAR; |