1import { assert } from "@effect/vitest"
2import { applyTransform } from "jscodeshift/src/testUtils.js"
3import { describe, it } from "vitest"
4import transformer from "./jsdoc.ts"
5
6const doc = "/** Repeats a value. */"
7
8const transform = (source: string): string =>
9 applyTransform(transformer, {}, { path: "Module.ts", source }, { parser: "ts" })
10
11const countDocs = (output: string): number => output.split("Repeats a value.").length - 1
12
13describe("jsdoc codemod", () => {
14 it("leaves generic calls inside an annotated dual implementation unchanged", () => {
15 const output = transform(`${doc}
16export const repeat: {
17 (n: number): <A>(self: A) => Array<A>
18 <A>(self: A, n: number): Array<A>
19} = dual(2, <A>(self: A, n: number): Array<A> => make<A, number>(self, n))
20`)
21 assert.include(output, "make<A, number>(self, n)")
22 assert.strictEqual(countDocs(output), 3)
23 })
24
25 it("leaves generic calls inside a dual implementation with type arguments unchanged", () => {
26 const output = transform(`${doc}
27export const repeat = dual<
28 (n: number) => <A>(self: A) => Array<A>,
29 <A>(self: A, n: number) => Array<A>
30>(2, <A>(self: A, n: number) => make<A, number>(self, n))
31`)
32 assert.include(output, "make<A, number>(self, n)")
33 assert.strictEqual(countDocs(output), 3)
34 })
35
36 it.each(["dual", "Dual.dual"])(
37 "leaves initializer call signatures unchanged on an annotated %s export",
38 (callee) => {
39 const output = transform(`${doc}
40export const repeat: {
41 (n: number): (self: string) => string
42 (self: string, n: number): string
43} = ${callee}<
44 { (n: number): (self: string) => string },
45 { (self: string, n: number): string }
46>(2, (self: string, n: number): string => self.repeat(n))
47`)
48 assert.strictEqual(countDocs(output), 3)
49 }
50 )
51})