MCPcopy Create free account
hub / github.com/Effect-TS/effect / parseExample

Function parseExample

packages/tools/jsdocs/src/Jsdocs.ts:1303–1379  ·  view source on GitHub ↗
(lines: Array<string>, headingIndex: number)

Source from the content-addressed store, hash-verified

1301const isTypeScriptFence = (line: string): boolean => /^```ts(?:\s.*)?$/.test(line)
1302
1303function parseExample(lines: Array<string>, headingIndex: number): {
1304 readonly example?: ParsedExample
1305 readonly nextIndex: number
1306 readonly diagnostics: ReadonlyArray<JSDocDiagnostic>
1307} {
1308 const diagnostics: Array<JSDocDiagnostic> = []
1309 const heading = lines[headingIndex].trim()
1310 const match = /^\*\*Example\*\* \((.+)\)$/.exec(heading)
1311 if (!match || match[1].trim() === "") {
1312 diagnostics.push(diagnostic("malformed-example", "TypeScript examples must use **Example** (Title)"))
1313 }
1314 if (lines[headingIndex + 1]?.trim() !== "") {
1315 diagnostics.push(diagnostic("invalid-spacing", "Example headings must be followed by exactly one blank line"))
1316 }
1317
1318 let index = headingIndex + 2
1319 const bodyStart = index
1320 let fenceIndex = -1
1321 while (index < lines.length) {
1322 const trimmed = lines[index].trim()
1323 if (isTypeScriptFence(trimmed)) {
1324 fenceIndex = index
1325 break
1326 }
1327 if (trimmed.startsWith("```")) {
1328 diagnostics.push(diagnostic("malformed-example", "Examples may only contain one TypeScript code fence"))
1329 }
1330 if (trimmed === "" && isHeadingLine(lines[index + 1])) {
1331 break
1332 }
1333 if (trimmed.startsWith("@")) {
1334 break
1335 }
1336 index++
1337 }
1338
1339 if (fenceIndex === -1) {
1340 diagnostics.push(diagnostic("malformed-example", "Examples must include a non-empty ```ts fence"))
1341 return { nextIndex: index, diagnostics }
1342 }
1343
1344 const bodyLines = lines.slice(bodyStart, fenceIndex)
1345 const nonEmptyBody = joinBody(bodyLines).trim() !== ""
1346 if (nonEmptyBody && bodyLines.at(-1)?.trim() !== "") {
1347 diagnostics.push(
1348 diagnostic("invalid-spacing", "Example prose must be separated from code by exactly one blank line")
1349 )
1350 }
1351 const prose = nonEmptyBody ? joinBody(bodyLines.slice(0, bodyLines.at(-1)?.trim() === "" ? -1 : undefined)) : null
1352
1353 index = fenceIndex + 1
1354 const codeStart = index
1355 while (index < lines.length && lines[index].trim() !== "```") {
1356 if (isTypeScriptFence(lines[index].trim())) {
1357 diagnostics.push(diagnostic("malformed-example", "Examples must contain exactly one TypeScript code fence"))
1358 }
1359 index++
1360 }

Callers 2

parseDescriptionContentFunction · 0.85
parseModuleExamplesFunction · 0.85

Calls 9

diagnosticFunction · 0.85
isTypeScriptFenceFunction · 0.85
isHeadingLineFunction · 0.85
joinBodyFunction · 0.85
trimMethod · 0.80
sliceMethod · 0.80
pushMethod · 0.65
atMethod · 0.65
execMethod · 0.45

Tested by

no test coverage detected