Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
ts-clone-node
Advanced tools
A library that helps you clone Nodes from a Typescript AST
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.
Become a sponsor/backer and get your logo listed here.
Bubbles Twitter: @usebubbles | Xerox | Trent Raymond | scrubtheweb | Joel |
$ npm install ts-clone-node
$ yarn add ts-clone-node
$ pnpm add ts-clone-node
ts-clone-node
depends on typescript
, so you need to manually install this as well.
To clone a Node from a Typescript AST, all you have to do is:
import {cloneNode} from "ts-clone-node";
// Clone the Node
const clonedNode = cloneNode(someNode);
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 "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)
});
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
.
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
});
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
});
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
});
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
});
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
});
Do you want to contribute? Awesome! Please follow these recommendations.
Frederik Wessberg Twitter: @FredWessberg Github: @wessberg Lead Developer |
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!
MIT © Frederik Wessberg (@FredWessberg) (Website)
FAQs
A library that helps you clone Nodes from a Typescript AST
The npm package ts-clone-node receives a total of 48,585 weekly downloads. As such, ts-clone-node popularity was classified as popular.
We found that ts-clone-node demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.