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

Method parseBlockScalar

packages/effect/src/encoding/Yaml.ts:1364–1430  ·  view source on GitHub ↗
(indicator: string, parentIndent: number)

Source from the content-addressed store, hash-verified

1362 }
1363
1364 private parseBlockScalar(indicator: string, parentIndent: number): string {
1365 const style = indicator[0]
1366 const chomp = indicator.includes("-") ? "strip" : indicator.includes("+") ? "keep" : "clip"
1367 const explicitIndent = Number.parseInt(indicator.replace(/[^1-9]/g, ""), 10)
1368 const start = this.index
1369 let end = start
1370 let contentIndent = Number.isNaN(explicitIndent) ? undefined : parentIndent + explicitIndent
1371 let hasContent = false
1372
1373 while (end < this.documentEnd) {
1374 const line = this.lines[end]
1375 // Only spaces make a line empty; a tab after the indentation is content.
1376 if (/[^ ]/.test(line.raw)) {
1377 if (contentIndent === undefined && line.indent > parentIndent) {
1378 // The first non-empty line sets the indentation, which no leading empty line may exceed.
1379 const deeper = this.lines.slice(start, end).find((empty) => empty.indent > line.indent)
1380 if (deeper !== undefined) {
1381 this.fail(deeper, "Leading empty lines cannot be more indented than the block scalar content")
1382 }
1383 contentIndent = line.indent
1384 }
1385 if (contentIndent !== undefined && line.indent >= contentIndent) {
1386 hasContent = true
1387 } else {
1388 this.rejectTabIndent(line)
1389 // A less indented line or comment ends the scalar.
1390 if (line.indent <= parentIndent || line.text.length === 0) break
1391 this.fail(line, `Expected block scalar indentation of ${contentIndent} spaces`)
1392 }
1393 }
1394 end++
1395 }
1396
1397 // Whitespace past the indentation is content only once the scalar has a content line.
1398 const content = this.lines.slice(start, end).map((line) => hasContent ? line.raw.slice(contentIndent) : "")
1399 this.index = end
1400
1401 let contentEnd = content.length
1402 while (contentEnd > 0 && content[contentEnd - 1].length === 0) contentEnd--
1403 const trailingBreaks = Math.max(0, content.length - contentEnd - (end === this.lines.length ? 1 : 0)) +
1404 (contentEnd > 0 && start + contentEnd < this.lines.length ? 1 : 0)
1405 content.length = contentEnd
1406
1407 let output = ""
1408 if (style === "|") {
1409 output = content.join("\n")
1410 } else {
1411 let blankLines = 0
1412 let previousMoreIndented: boolean | undefined
1413 for (const line of content) {
1414 if (line.length === 0) {
1415 blankLines++
1416 } else {
1417 const moreIndented = line.startsWith(" ") || line.startsWith("\t")
1418 const hardBreak = moreIndented || previousMoreIndented === true
1419 if (previousMoreIndented === undefined) output += "\n".repeat(blankLines)
1420 else if (blankLines === 0) output += hardBreak ? "\n" : " "
1421 else output += "\n".repeat(blankLines + (hardBreak ? 1 : 0))

Callers 1

parseValueMethod · 0.95

Calls 7

failMethod · 0.95
rejectTabIndentMethod · 0.95
sliceMethod · 0.80
findMethod · 0.65
replaceMethod · 0.45
mapMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected