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 @@
1import { definePlugin } from "@oxlint/plugins";1import { definePlugin } from "@oxlint/plugins";
2 2
3import namespaceNodeImports from "./rules/namespace-node-imports.ts";
3import noGlobalProcessRuntime from "./rules/no-global-process-runtime.ts";4import noGlobalProcessRuntime from "./rules/no-global-process-runtime.ts";
4import noInlineSchemaCompile from "./rules/no-inline-schema-compile.ts";5import noInlineSchemaCompile from "./rules/no-inline-schema-compile.ts";
5import noManualEffectRuntimeInTests from "./rules/no-manual-effect-runtime-in-tests.ts";6import 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,
oxlint-plugin-t3code/rules/namespace-node-imports.test.ts+63−0
@@ -0,0 +1,63 @@
1import { assert, describe } from "@effect/vitest";
2
3import { createOxlintRuleHarness } from "../test/utils.ts";
4
5const rule = createOxlintRuleHarness("t3code/namespace-node-imports");
6
7describe("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 @@
1import { defineRule } from "@oxlint/plugins";
2
3const NODE_MODULE_ALIASES = new Map([
4 ["assert/strict", "Assert"],
5 ["fs/promises", "FSP"],
6]);
7
8const NODE_SEGMENT_ALIASES = new Map([
9 ["fs", "FS"],
10 ["os", "OS"],
11 ["url", "URL"],
12 ["vm", "VM"],
13]);
14
15const 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
22const 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
33const 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
40const 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
47export 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