@wessberg/ts-clone-node

A library that helps you clone Nodes from a Typescript AST

this package has been renamed to ts-clone-node. Please install ts-clone-node instead
Last updated 5 years ago by wessberg .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @wessberg/ts-clone-node 
SYNC missed versions from official npm registry.
Logo

A library that helps you clone Nodes from a Typescript AST

Downloads per month NPM version Dependencies Contributors code style: prettiercode style: Standard License: MIT Support on Patreon

Description

The Typescript Compiler API is very powerful and comes with a lot of create and update functions that can be used for creating and updating nodes in Custom transformers while visiting a SourceFile. Under such circumstances, it is easy to run into problems if you reuse a Node in another part of the tree without properly cloning it, since the parent chain, as well as the pos and end values will have wrong values and will lead to malformed output after your transformations have been applied.

This can be cumbersome for example when you want to simply add or remove a specific modifier from an arbitrary node in a given position. This library exports a cloneNode function that makes it easy to deep-clone a Node from a Typescript AST without any faulty parent links. Additionally, you get a simple hook with which you can do simple things such as edit the top-level properties of the cloned object such as its modifiers, decorators, etc.

Features

  • Simple to use
  • Extensible
  • Supports dynamic TypeScript versions

Table of Contents

Install

npm

$ npm install @wessberg/ts-clone-node

Yarn

$ yarn add @wessberg/ts-clone-node

pnpm

$ pnpm add @wessberg/ts-clone-node

Peer Dependencies

@wessberg/ts-clone-node depends on typescript, so you need to manually install this as well.

Usage

To clone a Node from a Typescript AST, all you have to do is:

import {cloneNode} from "@wessberg/ts-clone-node";

// Clone the Node
const clonedNode = cloneNode(someNode);

Configuration

Hooking into and altering transformations

You can pass in a hook that enables you to modify the clone, agnostic to the kind of Node it is. For example:

import {cloneNode} from "@wessberg/ts-clone-node";

// Clone the Node, and alter the modifiers such that they don't include a modifier pointing
// to the 'declare' keyword
const clonedNode = cloneNode(someNode, {
	hook: node => {
		return {
			modifiers: modifiers => ensureNoDeclareModifier(modifiers)
		};
	}
});

There is also a 'finalize' which is invoked after a node has been cloned at any recursive step from the top node, allowing you to perform final alterations or track the node for other purposes.

const clonedNode = cloneNode(someNode, {
	finalize: (clonedNode, oldNode) => trackSomething(clonedNode, oldNode)
});

Passing in a specific TypeScript version

You can use pass a specific TypeScript to use as an option to cloneNode:

cloneNode(someNode, {
	typescript: specialTypescriptVersion
});

This can be useful, for example, in an environment where multiple packages in the same project depends on different TypeScript versions and you're relying on cloneNode.

Passing in a specific NodeFactory

From TypeScript v4 and forward, a NodeFactory can be retrieved from a TransformationContext to signal which transformer was responsible for creating or altering nodes. If you want to pass a specific NodeFactory, you can pass it as an option to cloneNode:

cloneNode(someNode, {
	factory: nodeFactoryFromTransformationContext
});

Setting parent pointers

By default, when you clone a node, it won't update the parent pointers such that you and TypeScripts compiler APIs can traverse the parent tree. You can toggle this behavior with the setParents option:

cloneNode(someNode, {
	setParents: true
});

Setting original node pointers

By default, when you clone a node, it won't keep references to the original nodes recursively. You can toggle this behavior with the setOriginalNodes option:

cloneNode(someNode, {
	setOriginalNodes: true
});

Preserving comments

By default, when you clone a node, comments will be preserved as much as possible and added to the cloned nodes as emitNodes. You can toggle this behavior with the preserveComments option:

cloneNode(someNode, {
	preserveComments: false
});

Preserving symbols

By default, when you clone a node, it won't preserve symbols from the original nodes. You can toggle this behavior with the preserveSymbols option:

cloneNode(someNode, {
	preserveSymbols: true
});

Contributing

Do you want to contribute? Awesome! Please follow these recommendations.

Maintainers

Frederik Wessberg
Frederik Wessberg
Twitter: @FredWessberg
Github: @wessberg
Lead Developer

Backers

Become a sponsor/backer and get your logo listed here.

Bubbles Christopher Blanchard Ideal Postcodes Xerox
Bubbles
Twitter: @usebubbles
Christopher Blanchard Ideal Postcodes Xerox

Patreon

Patrons on Patreon

FAQ

What is the point of this library

If you've run into the kind of trouble I'm explaining here, you'll understand. If not, I'm happy for you. You can move along!

License

MIT © Frederik Wessberg (@FredWessberg) (Website)

Current Tags

  • 0.3.19                                ...           latest (4 years ago)

28 Versions

  • 0.3.19 [deprecated]           ...           4 years ago
  • 0.3.18 [deprecated]           ...           4 years ago
  • 0.3.17 [deprecated]           ...           4 years ago
  • 0.3.16 [deprecated]           ...           4 years ago
  • 0.3.15 [deprecated]           ...           4 years ago
  • 0.3.14 [deprecated]           ...           4 years ago
  • 0.3.13 [deprecated]           ...           4 years ago
  • 0.3.12 [deprecated]           ...           4 years ago
  • 0.3.11 [deprecated]           ...           4 years ago
  • 0.3.10 [deprecated]           ...           4 years ago
  • 0.3.9 [deprecated]           ...           4 years ago
  • 0.3.8 [deprecated]           ...           5 years ago
  • 0.3.7 [deprecated]           ...           5 years ago
  • 0.3.6 [deprecated]           ...           5 years ago
  • 0.3.5 [deprecated]           ...           5 years ago
  • 0.3.4 [deprecated]           ...           5 years ago
  • 0.3.3 [deprecated]           ...           5 years ago
  • 0.3.2 [deprecated]           ...           5 years ago
  • 0.3.1 [deprecated]           ...           5 years ago
  • 0.3.0 [deprecated]           ...           5 years ago
  • 0.2.1 [deprecated]           ...           5 years ago
  • 0.2.0 [deprecated]           ...           5 years ago
  • 0.1.4 [deprecated]           ...           5 years ago
  • 0.1.3 [deprecated]           ...           5 years ago
  • 0.1.2 [deprecated]           ...           5 years ago
  • 0.1.1 [deprecated]           ...           5 years ago
  • 0.1.0 [deprecated]           ...           5 years ago
  • 0.0.0 [deprecated]           ...           5 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (17)

© 2010 - cnpmjs.org x YWFE | Home | YWFE