History

[codex] Enforce canonical Node namespace imports (#3238)

Julius Marminge committed 4 months ago

Commit 2fa37ec1 parent db2750271 files · +1205 −952Browse files

Showing the changes to oxlint-plugin-t3code (3 of 71 files). Show all

Unified · Split

3 files
  1. oxlint-plugin-t3code/index.ts +2 −0
  2. oxlint-plugin-t3code/rules/namespace-node-imports.test.ts +63 −0
  3. oxlint-plugin-t3code/rules/namespace-node-imports.ts +76 −0
oxlint-plugin-t3code/index.ts+2−0
@@ -1,5 +1,6 @@
11 import { definePlugin } from "@oxlint/plugins";
22
3+import namespaceNodeImports from "./rules/namespace-node-imports.ts";
34 import noGlobalProcessRuntime from "./rules/no-global-process-runtime.ts";
45 import noInlineSchemaCompile from "./rules/no-inline-schema-compile.ts";
56 import noManualEffectRuntimeInTests from "./rules/no-manual-effect-runtime-in-tests.ts";
@@ -9,6 +10,7 @@ export default definePlugin({ · 3 unmodified lines
910 name: "t3code",
1011 },
1112 rules: {
13+ "namespace-node-imports": namespaceNodeImports,
1214 "no-global-process-runtime": noGlobalProcessRuntime,
1315 "no-inline-schema-compile": noInlineSchemaCompile,
1416 "no-manual-effect-runtime-in-tests": noManualEffectRuntimeInTests,
oxlint-plugin-t3code/rules/namespace-node-imports.test.ts+63−0
@@ -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+});
oxlint-plugin-t3code/rules/namespace-node-imports.ts+76−0
@@ -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+});

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t