Snapdragon Utilities
snapdragon-util provides a focused collection of utility functions designed to interact with the Abstract Syntax Tree (AST) nodes generated or consumed by the `snapdragon` parser/compiler ecosystem. Key functionalities include robust node type checking (`isNode`), efficient value extraction from nodes (`value`), and critical compiler middleware helpers such as `noop` (for ignoring nodes), `identity` (for direct value appending), `append` (for custom value appending), and `toNoop` (for transforming nodes into empty text nodes without re-indexing). The package is currently at version `5.0.1` and is part of a stable, actively maintained ecosystem primarily targeting Node.js environments. Its core differentiation lies in its tight integration with `snapdragon-node` instances, making it indispensable for extending or customizing `snapdragon`-based parsers and compilers.
Common errors
-
TypeError: util.isNode is not a function
cause Attempting to access a utility function as a named ESM import, or `util` itself is `undefined` due to incorrect `require` path.fixEnsure you are using `const util = require('snapdragon-util');` and accessing functions like `util.isNode`. If using ESM, use `import util from 'snapdragon-util';` and then `util.isNode`. -
ReferenceError: Node is not defined
cause When creating `snapdragon-node` instances to pass to `snapdragon-util` functions, `snapdragon-node` itself has not been imported.fixAdd `const Node = require('snapdragon-node');` at the top of your file before attempting to create `new Node(...)` instances.
Warnings
- breaking The `.emit` method was renamed to `.append`. Any code relying on `.emit` will break.
- gotcha This package is a CommonJS module. Attempting to use named ESM imports (e.g., `import { isNode } from 'snapdragon-util';`) will result in `undefined` for the imported functions or module not found errors in some environments without a transpilation step.
- gotcha Many `snapdragon-util` functions expect instances of `snapdragon-node`. Passing plain JavaScript objects may lead to unexpected behavior or `undefined` returns.
Install
-
npm install snapdragon-util -
yarn add snapdragon-util -
pnpm add snapdragon-util
Imports
- util
const util = require('snapdragon-util').default;import util from 'snapdragon-util';
- isNode
import { isNode } from 'snapdragon-util';const util = require('snapdragon-util'); const isNode = util.isNode; - append
import { append } from 'snapdragon-util';const util = require('snapdragon-util'); const append = util.append;
Quickstart
const util = require('snapdragon-util');
const Node = require('snapdragon-node');
// Create a simple AST node
const textNode = new Node({ type: 'text', value: 'Hello' });
const wildcardNode = new Node({ type: 'star', val: '*' });
// Use isNode to check node type
console.log('Is textNode a Snapdragon Node?', util.isNode(textNode));
console.log('Is a plain object a Snapdragon Node?', util.isNode({}));
// Extract value using the value utility
console.log('Value of textNode:', util.value(textNode));
console.log('Value of wildcardNode:', util.value(wildcardNode));
// Example of using 'append' for a compiler (conceptual)
// In a real snapdragon compiler, you'd set this as middleware
const mockCompiler = { output: '' };
const appendGreeting = util.append('Hello World');
// Simulate calling the append middleware with a node and a compiler context
appendGreeting.call(mockCompiler, { type: 'greeting' });
console.log('Mock compiler output after append:', mockCompiler.output);