Visiting a custom AST without position
field doesn't seem to work (sometimes)?
#178
-
Somethign strange happened to me just now: I'm buyilding a custom transformer from export function toMttast(tree: Node, options: Options = {}) {
let mttast
const h = Object.assign<any, any>(transformer, {
handlers: options.handlers ? {...handlers, ...options.handlers} : handlers,
wrapText: true,
textProps: {},
} as Context)
let result = one(h, tree, undefined)
if (!result) {
mttast = u('root', [])
} else if (Array.isArray(result)) {
mttast = u('root', result)
} else {
mttast = result
}
removePosition(mttast, true)
// visit(mttast, 'text', ontext)
visit<any, 'text'>(mttast, 'text', (node, index, parent) => {
console.log('visit: ', {node, index, parent}) // ⬅️ because `parent` is defined in the visitor function it just hangs 🤷🏼♂️
})
return mttast
} (This function follows the I tried to replicate the error outside with a raw object but it does maybe this is not a bug on any unified tool, but I was curious to know if this happened before to someone else? here's the simple demo example I ran that worked: import { toMdast } from "hast-util-to-mdast"
import rehypeParse from "rehype-parse"
import { unified } from "unified"
import { removePosition } from "unist-util-remove-position"
import { visit } from "unist-util-visit"
let hast = unified()
.use(rehypeParse, { fragment: true })
.parse(`<span>hello </span><span>world</span>`)
let mdast = toMdast(hast, { fragment: true })
let noPosASt = removePosition(mdast, true)
// visit(noPosASt, "text", (node, index, parent) => {
// console.log("visit: ", node, index, parent)
// })
visit(
{
type: "root",
children: [
{
type: "text",
value: "hello ",
strong: true,
},
{
type: "text",
value: "world",
underline: true,
},
],
},
"text",
(node, index, parent) => {
console.log("visit: ", node, index, parent)
}
)
console.log(JSON.stringify(mdast, null, 2)) thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Are you using TypeScript 4.5 by any chance? This could be the same as #175 |
Beta Was this translation helpful? Give feedback.
Are you using TypeScript 4.5 by any chance? This could be the same as #175
In which case downgrade to TypeScript 4.4 for now, while we wait for microsoft/TypeScript#46900 to be addressed.