| 1 | import { assert, describe } from "@effect/vitest"; |
| 2 | |
| 3 | import { createOxlintRuleHarness } from "../test/utils.ts"; |
| 4 | |
| 5 | const rule = createOxlintRuleHarness("t3code/no-inline-schema-compile"); |
| 6 | |
| 7 | describe("t3code/no-inline-schema-compile", () => { |
| 8 | rule.valid( |
| 9 | "allows schema compilers hoisted to module scope", |
| 10 | ` |
| 11 | import { Schema } from "effect"; |
| 12 | |
| 13 | const User = Schema.Struct({ name: Schema.String }); |
| 14 | const decodeUser = Schema.decodeUnknownEffect(User); |
| 15 | |
| 16 | export const parseUser = (input: unknown) => decodeUser(input); |
| 17 | `, |
| 18 | ); |
| 19 | |
| 20 | rule.valid( |
| 21 | "allows factory helpers that return a precompiled decoder", |
| 22 | ` |
| 23 | import { Schema } from "effect"; |
| 24 | |
| 25 | export const makeParser = <A, I>(schema: Schema.Codec<A, I>) => { |
| 26 | const decode = Schema.decodeUnknownEffect(schema); |
| 27 | return (input: unknown) => decode(input); |
| 28 | }; |
| 29 | `, |
| 30 | ); |
| 31 | |
| 32 | rule.valid( |
| 33 | "allows schema construction helpers that use encode transformations", |
| 34 | ` |
| 35 | import { Schema } from "effect"; |
| 36 | |
| 37 | export const makePrettyJson = <S extends Schema.Top>(schema: S) => |
| 38 | Schema.fromJsonString(schema).pipe( |
| 39 | Schema.encode({ |
| 40 | decode: Schema.String, |
| 41 | encode: Schema.String, |
| 42 | }), |
| 43 | ); |
| 44 | `, |
| 45 | ); |
| 46 | |
| 47 | rule.valid( |
| 48 | "allows dynamic schema parameters that cannot be hoisted to module scope", |
| 49 | ` |
| 50 | import { Schema } from "effect"; |
| 51 | |
| 52 | export const parseWith = <A, I>(schema: Schema.Codec<A, I>, input: unknown) => |
| 53 | Schema.decodeUnknownEffect(schema)(input); |
| 54 | `, |
| 55 | ); |
| 56 | |
| 57 | rule.invalid( |
| 58 | "reports schema compilers inside function bodies", |
| 59 | ` |
| 60 | import { Schema } from "effect"; |
| 61 | |
| 62 | const User = Schema.Struct({ name: Schema.String }); |
| 63 | |
| 64 | export const parseUser = (input: unknown) => Schema.decodeUnknownEffect(User)(input); |
| 65 | `, |
| 66 | (output) => { |
| 67 | assert.match(output, /Hoist Schema\.decodeUnknownEffect/); |
| 68 | }, |
| 69 | ); |
| 70 | |
| 71 | rule.invalid( |
| 72 | "reports inline schema literals as high confidence findings", |
| 73 | ` |
| 74 | import { Schema } from "effect"; |
| 75 | |
| 76 | export const parseUser = (input: unknown) => |
| 77 | Schema.decodeUnknownEffect(Schema.Struct({ name: Schema.String }))(input); |
| 78 | `, |
| 79 | (output) => { |
| 80 | assert.match(output, /inline schema literal and the compiled function/); |
| 81 | }, |
| 82 | ); |
| 83 | }); |