syntax-tree / unist-util-visit Goto Github PK
View Code? Open in Web Editor NEWutility to visit nodes
Home Page: https://unifiedjs.com
License: MIT License
utility to visit nodes
Home Page: https://unifiedjs.com
License: MIT License
Hi, currently the type for Visitor is
type Visitor<V extends Node> = (
node: V,
index: number,
parent: Node
) => void | Action | Index | ActionTuple
I think the parent parameter should be of type Parent
, as the type Node
does not have a children property.
Add Typescript typings for unist-util-visit
projects have to // @ts-ignore
or manually add typings
Typings should be provided
This plugin is one of the more common utils used in transforms, the typings here can help inform a decision on unifiedjs/unified#65
This may be able to leverage https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/unist-util-is/index.d.ts
No response
After upgrading from 4.1.1 to 4.1.2 I receive the errors below:
[email protected] build:esm
tsc
node_modules/unist-util-visit/lib/index.d.ts:32:24 - error TS2307: Cannot find module 'unist-util-is/lib/index.js' or its corresponding type declarations.
32 Check extends import('unist-util-is/lib/index.js').Test
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/unist-util-visit/lib/index.d.ts:115:24 - error TS2307: Cannot find module 'unist-util-is/lib/index.js' or its corresponding type declarations.
115 Check extends import('unist-util-is/lib/index.js').Test
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/unist-util-visit/lib/index.d.ts:132:24 - error TS2307: Cannot find module 'unist-util-is/lib/index.js' or its corresponding type declarations.
132 Check extends import('unist-util-is/lib/index.js').Test = string
No type declaration errors.
Receive type declaration errors.
macos
No response
https://codesandbox.io/s/react-markdown-debug-forked-sqvof
The visit
method should be able to narrow the type of directiveVisitor
and not throw an error.
The visit
method's type is not able narrow the type of directiveVisitor
because it can't read the test ['textDirective', 'leafDirective', 'containerDirective']
Node v14
yarn v1
macOS
Next.js
4.1.0
No response
clone https://github.com/cjroebuck/next-build-stalled
npm i
npx tsc
typescript version 4.5.2
(typescript version 4.4.3 does build ok)
build shouldn't stall
build stalls indefinitely
In order to get the build to pass, comment out this line
I suspect it's an issue with the unist-util-visit types?
Node v16
npm v7
macOS
Next.js
Hey, @wooorm 👋
I'm working on Gatsby plugin for extending code block functionality and it's using the below syntax:
```js:title=index.js©=true
console.log('Some code goes here...')
```
The problem I'm facing is that ©
gets encoded to ©
. Is there any way to avoid it?
This is a doc suggestion + a more general question.
It may be worth putting in the docs some thoughts on what the contract is wrt mutating the tree. In particular, modifying individual nodes is fine, but unist-util-visit assumes the visitor does not mutate the number of children of the parent.
This is a bit of a pitfall, as it's not uncommon to want to traverse and mutate adjacent children. For example, see https://github.com/jackycute/remark-html-emoji-image/blob/master/index.js#L66-L69 — but it appears that approach is buggy, since unist-util-visit visits children sequentially by index.
More generally, I'd be interested to know what approach is best to handle the fully mutating use case. https://github.com/syntax-tree/unist-util-map and https://github.com/eush77/unist-util-filter are listed but cover different use cases. One easy option (but may not be the best) is to make a visit-dynamic
variant, something like:
'use strict';
module.exports = visit;
/**
* A mutating version of https://github.com/syntax-tree/unist-util-visit
*
* Traverse a Unist tree, while also allowing addition/deletion of children.
* Visitor should return number of nodes added/deleted (positive for addition, negative for removal,
* 0 if only visited), or false to terminate. To avoid confusion, we don't support reverse traversal.
*/
function visit(tree, type, visitor) {
if (typeof type === 'function') {
visitor = type;
type = null;
}
one(tree);
/* Visit a single node. */
function one(node, index, parent) {
let result;
index = index || (parent ? 0 : null);
if (!type || node.type === type) {
result = visitor(node, index, parent || null);
}
if (node.children && result !== false) {
all(node);
}
return result;
}
/* Visit children in `parent`, possibly adding or removing children. */
function all(parent) {
let child;
let index = 0;
while (index < parent.children.length) {
child = parent.children[index];
let delta = 0;
if (child) {
delta = one(child, index, parent);
if (delta === false) { // Abort.
return;
}
}
index += 1 + (delta || 0);
}
}
}
I think it would be easier to get started with this if the README contained the docs about common things like returning an action instead of referring to unist-util-visit-parents.
It took me a little while to figure out that, like unist-util-visit-parents, this accepts returning an action (such as visit.SKIP
).
Removing the details of how to call the function and just linking to unist-util-visit-parents might work.
The example in the readme on main uses import
, so it seems like just a new publish is needed.
Is it possible to modify nodes in an immutable way? eg,
// Redact all text nodes
node = visit(node, (child) => {
if (child.type === 'text') {
return { ...child, value: '[redacted]' }
}
})
Affected packages and versions: latest
Install in Node 10 environment (I haven't tested on other node versions). Receive the following error:
Link to code example: https://github.com/googleapis/release-please/pull/870/checks?check_run_id=2390814891
Library compiles.
I believe the goal is perhaps a dual-mode module (that would work for ESM and CJS?). This might just take some fiddling with extensions and export maps.
Here's what we landed on for yargs:
https://github.com/yargs/yargs/blob/master/package.json#L5
It's ugly but works on most Node versions.
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/runner/work/release-please/release-please/node_modules/unist-util-visit/index.js
require() of ES modules is not supported.
require() of /home/runner/work/release-please/release-please/node_modules/unist-util-visit/index.js from /home/runner/work/release-please/release-please/build/src/util/to-conventional-changelog-format.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/runner/work/release-please/release-please/node_modules/unist-util-visit/package.json.
unist-util-visit
No response
In following code cannot find types for tagName, properties and children.
const result = await unified()
.use(remarkParse, { fragment: true })
.use(remarkRehype)
.use(rehypeSlug)
.use(() => {
return (tree) => {
console.log(tree);
visit(tree, 'element', (node) => {
// @ts-ignore
if (node.tagName == 'h2') {
// console.log(node);
// @ts-ignore
const id = node.properties.id;
// @ts-ignore
const heading = node.children[0].value;
toc.push({ id, title: heading });
}
});
};
})
.use(rehypeStringify)
.process(fileContent);
return toc;
}
Types should be recognized.
node is of type 'never'
node@20 lts
No response
No response
No response
Is it safe to assume this is synchronous and will remain so? Might be helpful to add this in readme somewhere. 😄
e.g.
var defs = []
visit(ast, function (node, index, parent) {
if (node.type === 'definition') {
defs.push(node)
}
})
console.log(defs)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.