Unist Generated Node Checker

3.0.0 · active · verified Sun Apr 19

unist-util-generated is a utility within the unified ecosystem designed to determine if a unist node is 'generated.' A generated node is one that did not originate from the source text of the document but was rather programmatically created or modified, lacking explicit positional information. This is particularly useful for tools like linters, which might want to skip generated content to avoid reporting false positives to human authors. The package is currently stable at version 3.0.0 and adheres to a semantic versioning release cadence, with major versions often coinciding with shifts in Node.js compatibility or module system adoption. Its primary differentiator is its focused role in identifying synthetic content within unist trees, complementing other utilities like unist-util-position for retrieving positional data and unist-util-stringify-position for displaying it.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `unist-util-generated` to check if various unist nodes are considered 'generated.' It illustrates that nodes lacking any `position` property, or those with empty `start` and `end` position objects, are flagged as generated, while nodes with valid line/column data are not.

import { generated } from 'unist-util-generated'
import type { Node } from 'unist'

// A completely empty object is considered generated as it lacks positional info.
const node1: Node = {}
console.log('Node 1 (empty object):', generated(node1))

// A node with empty start/end positions is also generated.
const node2: Node = {position: {start: {}, end: {}}}
console.log('Node 2 (empty position):', generated(node2))

// A node with complete positional information is NOT generated.
const node3: Node = {
  position: {start: {line: 1, column: 1}, end: {line: 1, column: 2}}
}
console.log('Node 3 (full position):', generated(node3))

// Example with a typical unist node structure
interface TextNode extends Node {
  type: 'text';
  value: string;
}

const originalTextNode: TextNode = {
  type: 'text',
  value: 'Hello',
  position: {
    start: {line: 1, column: 1, offset: 0},
    end: {line: 1, column: 6, offset: 5}
  }
}
console.log('Original text node:', generated(originalTextNode))

const programmaticallyAddedNode: TextNode = {
  type: 'text',
  value: 'World'
  // No position info, so it's generated
}
console.log('Programmatically added node:', generated(programmaticallyAddedNode))

view raw JSON →