diff --git a/packages/tools/oxc/src/oxlint/rules/no-opaque-instance-fields.ts b/packages/tools/oxc/src/oxlint/rules/no-opaque-instance-fields.ts index cfda1c26ad..d4985d16f7 100644 --- a/packages/tools/oxc/src/oxlint/rules/no-opaque-instance-fields.ts +++ b/packages/tools/oxc/src/oxlint/rules/no-opaque-instance-fields.ts @@ -1,37 +1,88 @@ import type { CreateRule, ESTree, Visitor } from "oxlint" -function isSchemaOpaqueExtension(node: ESTree.Class): boolean { - const sc = node.superClass - if (!sc || sc.type !== "CallExpression") return false - const inner = sc.callee - if (!inner || inner.type !== "CallExpression") return false - const fn = inner.callee - return fn?.type === "MemberExpression" && - fn.object?.type === "Identifier" && - fn.object.name === "Schema" && - fn.property?.type === "Identifier" && - fn.property.name === "Opaque" -} +const SCHEMA_SOURCES = new Set(["effect", "effect/Schema"]) +const SCHEMA_NAMESPACE_SOURCES = new Set(["effect/Schema"]) const rule: CreateRule = { meta: { type: "problem", - docs: { description: "Disallow instance fields in Schema.Opaque classes" } + docs: { description: "Disallow instance members in Schema.Opaque classes" } }, create(context) { + // Track identifiers that point to Schema or Opaque imported from Schema. + // Local names that refer to the Schema module (Schema or namespace import). + // Example: `import { Schema } from "effect"` or `import * as S from "effect/Schema"`. + const schemaIdentifiers = new Set() + // Local names that refer to Opaque imported directly from the Schema module. + // Example: `import { Opaque as MyOpaque } from "effect/Schema"`. + const opaqueIdentifiers = new Set() + + // Match `class X extends Schema.Opaque(...)` or `class X extends Opaque(...)` when + // the identifiers are tied to the Schema module via imports. + function isSchemaOpaqueExtension(node: ESTree.Class): boolean { + const sc = node.superClass + if (!sc || sc.type !== "CallExpression") return false + const inner = sc.callee + if (!inner || inner.type !== "CallExpression") return false + return isOpaqueCallee(inner.callee) + } + + // Validate the outer `Opaque` call, allowing either `Opaque` or `.Opaque`. + function isOpaqueCallee(node: ESTree.Expression | null | undefined): boolean { + if (!node) return false + if (node.type === "Identifier") return opaqueIdentifiers.has(node.name) + if (node.type !== "MemberExpression") return false + if (node.property?.type !== "Identifier" || node.property.name !== "Opaque") return false + return isSchemaObject(node.object) + } + + // Ensure `.Opaque` is actually Schema (imported from Schema module). + function isSchemaObject(node: ESTree.Expression | null | undefined): boolean { + if (!node) return false + if (node.type === "Identifier") return schemaIdentifiers.has(node.name) + return false + } + function checkClass(node: ESTree.Class) { if (!isSchemaOpaqueExtension(node)) return for (const element of node.body.body) { if (element.type === "PropertyDefinition" && !element.static) { context.report({ node: element, - message: "Classes extending Schema.Opaque must not have instance fields" + message: "Classes extending Schema.Opaque must not have instance members" + }) + } else if (element.type === "MethodDefinition" && !element.static) { + context.report({ + node: element, + message: "Classes extending Schema.Opaque must not have instance members" }) } } } return { + // Record identifiers for Schema/Opaque imports so we don't match unrelated modules. + ImportDeclaration(node: ESTree.ImportDeclaration) { + if (node.importKind === "type") return + const source = node.source.value + if (typeof source !== "string" || !SCHEMA_SOURCES.has(source)) return + + for (const specifier of node.specifiers) { + if (specifier.type === "ImportNamespaceSpecifier") { + if (SCHEMA_NAMESPACE_SOURCES.has(source)) { + schemaIdentifiers.add(specifier.local.name) + } + } else if (specifier.type === "ImportSpecifier" && specifier.importKind !== "type") { + if (specifier.imported.type !== "Identifier") continue + const importedName = specifier.imported.name + if (importedName === "Schema") { + schemaIdentifiers.add(specifier.local.name) + } else if (importedName === "Opaque") { + opaqueIdentifiers.add(specifier.local.name) + } + } + } + }, ClassDeclaration: checkClass, ClassExpression: checkClass } as Visitor diff --git a/packages/tools/oxc/test/no-opaque-instance-fields.test.ts b/packages/tools/oxc/test/no-opaque-instance-fields.test.ts index 9b9d639890..fc11b4fe22 100644 --- a/packages/tools/oxc/test/no-opaque-instance-fields.test.ts +++ b/packages/tools/oxc/test/no-opaque-instance-fields.test.ts @@ -1,19 +1,30 @@ import rule from "@effect/oxc/oxlint/rules/no-opaque-instance-fields" import { describe, expect, it } from "vitest" -import { runRule } from "./utils.ts" +import { createTestContext, runRule } from "./utils.ts" + +function runRuleWithNodes(nodes: Array<{ visitor: string; node: unknown }>) { + const { context, errors } = createTestContext() + const visitors = rule.create(context as never) + for (const { visitor, node } of nodes) { + const handler = visitors[visitor as keyof typeof visitors] + if (handler) { + ;(handler as (node: unknown) => void)(node) + } + } + return errors +} describe("no-opaque-instance-fields", () => { - const createSchemaOpaqueClass = (members: Array<{ type: string; static: boolean }>) => ({ + const createOpaqueClass = ( + callee: { type: string; name?: string; object?: unknown; property?: unknown }, + members: Array<{ type: string; static: boolean }> + ) => ({ type: "ClassDeclaration", superClass: { type: "CallExpression", callee: { type: "CallExpression", - callee: { - type: "MemberExpression", - object: { type: "Identifier", name: "Schema" }, - property: { type: "Identifier", name: "Opaque" } - } + callee } }, body: { @@ -21,6 +32,69 @@ describe("no-opaque-instance-fields", () => { } }) + const createSchemaOpaqueClass = (members: Array<{ type: string; static: boolean }>) => + createOpaqueClass({ + type: "MemberExpression", + object: { type: "Identifier", name: "Schema" }, + property: { type: "Identifier", name: "Opaque" } + }, members) + + const createSchemaImport = () => ({ + type: "ImportDeclaration", + source: { type: "Literal", value: "effect" }, + specifiers: [{ + type: "ImportSpecifier", + imported: { type: "Identifier", name: "Schema" }, + local: { type: "Identifier", name: "Schema" }, + importKind: "value" + }], + importKind: "value" + }) + + const createOpaqueImport = (localName = "Opaque") => ({ + type: "ImportDeclaration", + source: { type: "Literal", value: "effect/Schema" }, + specifiers: [{ + type: "ImportSpecifier", + imported: { type: "Identifier", name: "Opaque" }, + local: { type: "Identifier", name: localName }, + importKind: "value" + }], + importKind: "value" + }) + + const createSchemaNamespaceImport = (localName = "S") => ({ + type: "ImportDeclaration", + source: { type: "Literal", value: "effect/Schema" }, + specifiers: [{ + type: "ImportNamespaceSpecifier", + local: { type: "Identifier", name: localName } + }], + importKind: "value" + }) + + const createOtherOpaqueImport = (localName = "Opaque") => ({ + type: "ImportDeclaration", + source: { type: "Literal", value: "other" }, + specifiers: [{ + type: "ImportSpecifier", + imported: { type: "Identifier", name: "Opaque" }, + local: { type: "Identifier", name: localName }, + importKind: "value" + }], + importKind: "value" + }) + + const createOtherNamespaceImport = (localName = "S") => ({ + type: "ImportDeclaration", + source: { type: "Literal", value: "other" }, + specifiers: [{ + type: "ImportNamespaceSpecifier", + local: { type: "Identifier", name: localName } + }], + importKind: "value" + }) + const createRegularClass = (members: Array<{ type: string; static: boolean }>) => ({ type: "ClassDeclaration", superClass: { @@ -34,7 +108,10 @@ describe("no-opaque-instance-fields", () => { it("should not report for class without instance fields", () => { const node = createSchemaOpaqueClass([]) - const errors = runRule(rule, "ClassDeclaration", node) + const errors = runRuleWithNodes([ + { visitor: "ImportDeclaration", node: createSchemaImport() }, + { visitor: "ClassDeclaration", node } + ]) expect(errors).toHaveLength(0) }) @@ -42,7 +119,10 @@ describe("no-opaque-instance-fields", () => { const node = createSchemaOpaqueClass([ { type: "PropertyDefinition", static: true } ]) - const errors = runRule(rule, "ClassDeclaration", node) + const errors = runRuleWithNodes([ + { visitor: "ImportDeclaration", node: createSchemaImport() }, + { visitor: "ClassDeclaration", node } + ]) expect(errors).toHaveLength(0) }) @@ -50,9 +130,76 @@ describe("no-opaque-instance-fields", () => { const node = createSchemaOpaqueClass([ { type: "PropertyDefinition", static: false } ]) - const errors = runRule(rule, "ClassDeclaration", node) + const errors = runRuleWithNodes([ + { visitor: "ImportDeclaration", node: createSchemaImport() }, + { visitor: "ClassDeclaration", node } + ]) expect(errors).toHaveLength(1) - expect(errors[0].message).toBe("Classes extending Schema.Opaque must not have instance fields") + expect(errors[0].message).toBe("Classes extending Schema.Opaque must not have instance members") + }) + + it("should not report when Schema.Opaque has no Schema import", () => { + const node = createSchemaOpaqueClass([ + { type: "PropertyDefinition", static: false } + ]) + const errors = runRule(rule, "ClassDeclaration", node) + expect(errors).toHaveLength(0) + }) + + it("should report for instance fields in Opaque class", () => { + const node = createOpaqueClass( + { type: "Identifier", name: "Opaque" }, + [{ type: "PropertyDefinition", static: false }] + ) + const errors = runRuleWithNodes([ + { visitor: "ImportDeclaration", node: createOpaqueImport() }, + { visitor: "ClassDeclaration", node } + ]) + expect(errors).toHaveLength(1) + }) + + it("should report for instance fields in aliased Opaque class", () => { + const node = createOpaqueClass( + { + type: "MemberExpression", + object: { type: "Identifier", name: "S" }, + property: { type: "Identifier", name: "Opaque" } + }, + [{ type: "PropertyDefinition", static: false }] + ) + const errors = runRuleWithNodes([ + { visitor: "ImportDeclaration", node: createSchemaNamespaceImport() }, + { visitor: "ClassDeclaration", node } + ]) + expect(errors).toHaveLength(1) + }) + + it("should not report for Opaque import from other module", () => { + const node = createOpaqueClass( + { type: "Identifier", name: "Opaque" }, + [{ type: "PropertyDefinition", static: false }] + ) + const errors = runRuleWithNodes([ + { visitor: "ImportDeclaration", node: createOtherOpaqueImport() }, + { visitor: "ClassDeclaration", node } + ]) + expect(errors).toHaveLength(0) + }) + + it("should not report for namespace Opaque from other module", () => { + const node = createOpaqueClass( + { + type: "MemberExpression", + object: { type: "Identifier", name: "S" }, + property: { type: "Identifier", name: "Opaque" } + }, + [{ type: "PropertyDefinition", static: false }] + ) + const errors = runRuleWithNodes([ + { visitor: "ImportDeclaration", node: createOtherNamespaceImport() }, + { visitor: "ClassDeclaration", node } + ]) + expect(errors).toHaveLength(0) }) it("should report for multiple instance fields", () => { @@ -61,16 +208,22 @@ describe("no-opaque-instance-fields", () => { { type: "PropertyDefinition", static: true }, { type: "PropertyDefinition", static: false } ]) - const errors = runRule(rule, "ClassDeclaration", node) + const errors = runRuleWithNodes([ + { visitor: "ImportDeclaration", node: createSchemaImport() }, + { visitor: "ClassDeclaration", node } + ]) expect(errors).toHaveLength(2) }) - it("should not report for methods", () => { + it("should report for instance methods", () => { const node = createSchemaOpaqueClass([ { type: "MethodDefinition", static: false } ]) - const errors = runRule(rule, "ClassDeclaration", node) - expect(errors).toHaveLength(0) + const errors = runRuleWithNodes([ + { visitor: "ImportDeclaration", node: createSchemaImport() }, + { visitor: "ClassDeclaration", node } + ]) + expect(errors).toHaveLength(1) }) it("should not report for non-Schema.Opaque classes", () => { @@ -88,7 +241,10 @@ describe("no-opaque-instance-fields", () => { ]), type: "ClassExpression" } - const errors = runRule(rule, "ClassExpression", node) + const errors = runRuleWithNodes([ + { visitor: "ImportDeclaration", node: createSchemaImport() }, + { visitor: "ClassExpression", node } + ]) expect(errors).toHaveLength(1) })