[codex] Enforce canonical Node namespace imports (#3238)
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
db27502aff
commit
2fa37ec183
71 files changed
+1205
-952
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { definePlugin } from "@oxlint/plugins";
|
||||
|
||||
import namespaceNodeImports from "./rules/namespace-node-imports.ts";
|
||||
import noGlobalProcessRuntime from "./rules/no-global-process-runtime.ts";
|
||||
import noInlineSchemaCompile from "./rules/no-inline-schema-compile.ts";
|
||||
import noManualEffectRuntimeInTests from "./rules/no-manual-effect-runtime-in-tests.ts";
|
||||
@@ -9,6 +10,7 @@ export default definePlugin({
|
||||
name: "t3code",
|
||||
},
|
||||
rules: {
|
||||
"namespace-node-imports": namespaceNodeImports,
|
||||
"no-global-process-runtime": noGlobalProcessRuntime,
|
||||
"no-inline-schema-compile": noInlineSchemaCompile,
|
||||
"no-manual-effect-runtime-in-tests": noManualEffectRuntimeInTests,
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import { assert, describe } from "@effect/vitest";
|
||||
|
||||
import { createOxlintRuleHarness } from "../test/utils.ts";
|
||||
|
||||
const rule = createOxlintRuleHarness("t3code/namespace-node-imports");
|
||||
|
||||
describe("t3code/namespace-node-imports", () => {
|
||||
rule.valid(
|
||||
"allows canonical Node namespaces",
|
||||
`
|
||||
import * as NodeFS from "node:fs";
|
||||
import * as NodeFSP from "node:fs/promises";
|
||||
import * as NodeAssert from "node:assert/strict";
|
||||
import * as NodeChildProcess from "node:child_process";
|
||||
import * as NodeTimersPromises from "node:timers/promises";
|
||||
import type * as NodeStream from "node:stream";
|
||||
|
||||
NodeAssert.ok(NodeChildProcess.spawn && NodeTimersPromises.setTimeout);
|
||||
export const read = NodeFS.readFileSync;
|
||||
export const readAsync = NodeFSP.readFile;
|
||||
export type Input = NodeStream.Readable;
|
||||
`,
|
||||
);
|
||||
|
||||
rule.valid(
|
||||
"does not apply to non-Node packages",
|
||||
`
|
||||
import { BrowserWindow } from "electron";
|
||||
`,
|
||||
);
|
||||
|
||||
rule.invalid(
|
||||
"reports named imports",
|
||||
`
|
||||
import { readFile } from "node:fs/promises";
|
||||
`,
|
||||
(output) => {
|
||||
assert.match(output, /namespace named NodeFSP/);
|
||||
},
|
||||
);
|
||||
|
||||
rule.invalid(
|
||||
"reports default imports",
|
||||
`
|
||||
import path from "node:path";
|
||||
`,
|
||||
(output) => {
|
||||
assert.match(output, /namespace named NodePath/);
|
||||
},
|
||||
);
|
||||
|
||||
rule.invalid(
|
||||
"reports non-canonical namespace aliases",
|
||||
`
|
||||
import * as Crypto from "node:crypto";
|
||||
import * as NodeOs from "node:os";
|
||||
`,
|
||||
(output) => {
|
||||
assert.match(output, /namespace named NodeCrypto/);
|
||||
assert.match(output, /namespace named NodeOS/);
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,76 @@
|
||||
import { defineRule } from "@oxlint/plugins";
|
||||
|
||||
const NODE_MODULE_ALIASES = new Map([
|
||||
["assert/strict", "Assert"],
|
||||
["fs/promises", "FSP"],
|
||||
]);
|
||||
|
||||
const NODE_SEGMENT_ALIASES = new Map([
|
||||
["fs", "FS"],
|
||||
["os", "OS"],
|
||||
["url", "URL"],
|
||||
["vm", "VM"],
|
||||
]);
|
||||
|
||||
const toPascalCase = (value: string) =>
|
||||
value
|
||||
.split(/[_-]/u)
|
||||
.filter((segment) => segment.length > 0)
|
||||
.map((segment) => segment[0]?.toUpperCase() + segment.slice(1))
|
||||
.join("");
|
||||
|
||||
const expectedNamespaceAlias = (source: string) => {
|
||||
const moduleName = source.slice("node:".length);
|
||||
const knownAlias = NODE_MODULE_ALIASES.get(moduleName);
|
||||
if (knownAlias !== undefined) return `Node${knownAlias}`;
|
||||
|
||||
return `Node${moduleName
|
||||
.split("/")
|
||||
.map((segment) => NODE_SEGMENT_ALIASES.get(segment) ?? toPascalCase(segment))
|
||||
.join("")}`;
|
||||
};
|
||||
|
||||
const literalStringValue = (node: unknown): string | undefined => {
|
||||
if (typeof node !== "object" || node === null) return undefined;
|
||||
if (!("type" in node) || node.type !== "Literal") return undefined;
|
||||
if (!("value" in node) || typeof node.value !== "string") return undefined;
|
||||
return node.value;
|
||||
};
|
||||
|
||||
const identifierName = (node: unknown): string | undefined => {
|
||||
if (typeof node !== "object" || node === null) return undefined;
|
||||
if (!("type" in node) || node.type !== "Identifier") return undefined;
|
||||
if (!("name" in node) || typeof node.name !== "string") return undefined;
|
||||
return node.name;
|
||||
};
|
||||
|
||||
export default defineRule({
|
||||
meta: {
|
||||
type: "problem",
|
||||
docs: {
|
||||
description: "Require canonical namespace imports for Node.js built-in modules.",
|
||||
},
|
||||
},
|
||||
create(context) {
|
||||
return {
|
||||
ImportDeclaration(node) {
|
||||
const source = literalStringValue(node.source);
|
||||
if (source === undefined || !source.startsWith("node:")) return;
|
||||
|
||||
const expectedAlias = expectedNamespaceAlias(source);
|
||||
const namespaceImport =
|
||||
node.specifiers.length === 1 && node.specifiers[0]?.type === "ImportNamespaceSpecifier"
|
||||
? node.specifiers[0]
|
||||
: undefined;
|
||||
const actualAlias = identifierName(namespaceImport?.local);
|
||||
|
||||
if (actualAlias === expectedAlias) return;
|
||||
|
||||
context.report({
|
||||
node,
|
||||
message: `Import ${source} as a namespace named ${expectedAlias}.`,
|
||||
});
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
Reference in new issue
Block a user