Showing the changes to oxlint-plugin-t3code (3 of 71 files). Show all
Unified · Split
| @@ -1,5 +1,6 @@ | |||
| 1 | import { definePlugin } from "@oxlint/plugins"; | 1 | import { definePlugin } from "@oxlint/plugins"; |
| 2 | 2 | ||
| 3 | import namespaceNodeImports from "./rules/namespace-node-imports.ts"; | ||
| 3 | import noGlobalProcessRuntime from "./rules/no-global-process-runtime.ts"; | 4 | import noGlobalProcessRuntime from "./rules/no-global-process-runtime.ts"; |
| 4 | import noInlineSchemaCompile from "./rules/no-inline-schema-compile.ts"; | 5 | import noInlineSchemaCompile from "./rules/no-inline-schema-compile.ts"; |
| 5 | import noManualEffectRuntimeInTests from "./rules/no-manual-effect-runtime-in-tests.ts"; | 6 | import noManualEffectRuntimeInTests from "./rules/no-manual-effect-runtime-in-tests.ts"; |
| @@ -9,6 +10,7 @@ export default definePlugin({ · 3 unmodified lines | |||
| 9 | name: "t3code", | 10 | name: "t3code", |
| 10 | }, | 11 | }, |
| 11 | rules: { | 12 | rules: { |
| 13 | "namespace-node-imports": namespaceNodeImports, | ||
| 12 | "no-global-process-runtime": noGlobalProcessRuntime, | 14 | "no-global-process-runtime": noGlobalProcessRuntime, |
| 13 | "no-inline-schema-compile": noInlineSchemaCompile, | 15 | "no-inline-schema-compile": noInlineSchemaCompile, |
| 14 | "no-manual-effect-runtime-in-tests": noManualEffectRuntimeInTests, | 16 | "no-manual-effect-runtime-in-tests": noManualEffectRuntimeInTests, |
| @@ -0,0 +1,63 @@ | |||
| 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 | }); | ||
| @@ -0,0 +1,76 @@ | |||
| 1 | import { defineRule } from "@oxlint/plugins"; | ||
| 2 | |||
| 3 | const NODE_MODULE_ALIASES = new Map([ | ||
| 4 | ["assert/strict", "Assert"], | ||
| 5 | ["fs/promises", "FSP"], | ||
| 6 | ]); | ||
| 7 | |||
| 8 | const NODE_SEGMENT_ALIASES = new Map([ | ||
| 9 | ["fs", "FS"], | ||
| 10 | ["os", "OS"], | ||
| 11 | ["url", "URL"], | ||
| 12 | ["vm", "VM"], | ||
| 13 | ]); | ||
| 14 | |||
| 15 | const toPascalCase = (value: string) => | ||
| 16 | value | ||
| 17 | .split(/[_-]/u) | ||
| 18 | .filter((segment) => segment.length > 0) | ||
| 19 | .map((segment) => segment[0]?.toUpperCase() + segment.slice(1)) | ||
| 20 | .join(""); | ||
| 21 | |||
| 22 | const expectedNamespaceAlias = (source: string) => { | ||
| 23 | const moduleName = source.slice("node:".length); | ||
| 24 | const knownAlias = NODE_MODULE_ALIASES.get(moduleName); | ||
| 25 | if (knownAlias !== undefined) return `Node${knownAlias}`; | ||
| 26 | |||
| 27 | return `Node${moduleName | ||
| 28 | .split("/") | ||
| 29 | .map((segment) => NODE_SEGMENT_ALIASES.get(segment) ?? toPascalCase(segment)) | ||
| 30 | .join("")}`; | ||
| 31 | }; | ||
| 32 | |||
| 33 | const literalStringValue = (node: unknown): string | undefined => { | ||
| 34 | if (typeof node !== "object" || node === null) return undefined; | ||
| 35 | if (!("type" in node) || node.type !== "Literal") return undefined; | ||
| 36 | if (!("value" in node) || typeof node.value !== "string") return undefined; | ||
| 37 | return node.value; | ||
| 38 | }; | ||
| 39 | |||
| 40 | const identifierName = (node: unknown): string | undefined => { | ||
| 41 | if (typeof node !== "object" || node === null) return undefined; | ||
| 42 | if (!("type" in node) || node.type !== "Identifier") return undefined; | ||
| 43 | if (!("name" in node) || typeof node.name !== "string") return undefined; | ||
| 44 | return node.name; | ||
| 45 | }; | ||
| 46 | |||
| 47 | export default defineRule({ | ||
| 48 | meta: { | ||
| 49 | type: "problem", | ||
| 50 | docs: { | ||
| 51 | description: "Require canonical namespace imports for Node.js built-in modules.", | ||
| 52 | }, | ||
| 53 | }, | ||
| 54 | create(context) { | ||
| 55 | return { | ||
| 56 | ImportDeclaration(node) { | ||
| 57 | const source = literalStringValue(node.source); | ||
| 58 | if (source === undefined || !source.startsWith("node:")) return; | ||
| 59 | |||
| 60 | const expectedAlias = expectedNamespaceAlias(source); | ||
| 61 | const namespaceImport = | ||
| 62 | node.specifiers.length === 1 && node.specifiers[0]?.type === "ImportNamespaceSpecifier" | ||
| 63 | ? node.specifiers[0] | ||
| 64 | : undefined; | ||
| 65 | const actualAlias = identifierName(namespaceImport?.local); | ||
| 66 | |||
| 67 | if (actualAlias === expectedAlias) return; | ||
| 68 | |||
| 69 | context.report({ | ||
| 70 | node, | ||
| 71 | message: `Import ${source} as a namespace named ${expectedAlias}.`, | ||
| 72 | }); | ||
| 73 | }, | ||
| 74 | }; | ||
| 75 | }, | ||
| 76 | }); | ||