How to use the isReferenced function from @babel/types

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

@babel/types.isReferenced is a function in the Babel compiler suite that checks whether a given node in an abstract syntax tree is being referenced somewhere else in the code.

25
26
27
28
29
30
31
32
33
34
Identifier(node, asset, ancestors) {
  let parent = ancestors[ancestors.length - 2];
  if (
    VARS.hasOwnProperty(node.name) &&
    !asset.globals.has(node.name) &&
    types.isReferenced(node, parent)
  ) {
    asset.globals.set(node.name, VARS[node.name](asset));
  }
},
fork icon0
star icon2
watch icon2

+ 9 other calls in file

356
357
358
359
360
361
362
363
364
365
  return makeTrace(state.fileNameIdentifier, location.start.line, location.start.column);
}

function convertJSXIdentifier(node, parent) {
  if (t.isJSXIdentifier(node)) {
    if (node.name === "this" && t.isReferenced(node, parent)) {
      return t.thisExpression();
    } else if (t.isValidIdentifier(node.name, false)) {
      node.type = "Identifier";
    } else {
fork icon0
star icon0
watch icon1

+ 17 other calls in file

How does @babel/types.isReferenced work?

@babel/types.isReferenced is a function that checks whether a given node in the Abstract Syntax Tree (AST) is referenced somewhere else in the code or not. In more detail, this function takes in an AST node and a parent scope and returns a boolean indicating whether the node is referenced somewhere else in the scope or any of its child scopes. It does this by analyzing the node's context and determining if it is being used in any way that would require it to be referenced by other parts of the code.

105
106
107
108
109
110
111
112
113
114
115
116


};
exports.Scope = Scope;
const Referenced = {
  checkPath(path) {
    return t.isReferenced(path.node, path.parent);
  }


};
exports.Referenced = Referenced;
fork icon0
star icon0
watch icon0

23
24
25
26
27
28
29
30
31
};
module.exports = {
  Identifier(node, asset, ancestors) {
    let parent = ancestors[ancestors.length - 2];

    if (VARS.hasOwnProperty(node.name) && !asset.globals.has(node.name) && types.isReferenced(node, parent)) {
      asset.globals.set(node.name, VARS[node.name](asset));
    }
  },
fork icon0
star icon0
watch icon1

+ 13 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const t = require("@babel/types");

const ast = t.file(
  t.program([
    t.variableDeclaration("const", [
      t.variableDeclarator(t.identifier("x"), t.numericLiteral(42)),
    ]),
    t.functionDeclaration(
      t.identifier("foo"),
      [t.identifier("bar")],
      t.blockStatement([
        t.expressionStatement(
          t.assignmentExpression("=", t.identifier("x"), t.identifier("bar"))
        ),
      ])
    ),
  ])
);

// Check if the "x" variable is referenced in the "foo" function.
const x = ast.program.body[0].declarations[0].id;
const foo = ast.program.body[1];
const isReferenced = t.isReferenced(x, foo);
console.log(isReferenced); // true

In this example, we create an abstract syntax tree (AST) that represents a JavaScript program containing a const declaration of a variable named x and a function declaration named foo. We then use @babel/types.isReferenced to check if the x variable is referenced in the foo function, and the function returns true because the x variable is assigned the value of the bar parameter in the function.

Other functions in @babel/types

Sorted by popularity

function icon

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