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.
Common errors
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.
Warnings
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.
Install
npm install power-assert-context-reducer-ast yarn add power-assert-context-reducer-ast pnpm add power-assert-context-reducer-ast Imports
- appendAst wrong
import { appendAst } from 'power-assert-context-reducer-ast';correctvar appendAst = require('power-assert-context-reducer-ast'); - default wrong
var appendAst = require('power-assert-context-reducer-ast').default;correctvar appendAst = require('power-assert-context-reducer-ast'); - appendAst wrong
const { appendAst } = require('power-assert-context-reducer-ast');correctconst appendAst = require('power-assert-context-reducer-ast');
Quickstart
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