How to use the clone function from @babel/types

Find comprehensive JavaScript @babel/types.clone code examples handpicked from public code repositorys.

@babel/types.clone creates a deep clone of an abstract syntax tree (AST) node in a Babel plugin.

751
752
753
754
755
756
757
758
759
760
let lastParentFn
traverse.default(ast, {
  CallExpression (path) {
    if (isTTestAwaitSomething(path)) {
      const clonedPath = btypes.clone(path.node)
      const awaitexpr = btypes.clone(path.node.arguments[0])
      const curParentFn = path.getFunctionParent()
      let result
      if (lastParentFn == null) {
        lastParentFn = curParentFn
fork icon0
star icon15
watch icon1

383
384
385
386
387
388
389
390
391
392
// Add assignment to exports object for namespace imports and commonjs.
path.insertAfter(
  EXPORT_ASSIGN_TEMPLATE({
    EXPORTS: getExportsIdentifier(asset, path.scope),
    NAME: t.identifier('default'),
    LOCAL: t.clone(identifier)
  })
);

if (t.isIdentifier(declaration)) {
fork icon0
star icon1
watch icon1

How does @babel/types.clone work?

@babel/types.clone is a utility function provided by the @babel/types package that creates a deep clone of an abstract syntax tree (AST) node, making sure that any nested nodes are also cloned. It is useful when working with AST nodes to avoid unintended side effects due to mutations of original nodes. The function creates a new node with the same properties as the original node, but with new identity and memory location.

Ai Example

1
2
3
4
5
6
7
8
const { clone } = require("@babel/types");

const originalNode = {
  type: "Identifier",
  name: "foo",
};

const clonedNode = clone(originalNode);

In this example, we're importing the clone function from the @babel/types module. We then create an original AST node object with a type of "Identifier" and a name of "foo". Finally, we use clone to create a deep copy of the original node object and assign it to clonedNode.

Other functions in @babel/types

Sorted by popularity

function icon

@babel/types.identifier is the most popular function in @babel/types (20936 examples)