power-assert-context-reducer-ast

raw JSON →
1.2.0 verified Fri May 01 auth: no javascript

Appends AST, tokens, and visitorKeys to power-assert context at runtime. Version 1.2.0 is the latest stable release. This package is part of the power-assert-runtime monorepo and is intended for use when the transpiler does not add AST metadata during compilation. It parses the assertion expression to generate the missing AST nodes, enabling power-assert's enhanced assertion messages even without a transpiler step. The package is lightweight and focused on a single responsibility within the power-assert ecosystem.

error TypeError: appendAst is not a function
cause Attempted to use ES module import syntax (import { appendAst } from ...) in a CommonJS-only package.
fix
Use require('power-assert-context-reducer-ast') instead.
error Cannot find module 'acorn'
cause acorn was not installed as a peer or transitive dependency; missing from node_modules.
fix
Run 'npm install acorn' or 'npm install power-assert-context-reducer-ast' to include all dependencies.
error TypeError: Cannot read property 'ast' of undefined
cause The source property is missing or undefined in the context object passed to appendAst.
fix
Ensure the context object has a source property with content, filepath, and line.
breaking Requires acorn as a dependency but does not expose it; version mismatches may cause runtime errors.
fix Ensure acorn is installed as a transitive dependency; avoid directly requiring acorn from this package.
gotcha The input context must not already have ast, tokens, or visitorKeys; if present, they will be overwritten.
fix Check if context.source.ast is undefined before calling appendAst, or clone the context.
deprecated This package is part of the power-assert-runtime monorepo and may receive less frequent updates.
fix Consider using power-assert directly with a transpiler plugin that embeds AST at compile time.
npm install power-assert-context-reducer-ast
yarn add power-assert-context-reducer-ast
pnpm add power-assert-context-reducer-ast

Demonstrates how to use appendAst to add AST, tokens, and visitorKeys to a power-assert context object.

var appendAst = require('power-assert-context-reducer-ast');

// Given a powerAssertContext object without ast, tokens, visitorKeys
var context = {
  source: {
    content: 'assert(foo === bar)',
    filepath: 'test/some_test.js',
    line: 1
  },
  args: [
    {
      value: false,
      events: [
        { value: 'FOO', espath: 'arguments/0/left' },
        { value: 'BAR', espath: 'arguments/0/right' },
        { value: false, espath: 'arguments/0' }
      ]
    }
  ]
};

var appendedContext = appendAst(context);
console.log(appendedContext.source.ast);
// prints JSON representation of AST nodes