| 1 | import { assert, describe } from "@effect/vitest"; |
| 2 | |
| 3 | import { createOxlintRuleHarness } from "../test/utils.ts"; |
| 4 | |
| 5 | const rule = createOxlintRuleHarness("t3code/namespace-node-imports"); |
| 6 | |
| 7 | describe("t3code/namespace-node-imports", () => { |
| 8 | rule.valid( |
| 9 | "allows canonical Node namespaces", |
| 10 | ` |
| 11 | import * as NodeFS from "node:fs"; |
| 12 | import * as NodeFSP from "node:fs/promises"; |
| 13 | import * as NodeAssert from "node:assert/strict"; |
| 14 | import * as NodeChildProcess from "node:child_process"; |
| 15 | import * as NodeTimersPromises from "node:timers/promises"; |
| 16 | import type * as NodeStream from "node:stream"; |
| 17 | |
| 18 | NodeAssert.ok(NodeChildProcess.spawn && NodeTimersPromises.setTimeout); |
| 19 | export const read = NodeFS.readFileSync; |
| 20 | export const readAsync = NodeFSP.readFile; |
| 21 | export type Input = NodeStream.Readable; |
| 22 | `, |
| 23 | ); |
| 24 | |
| 25 | rule.valid( |
| 26 | "does not apply to non-Node packages", |
| 27 | ` |
| 28 | import { BrowserWindow } from "electron"; |
| 29 | `, |
| 30 | ); |
| 31 | |
| 32 | rule.invalid( |
| 33 | "reports named imports", |
| 34 | ` |
| 35 | import { readFile } from "node:fs/promises"; |
| 36 | `, |
| 37 | (output) => { |
| 38 | assert.match(output, /namespace named NodeFSP/); |
| 39 | }, |
| 40 | ); |
| 41 | |
| 42 | rule.invalid( |
| 43 | "reports default imports", |
| 44 | ` |
| 45 | import path from "node:path"; |
| 46 | `, |
| 47 | (output) => { |
| 48 | assert.match(output, /namespace named NodePath/); |
| 49 | }, |
| 50 | ); |
| 51 | |
| 52 | rule.invalid( |
| 53 | "reports non-canonical namespace aliases", |
| 54 | ` |
| 55 | import * as Crypto from "node:crypto"; |
| 56 | import * as NodeOs from "node:os"; |
| 57 | `, |
| 58 | (output) => { |
| 59 | assert.match(output, /namespace named NodeCrypto/); |
| 60 | assert.match(output, /namespace named NodeOS/); |
| 61 | }, |
| 62 | ); |
| 63 | }); |