The `cst` package provides a Concrete Syntax Tree (CST) implementation for JavaScript, distinguishing itself from Abstract Syntax Trees (ASTs) by preserving all source code information, including whitespace, comments, and punctuation. This makes it particularly useful for applications requiring precise code transformation, refactoring, linting, and style enforcement, where original formatting must be maintained. The library aims for 100% compatibility with the ESTree AST specification, ensuring that its `Node` structures align with standard AST representations. A core principle is that the tree always remains valid, protecting against structural inconsistencies during modifications. The current stable version is 0.4.10, but its last publish date was over six years ago, indicating that it is in a maintenance state with no active development or new releases. Despite this, its core functionality for detailed source code representation and manipulation remains sound. Key differentiators include its complete representation of the source text, ESTree AST compatibility, and robust mutation methods designed to preserve code integrity and formatting.
npm install cstVerified import paths — ran on the pinned version, not inferred.
Demonstrates parsing JavaScript code into a CST, traversing the tree to find a specific string literal token, and then modifying that token's value while preserving surrounding whitespace and comments.
Update code to use standard JavaScript array/generator syntax or ensure input does not contain comprehensions.
Review any code that uses `Token.cloneElement` (or its inherited version) to ensure it behaves as expected after the fix.
Always use methods like `parent.replaceChildren(newElement, oldElementRef, oldElementRef)` to perform modifications, constructing new `Node` or `Token` instances as needed.
Ensure the input string is valid JavaScript code compatible with the parser's grammar (ES5/ES6+). Check the exact line and column indicated in the error message.
Verify that `someElement` is a `Node` and is the parent of the `Element`s you intend to replace. Ensure the parent element is part of a valid CST structure.
Add error handling around `parse()` calls and null checks when traversing the CST, especially after operations that might return `undefined`.
No dependency data recorded yet.