Skip to content

Commit e81363a

Browse files
committed
fix: tighten tagged error linting
1 parent 8a9e0a3 commit e81363a

4 files changed

Lines changed: 140 additions & 49 deletions

File tree

‎packages/core/src/npm.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ import { LayerNode } from "./effect/layer-node"
1111
import { filesystem } from "./effect/layer-node-platform"
1212
import { makeRuntime } from "./effect/runtime"
1313
import { NpmConfig } from "./npm-config"
14-
import { errorMessage } from "./util/error"
1514

1615
export class InstallFailedError extends Schema.TaggedErrorClass<InstallFailedError>()("NpmInstallFailedError", {
1716
add: Schema.Array(Schema.String).pipe(Schema.optional),
1817
dir: Schema.String,
1918
cause: Schema.optional(Schema.Defect()),
2019
}) {
2120
override get message() {
22-
const detail = this.cause === undefined ? undefined : errorMessage(this.cause)
23-
return `Failed to install ${this.add?.join(", ") || "dependencies"} in ${this.dir}${detail ? `: ${detail}` : ""}`
21+
return `Failed to install dependencies in ${this.dir}`
2422
}
2523
}
2624

‎packages/opencode/src/skill/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function isSkillFrontmatter(data: unknown): data is { name: string; description?
6060

6161
export class InvalidError extends Schema.TaggedErrorClass<InvalidError>()("SkillInvalidError", {
6262
path: Schema.String,
63-
message: Schema.optional(Schema.String),
63+
message: Schema.String,
6464
issues: Schema.optional(Schema.Array(Issue)),
6565
}) {}
6666

‎script/lint/opencode.mjs‎

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,78 @@ const taggedErrorMessage = {
1010
schema: [],
1111
},
1212
create(context) {
13-
const schemas = new Set()
14-
1513
function visitClass(node) {
16-
const fields = taggedErrorFields(node.superClass, schemas)
17-
if (
18-
!fields ||
19-
fields.properties.some((property) => property.type === "SpreadElement" || property.computed)
20-
)
21-
return
22-
if (fields.properties.some((property) => propertyName(property) === "message")) return
14+
const fields = taggedErrorFields(context, node.superClass)
15+
if (!fields) return
16+
if (fields.properties.some(hasRequiredMessage)) return
2317
if (node.body.body.some(hasInstanceMessage)) return
2418
context.report({ node, messageId: "missing" })
2519
}
2620

2721
return {
28-
ImportDeclaration(node) {
29-
if (node.source.value !== "effect" && node.source.value !== "effect/Schema") return
30-
for (const specifier of node.specifiers) {
31-
if (specifier.type === "ImportSpecifier" && specifier.imported.name === "Schema") schemas.add(specifier.local.name)
32-
if (specifier.type === "ImportNamespaceSpecifier" && node.source.value === "effect/Schema")
33-
schemas.add(specifier.local.name)
34-
}
35-
},
3622
ClassDeclaration: visitClass,
3723
ClassExpression: visitClass,
3824
}
3925
},
4026
}
4127

42-
function taggedErrorFields(superClass, schemas) {
28+
function taggedErrorFields(context, superClass) {
4329
if (superClass?.type !== "CallExpression" || superClass.arguments.length < 2) return null
4430
if (superClass.arguments[1].type !== "ObjectExpression") return null
4531
const factory = superClass.callee
4632
if (factory.type !== "CallExpression" || factory.arguments.length !== 0) return null
4733
const taggedError = factory.callee
4834
if (taggedError.type !== "MemberExpression" || taggedError.computed) return null
49-
if (taggedError.object.type !== "Identifier" || !schemas.has(taggedError.object.name)) return null
35+
if (taggedError.object.type !== "Identifier" || !isEffectSchema(context, taggedError.object)) return null
5036
if (taggedError.property.type !== "Identifier" || taggedError.property.name !== "TaggedErrorClass") return null
5137
return superClass.arguments[1]
5238
}
5339

40+
function isEffectSchema(context, identifier) {
41+
const variable = findVariable(context.sourceCode.getScope(identifier), identifier.name)
42+
if (!variable || variable.defs.length !== 1 || variable.defs[0].type !== "ImportBinding") return false
43+
const definition = variable.defs[0]
44+
if (definition.parent.source.value === "effect")
45+
return definition.node.type === "ImportSpecifier" && definition.node.imported.name === "Schema"
46+
return definition.parent.source.value === "effect/Schema" && definition.node.type === "ImportNamespaceSpecifier"
47+
}
48+
49+
function findVariable(scope, name) {
50+
return scope.set.get(name) ?? (scope.upper ? findVariable(scope.upper, name) : null)
51+
}
52+
53+
function hasRequiredMessage(property) {
54+
if (propertyName(property) !== "message" || property.type !== "Property") return false
55+
return isStringSchema(property.value) && !isOptional(property.value)
56+
}
57+
58+
function isStringSchema(node) {
59+
if (node.type === "MemberExpression")
60+
return (
61+
node.object.type === "Identifier" &&
62+
node.object.name === "Schema" &&
63+
node.property.type === "Identifier" &&
64+
node.property.name === "String"
65+
)
66+
if (node.type !== "CallExpression" || node.callee.type !== "MemberExpression") return false
67+
return isStringSchema(node.callee.object)
68+
}
69+
70+
function isOptional(node) {
71+
if (node.type !== "CallExpression" || node.callee.type !== "MemberExpression") return false
72+
if (node.callee.object.type === "Identifier" && node.callee.object.name === "Schema")
73+
return node.callee.property.type === "Identifier" && node.callee.property.name === "optional"
74+
if (node.callee.property.type !== "Identifier" || node.callee.property.name !== "pipe") return false
75+
return node.arguments.some(
76+
(argument) =>
77+
argument.type === "MemberExpression" &&
78+
argument.object.type === "Identifier" &&
79+
argument.object.name === "Schema" &&
80+
argument.property.type === "Identifier" &&
81+
argument.property.name === "optional",
82+
)
83+
}
84+
5485
function propertyName(property) {
5586
if (property.type !== "Property" && property.type !== "PropertyDefinition" && property.type !== "MethodDefinition")
5687
return null
@@ -61,8 +92,15 @@ function propertyName(property) {
6192

6293
function hasInstanceMessage(member) {
6394
if (member.static || propertyName(member) !== "message") return false
64-
if (member.type === "MethodDefinition") return member.kind === "get"
65-
return member.type === "PropertyDefinition" && member.value !== null
95+
if (member.type === "PropertyDefinition") return member.value !== null && !isEmptyMessage(member.value)
96+
if (member.type !== "MethodDefinition" || member.kind !== "get") return false
97+
if (member.value.body.body.length === 0) return false
98+
const direct = member.value.body.body.length === 1 ? member.value.body.body[0] : null
99+
return direct?.type !== "ReturnStatement" || (direct.argument !== null && !isEmptyMessage(direct.argument))
100+
}
101+
102+
function isEmptyMessage(node) {
103+
return (node.type === "Identifier" && node.name === "undefined") || (node.type === "Literal" && !node.value)
66104
}
67105

68106
export default {

‎script/lint/test.ts‎

Lines changed: 79 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,44 @@ export namespace Example { export class Error extends Schema.TaggedErrorClass<Er
3232
},
3333
{
3434
name: "spread fields",
35-
errors: 0,
35+
errors: 1,
3636
source: `import { Schema } from "effect"
3737
const fields = { message: Schema.String }
3838
class Example extends Schema.TaggedErrorClass<Example>()("Example", { ...fields }) {}`,
3939
},
4040
{
4141
name: "computed fields",
42-
errors: 0,
42+
errors: 1,
4343
source: `import { Schema } from "effect"
4444
const key = "message"
4545
class Example extends Schema.TaggedErrorClass<Example>()("Example", { [key]: Schema.String }) {}`,
46+
},
47+
{
48+
name: "optional message",
49+
errors: 1,
50+
source: `import { Schema } from "effect"
51+
class Example extends Schema.TaggedErrorClass<Example>()("Example", { message: Schema.optional(Schema.String) }) {}`,
52+
},
53+
{
54+
name: "piped optional message",
55+
errors: 1,
56+
source: `import { Schema } from "effect"
57+
class Example extends Schema.TaggedErrorClass<Example>()("Example", { message: Schema.String.pipe(Schema.optional) }) {}`,
4658
},
4759
{
4860
name: "unrelated Schema binding",
4961
errors: 0,
5062
source: `const Schema = getSchema()
5163
class Example extends Schema.TaggedErrorClass()("Example", {}) {}`,
64+
},
65+
{
66+
name: "shadowed Schema binding",
67+
errors: 0,
68+
source: `import { Schema } from "effect"
69+
function example() {
70+
const Schema = getSchema()
71+
return class extends Schema.TaggedErrorClass()("Example", {}) {}
72+
}`,
5273
},
5374
{
5475
name: "missing message",
@@ -67,6 +88,24 @@ class Example extends Schema.TaggedErrorClass<Example>()("Example", {}) { static
6788
errors: 1,
6889
source: `import { Schema } from "effect"
6990
class Example extends Schema.TaggedErrorClass<Example>()("Example", {}) { declare message: string }`,
91+
},
92+
{
93+
name: "undefined property",
94+
errors: 1,
95+
source: `import { Schema } from "effect"
96+
class Example extends Schema.TaggedErrorClass<Example>()("Example", {}) { override message = undefined }`,
97+
},
98+
{
99+
name: "empty getter",
100+
errors: 1,
101+
source: `import { Schema } from "effect"
102+
class Example extends Schema.TaggedErrorClass<Example>()("Example", {}) { override get message() { return "" } }`,
103+
},
104+
{
105+
name: "non-string message schema",
106+
errors: 1,
107+
source: `import { Schema } from "effect"
108+
class Example extends Schema.TaggedErrorClass<Example>()("Example", { message: Schema.Unknown }) {}`,
70109
},
71110
{
72111
name: "documented disable",
@@ -88,28 +127,31 @@ try {
88127
rules: { "opencode/tagged-error-message": "error" },
89128
}),
90129
)
91-
for (const [index, fixture] of cases.entries()) {
92-
const file = path.join(directory, `${index}.ts`)
93-
await Bun.write(file, fixture.source)
94-
const result = Bun.spawnSync([
95-
path.join(import.meta.dir, "../../node_modules/.bin/oxlint"),
96-
"--config",
97-
config,
98-
"--format",
99-
"json",
100-
file,
101-
])
102-
if (!result.stdout.length) throw new Error(`${fixture.name}: ${result.stderr.toString()}`)
103-
const diagnostics: unknown = JSON.parse(result.stdout.toString())
104-
if (!hasDiagnostics(diagnostics)) throw new Error(`${fixture.name}: invalid Oxlint output`)
105-
const errors = diagnostics.diagnostics.filter(
106-
(item) => typeof item === "object" && item !== null && "code" in item && item.code === "opencode(tagged-error-message)",
107-
).length
108-
if (errors !== fixture.errors)
109-
throw new Error(
110-
`${fixture.name}: expected ${fixture.errors} errors, received ${errors}\n${result.stdout.toString()}`,
111-
)
112-
}
130+
await Promise.all(
131+
cases.map(async (fixture, index) => {
132+
const file = path.join(directory, `${index}.ts`)
133+
await Bun.write(file, fixture.source)
134+
const result = Bun.spawnSync([
135+
path.join(import.meta.dir, "../../node_modules/.bin/oxlint"),
136+
"--config",
137+
config,
138+
"--format",
139+
"json",
140+
file,
141+
])
142+
if (!result.stdout.length) throw new Error(`${fixture.name}: ${result.stderr.toString()}`)
143+
const diagnostics: unknown = JSON.parse(result.stdout.toString())
144+
if (!hasDiagnostics(diagnostics)) throw new Error(`${fixture.name}: invalid Oxlint output`)
145+
const errors = diagnostics.diagnostics.filter(isErrorDiagnostic)
146+
if (errors.length !== fixture.errors || !errors.every(isRuleDiagnostic))
147+
throw new Error(
148+
`${fixture.name}: expected ${fixture.errors} errors, received ${errors.length}\n${result.stdout.toString()}`,
149+
)
150+
const exitCode = fixture.errors === 0 ? 0 : 1
151+
if (result.exitCode !== exitCode || result.signalCode)
152+
throw new Error(`${fixture.name}: expected exit ${exitCode}, received ${result.exitCode}`)
153+
}),
154+
)
113155
} finally {
114156
await rm(directory, { recursive: true, force: true })
115157
}
@@ -119,3 +161,16 @@ console.log(`Validated ${cases.length} tagged error lint fixtures`)
119161
function hasDiagnostics(value: unknown): value is { diagnostics: unknown[] } {
120162
return typeof value === "object" && value !== null && "diagnostics" in value && Array.isArray(value.diagnostics)
121163
}
164+
165+
function isRuleDiagnostic(value: unknown) {
166+
return (
167+
typeof value === "object" &&
168+
value !== null &&
169+
"code" in value &&
170+
value.code === "opencode(tagged-error-message)"
171+
)
172+
}
173+
174+
function isErrorDiagnostic(value: unknown) {
175+
return typeof value === "object" && value !== null && "severity" in value && value.severity === "error"
176+
}

0 commit comments

Comments
 (0)