From 1482ee2c433548790ae372eaf54c3c35b0de9f22 Mon Sep 17 00:00:00 2001 From: Stephen Li Date: Wed, 3 Jul 2024 19:43:18 -0400 Subject: [PATCH] Throw getEnumeratedListType error in node generator --- src/RstNode/List/BulletListItem.ts | 45 ++++++++++++++++---------- src/RstNode/List/EnumeratedList.ts | 4 +++ src/RstNode/List/EnumeratedListType.ts | 4 +-- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/RstNode/List/BulletListItem.ts b/src/RstNode/List/BulletListItem.ts index 3d497656..595205d1 100644 --- a/src/RstNode/List/BulletListItem.ts +++ b/src/RstNode/List/BulletListItem.ts @@ -1,11 +1,13 @@ import { romanToInt } from '@/utils/romanToInt.js' import { RstNode, RstNodeJson, RstNodeObject, RstNodeSource } from '../RstNode.js' import { RstEnumeratedList } from './EnumeratedList.js' -import { RstEnumeratedListType } from './EnumeratedListType.js' import { createNodeGenerators } from '@/Generator/RstGenerator.js' import { RstNodeRegistrar } from '@/Parser/RstNodeRegistrar.js' import { RstNodeType } from '../RstNodeType.js' import { HtmlAttributeStore } from '@/Generator/HtmlAttributeStore.js' +import { RstGeneratorState } from '@/Generator/RstGeneratorState.js' +import { RstGeneratorError } from '@/Generator/RstGeneratorError.js' +import { RstEnumeratedListType } from './EnumeratedListType.js' // ---------------------------------------------------------------------------- // MARK: Node @@ -77,25 +79,34 @@ export class RstBulletListItem extends RstNode { return false } - getEnumeratedListValue(): number { + getEnumeratedListValue(generatorState: RstGeneratorState): number { if (this.bullet === '#') { - return this.getMyIndexInParent() + 1 - } + const idx = this.getMyIndexInParent() + if (idx === null) { + throw new RstGeneratorError(generatorState, this, 'Failed to getEnumeratedListValue (failed to getMyIndexInParent)') + } - const parent = this.getParent(RstNodeType.EnumeratedList) - switch (parent.listType) { - case RstEnumeratedListType.AlphabetUpper: - case RstEnumeratedListType.AlphabetLower: { - const letter = this.bullet.toLowerCase() - return letter.charCodeAt(0) - 'a'.charCodeAt(0) + 1 + return idx + 1 + } else { + const parent = this.getParent(RstNodeType.EnumeratedList) + if (!parent) { + throw new RstGeneratorError(generatorState, this, 'Failed to getEnumeratedListValue (failed to getParent)') } - case RstEnumeratedListType.RomanUpper: - case RstEnumeratedListType.RomanLower: - return romanToInt(this.bullet) + switch (parent.listType) { + case RstEnumeratedListType.AlphabetUpper: + case RstEnumeratedListType.AlphabetLower: { + const letter = this.bullet.toLowerCase() + return letter.charCodeAt(0) - 'a'.charCodeAt(0) + 1 + } + + case RstEnumeratedListType.RomanUpper: + case RstEnumeratedListType.RomanLower: + return romanToInt(this.bullet) - default: - return parseInt(this.bullet) + default: + return parseInt(this.bullet) + } } } } @@ -110,7 +121,7 @@ export const bulletListItemGenerators = createNodeGenerators( (generatorState, node) => { const attrs = new HtmlAttributeStore() if (node.parent instanceof RstEnumeratedList && node.isFirstChild() && !node.isEnumeratedListFirstValue()) { - attrs.set('value', node.getEnumeratedListValue().toString()) + attrs.set('value', node.getEnumeratedListValue(generatorState).toString()) } generatorState.writeLineHtmlTagWithAttr('li', node, attrs, () => { @@ -120,7 +131,7 @@ export const bulletListItemGenerators = createNodeGenerators( (generatorState, node) => { const bullet = node.parent instanceof RstEnumeratedList - ? `${node.getEnumeratedListValue()}. ` + ? `${node.getEnumeratedListValue(generatorState)}. ` : `${node.bullet} ` const bulletWhitespace = ' '.repeat(bullet.length) diff --git a/src/RstNode/List/EnumeratedList.ts b/src/RstNode/List/EnumeratedList.ts index 7f48856c..2c58a117 100644 --- a/src/RstNode/List/EnumeratedList.ts +++ b/src/RstNode/List/EnumeratedList.ts @@ -116,6 +116,10 @@ export const enumeratedListParser: RstNodeParser = { } const listType = getEnumeratedListType(listItems[0].bullet) + if (listType === null) { + return null + } + const endLineIdx = parserState.lineIdx return new RstEnumeratedList(parserState.registrar, { startLineIdx, endLineIdx }, listItems, listType) }, diff --git a/src/RstNode/List/EnumeratedListType.ts b/src/RstNode/List/EnumeratedListType.ts index 8c897f95..b1557eed 100644 --- a/src/RstNode/List/EnumeratedListType.ts +++ b/src/RstNode/List/EnumeratedListType.ts @@ -8,7 +8,7 @@ export const enum RstEnumeratedListType { RomanLower = 'RomanLower', } -export function getEnumeratedListType(bulletValue: string): RstEnumeratedListType { +export function getEnumeratedListType(bulletValue: string): RstEnumeratedListType | null { switch (true) { case romanUpperRe.test(bulletValue): return RstEnumeratedListType.RomanUpper @@ -27,7 +27,7 @@ export function getEnumeratedListType(bulletValue: string): RstEnumeratedListTyp return RstEnumeratedListType.AlphabetLower default: - throw new Error('Failed to getEnumeratedListType') + return null } }