The best way to control Claude Code, Codex and more from any computer or phone

main
ts 218 lines 5.81 KB
Raw
1 import * as Checker from "@effect/docgen/Checker"
2 import * as Configuration from "@effect/docgen/Configuration"
3 import * as Parser from "@effect/docgen/Parser"
4 import { assert, describe, it } from "@effect/vitest"
5 import { Effect, Predicate } from "effect"
6 import * as Path from "effect/Path"
7 import * as ast from "ts-morph"
8
9 const project = new ast.Project({
10 compilerOptions: { strict: true },
11 useInMemoryFileSystem: true
12 })
13
14 const defaultConfig: Configuration.ConfigurationShape = {
15 projectName: "docgen",
16 projectHomepage: "https://github.com/effect-ts/docgen",
17 srcLink: "https://github.com/effect-ts/docgen/blob/main/src/",
18 srcDir: "src",
19 outDir: "docs",
20 theme: "mikearnaldi/just-the-docs",
21 enableSearch: true,
22 enforceDescriptions: false,
23 enforceExamples: false,
24 enforceVersion: true,
25 runExamples: false,
26 tscExecutable: "tsc",
27 exclude: [],
28 parseCompilerOptions: {},
29 examplesCompilerOptions: {}
30 }
31
32 const makeSourcefile = (source: string | ast.SourceFile) => {
33 if (Predicate.isString(source)) {
34 const filename = `test.ts`
35 const existing = project.getSourceFile(filename)
36 if (existing) {
37 project.removeSourceFile(existing)
38 }
39 return project.createSourceFile(filename, source)
40 }
41 return source
42 }
43
44 const makeSource = (source: string | ast.SourceFile) => {
45 const sourceFile = makeSourcefile(source)
46 const filename = sourceFile.getBaseName()
47 return Parser.Source.of({
48 path: [filename],
49 sourceFile
50 })
51 }
52
53 const expectFailure = Effect.fnUntraced(function*<A>(
54 config: Partial<Configuration.ConfigurationShape>,
55 sourceText: string,
56 parser: Effect.Effect<A, never, Parser.Source | Configuration.Configuration | Path.Path>,
57 checker: (a: A) => Effect.Effect<Array<string>, never, Configuration.Configuration | Parser.Source>,
58 failure: ReadonlyArray<string>
59 ) {
60 const actual = yield* Effect.exit(parser.pipe(
61 Effect.flatMap(checker),
62 Effect.provideService(Parser.Source, makeSource(sourceText)),
63 Effect.provideService(Configuration.Configuration, { ...defaultConfig, ...config }),
64 Effect.provide(Path.layer)
65 ))
66 assert.ok(actual._tag === "Success")
67 assert.deepStrictEqual(actual.value, failure)
68 })
69
70 describe("Checker", () => {
71 describe("checkFunctions", () => {
72 it.effect("should raise an error if `@since` tag is missing", () =>
73 expectFailure(
74 {},
75 `
76 /** @since 1.0.0 */
77 export function a() {}
78
79 /** description */
80 export function b() {}
81 `,
82 Parser.parseFunctions,
83 Checker.checkFunctions,
84 [
85 "Missing `@since` tag in file /test.ts:\n" +
86 "\n" +
87 " 4 |\n" +
88 " 5 | /** description */\n" +
89 "> 6 | export function b() {}\n" +
90 " | ^\n" +
91 " 7 | "
92 ]
93 ))
94 })
95
96 describe("checkExports", () => {
97 it.effect("should raise an error if `@since` tag is missing", () =>
98 expectFailure(
99 {},
100 "export { a }",
101 Parser.parseExports,
102 Checker.checkExports,
103 [
104 "Missing `@since` tag in file /test.ts:\n" +
105 "\n" +
106 "> 1 | export { a }\n" +
107 " | ^"
108 ]
109 ))
110 })
111
112 describe("checkNamespaces", () => {
113 it.effect("should raise an error if `@since` tag is missing", () =>
114 expectFailure(
115 {},
116 "export namespace A {}",
117 Parser.parseNamespaces,
118 Checker.checkNamespaces,
119 [
120 "Missing `@since` tag in file /test.ts:\n" +
121 "\n" +
122 "> 1 | export namespace A {}\n" +
123 " | ^"
124 ]
125 ))
126
127 it.effect("should raise an error if `@since` tag is missing on a nested interface", () =>
128 expectFailure(
129 {},
130 `
131 /**
132 * @since 1.0.0
133 */
134 export namespace A {
135 export interface B {}
136 }
137 `,
138 Parser.parseNamespaces,
139 Checker.checkNamespaces,
140 [
141 "Missing `@since` tag in file /test.ts:\n" +
142 "\n" +
143 " 4 | */\n" +
144 " 5 | export namespace A {\n" +
145 "> 6 | export interface B {}\n" +
146 " | ^\n" +
147 " 7 | }\n" +
148 " 8 | "
149 ]
150 ))
151
152 it.effect("should raise an error if `@since` tag is missing on a nested type alias", () =>
153 expectFailure(
154 {},
155 `
156 /**
157 * @since 1.0.0
158 */
159 export namespace A {
160 export type B = string
161 }
162 `,
163 Parser.parseNamespaces,
164 Checker.checkNamespaces,
165 [
166 "Missing `@since` tag in file /test.ts:\n" +
167 "\n" +
168 " 4 | */\n" +
169 " 5 | export namespace A {\n" +
170 "> 6 | export type B = string\n" +
171 " | ^\n" +
172 " 7 | }\n" +
173 " 8 | "
174 ]
175 ))
176
177 it.effect("should raise an error if `@since` tag is missing on a nested namespace", () =>
178 expectFailure(
179 {},
180 `
181 /**
182 * @since 1.0.0
183 */
184 export namespace A {
185 export namespace B {}
186 }
187 `,
188 Parser.parseNamespaces,
189 Checker.checkNamespaces,
190 [
191 "Missing `@since` tag in file /test.ts:\n" +
192 "\n" +
193 " 4 | */\n" +
194 " 5 | export namespace A {\n" +
195 "> 6 | export namespace B {}\n" +
196 " | ^\n" +
197 " 7 | }\n" +
198 " 8 | "
199 ]
200 ))
201 })
202
203 describe("checkClasses", () => {
204 it.effect("should raise an error if `@since` tag is missing", () =>
205 expectFailure(
206 {},
207 `export class MyClass {}`,
208 Parser.parseClasses,
209 Checker.checkClasses,
210 [
211 "Missing `@since` tag in file /test.ts:\n" +
212 "\n" +
213 "> 1 | export class MyClass {}\n" +
214 " | ^"
215 ]
216 ))
217 })
218 })