types: expose positioned AST types for parseMd#39
Merged
Conversation
Contributor
Author
review 反馈已处理(新增 2 个 commit)主要修改
最终验证(TS 5.8 默认环境) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
@lint-md/core从 parser 0.0.14 升级到 0.1.1 后,MarkdownNode直接采用 mdast/unist 的节点类型。该类型将node.position和position.start/end.offset声明为可选,而parseMd运行时实际生成的节点总是带有完整的 position 与 offset。由于公开类型未表达这一契约,下游
@lint-md/core在strictNullChecks下必须反复检查node.position/ start / end 并对 offset 使用非空断言。改动
新增 positioned AST 类型,并让
parseMd返回专门的 positioned 根类型,在 API 边界表达"所有解析节点都带position与start/end.offset"的运行时契约。新公开类型 (
src/types.ts):ParsedPoint—line/column/offset全部必填ParsedPosition—start/end必填Positioned<T>— 递归重写 mdast 节点,使position与子节点的position都为必填PositionedMarkdownRoot = Positioned<MarkdownRoot>PositionedMarkdownNode = Positioned<MarkdownNode>parseMd返回值 (src/parse-md.ts):(md: string) => PositionedMarkdownRoot(用as断言表达运行时契约,运行时不变)revertMdAstNode仍接受宽类型MarkdownRoot,不破坏下游传入自构 AST 的场景不改动:
MarkdownRoot/MarkdownNode/MarkdownDirectiveFields等现有类型保持原样,避免污染所有 AST 上下文declare module 'mdast'/'unist'收紧原类型测试
__tests__/position.spec.ts— 递归遍历整棵树,断言parseMd输出中每个节点的position.start.{line,column,offset}与position.end.{line,column,offset}都是number;覆盖 frontmatter / heading / paragraph / list / table / task list / strikethrough / code / link / autolink / inline math / container directive__tests__/types/package-exports.mts/.cts— 在 ESM 与 CJS 入口验证parseMd('# …').position.start.offset编译为number,并能赋值给PositionedMarkdownRoot/PositionedMarkdownNodepnpm run lint/build/test/test:types/test:package全过;API Extractor 自动重生成etc/parser.api.md下游影响
@lint-md/core升级后可直接:无需
if (!node.position)与!非空断言。验证
Closes #38