3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-19 22:55:44 +01:00

Answer stability round 1: the guard's comment holds at both its call sites, and a giving-way list's items keep the list's path

This commit is contained in:
2026-10-13 01:09:32 +02:00
committed by Lillem4n
parent 7caa8237f0
commit 4657796d4d
5 changed files with 16 additions and 25 deletions
+1 -1
View File
@@ -44,7 +44,7 @@ test('refuses a value that is not an ADF document, naming the check it failed',
assert.equal(markdown(adfToLosslessMarkdown({ type: 'doc', version: Number.NaN })), 'not-an-adf-document: an ADF document holds a version number: found NaN')
})
const tooLong = 'unsupported-node-shape: spelled as markdown, this would be longer than 536870888 characters, the longest string V8 holds'
const tooLong = 'unsupported-node-shape: spelled as markdown, this would be longer than 536870888 characters, the longest markdown this library writes'
// A code block of 300 lines; two of these together fall some 1000 characters short of V8's longest string, so only what their spelling adds passes it.
function nearlyHalfTheBound(): AdfNode {
+2 -3
View File
@@ -8,14 +8,13 @@ const pastLongestString = ['InternalError: allocation size overflow', 'RangeErro
const pastEngineLimit = ['RangeError: Invalid array length', 'RangeError: Out of memory']
export function tooLong<T>(): Result<T> {
return failure('unsupported-node-shape', `spelled as markdown, this would be longer than ${longestMarkdown} characters, the longest string V8 holds`, [])
return failure('unsupported-node-shape', `spelled as markdown, this would be longer than ${longestMarkdown} characters, the longest markdown this library writes`, [])
}
export function withinLongest(text: string): Result<string> {
return text.length > longestMarkdown ? tooLong() : success(text)
}
// Joins the pieces as they come, so a spelling that grows far past its input stops at the bound unbuilt.
export function joinedWithin(pieces: Iterable<string>): Result<string> {
const kept: string[] = []
let length = 0
@@ -27,7 +26,7 @@ export function joinedWithin(pieces: Iterable<string>): Result<string> {
return success(kept.join(''))
}
// Any concatenation can pass an engine's longest string or array, so this wraps a whole conversion.
// Any concatenation can pass an engine's longest string or array; wrap every call that builds one.
export function withinEngineLimits<T>(convert: () => Result<T>): Result<T> {
try {
return convert()
@@ -138,21 +138,13 @@ function portableSequence(blocks: readonly Sourced[], reduction: Reduction): Res
continue
}
// Back on `pending`: a replacement may take the directive form itself.
const replacement = block === next.block ? listItemBlocks(block, next.path) : [{ block, path: next.path }]
const { path } = next
const replacement = (block === next.block ? nodeContent(block).flatMap(nodeContent) : [block]).map((node) => ({ block: node, path }))
for (const node of replacement.reverse()) pending.push(node)
}
return success(sequence)
}
// Offsets are the reduced item's, which are the caller's while the item's blocks reduce one to one.
function listItemBlocks(list: AdfNode, path: ConvertErrorPath): Sourced[] {
const blocks: Sourced[] = []
for (const [index, item] of nodeContent(list).entries()) {
for (const [offset, block] of nodeContent(item).entries()) blocks.push({ block, path: [...path, 'content', index, 'content', offset] })
}
return blocks
}
// A numbered list whose markers run past CommonMark's keeps its numbers as text in a bullet list.
function numberedPastMarkers(list: AdfNode): AdfNode {
const order = nodeAttrs(list)['order']