Skip to content

Instantly share code, notes, and snippets.

@xtuc
Last active April 17, 2019 04:43
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xtuc/26931c424fb7148c000217de8e7fdf76 to your computer and use it in GitHub Desktop.
Save xtuc/26931c424fb7148c000217de8e7fdf76 to your computer and use it in GitHub Desktop.

Path

remove

Removes the current path.

Example

Visistor:

BooleanLiteral(path) {
  path.remove();
}

In:

var bar;
true

Out:

var bar;

replaceWithMultiple(nodes: [Object])

Replaces the current path with an array of nodes.

Example

Visistor:

BooleanLiteral(path) {
  const nodes = [
    t.identifier("foo"),
    t.identifier("bar")
  ];

  path.replaceWithMultiple(nodes);
}

In:

true

Out:

foo
bar

replaceWithSourceString(replacement: string)

Replaces the current path with the replacement. Replacement will be parsed and must be an expression.

Example

Visistor:

BooleanLiteral(path) {
  path.replaceWithSourceString('Date.now()')
}

In:

true

Out:

Date.now()

replaceWith(replacement: Object)

Replaces the current path a node.

Example

Visistor:

BooleanLiteral(path) {
  path.replaceWithMultiple(t.identifier("bar"));
}

In:

true

Out:

bar

insertBefore(nodes: [Object])

Inserts nodes before the current one.

Example

Visistor:

BooleanLiteral(path) {
  const nodes = [
    t.returnStatement()
  ];

  path.insertBefore(nodes);
}

In:

true

Out:

return;
true

insertAfter(nodes: [Object])

Inserts nodes after the current one.

Scope

generateUidIdentifier(name: string = "temp")

Generates an uniq ID and returns it as an identifier.

Example

Visistor:

Identifier(path) {
  path.node.name = path.scope.generateUidIdentifier().name;
}

In:

var foo = "test";

Out:

var _temp = "test";

generateUid(name: string = "temp")

Generates an uniq ID and returns it as a string.

Example

Visistor:

Identifier(path) {
  path.node.name = path.scope.generateUid();
}

In:

var foo = "test";

Out:

var _temp = "test";

generateUidIdentifierBasedOnNode(node: Object, defaultName?: string)

Generates an uniq ID based on the name of the node and returns it as an identifier.

Example

Visistor:

Identifier(path) {
  path.node.name = path.scope.generateUidIdentifierBasedOnNode(path.node).name;
}

In:

var foo = "test";

Out:

var _foo = "test";

rename(oldName: string, newName?: string, block?: Object)

Rename the given name in the current scope.

Example

Visistor:

Identifier(path) {
  path.scope.rename("foo");
}

In:

var foo = "test";

Out:

var _foo = "test";

hasLabel(name: string)

Returns true if a label matches the name exists in the current scope.

Example

Visistor:

BooleanLiteral(path) {
  console.log(path.scope.hasLabel("foo"))
}

In:

foo: {
  true // log false
}

getLabel(name: string)

Returns the label matching the name from the current scope.

Example

Visistor:

Program(path) {
  console.log(path.scope.getLabel("foo"))
}

In:

foo: {
}

getBinding(name: string)

Returns the binding matching the name.

getBindingIdentifier(name: string)

Returns the identifier matching the name.

getOwnBindingIdentifier(name: string)

Returns the identifier matching the name in the current scope.

hasOwnBinding(name: string)

Returns true if an identifier matches the name in the current scope.

parentHasBinding(name: string, noGlobals?: boolean)

Returns true if an identifier matches the name in the parent scope.

moveBindingTo(name: string, scope: Object)

Moves a binding to a given scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment