How to use the isVariableDeclaration function from @babel/types

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

@babel/types.isVariableDeclaration is a function provided by Babel that checks whether a given object represents a variable declaration.

150
151
152
153
154
155
156
157
158
159
Program: {
        exit: function (path) {
                var {body} = path.node;

                var node = body.find(node => (
                        t.isVariableDeclaration(node) &&
                        (node = node.declarations[0]) &&
                        t.isIdentifier(node.id) &&
                        node.id.name === 'requireJSX'
                ));
fork icon2
star icon34
watch icon2

+ 33 other calls in file

204
205
206
207
208
209
210
211
212
213

traverse(ast, {
  Declaration({node}) {
    const {loc} = node

    if (t.isFunctionDeclaration(node) || t.isVariableDeclaration(node) || t.isClassDeclaration(node)) {
      let name

      if (t.isVariableDeclaration(node)) {
        name = node.declarations[0].id.name
fork icon0
star icon6
watch icon0

+ 10 other calls in file

How does @babel/types.isVariableDeclaration work?

@babel/types.isVariableDeclaration works by examining a given object and determining whether it represents a variable declaration.

In JavaScript, a variable declaration is a statement that declares a new variable using the var, let, or const keywords. A variable declaration can declare multiple variables at once, separated by commas.

To check whether a given object represents a variable declaration, @babel/types.isVariableDeclaration checks whether the object has a type property that equals VariableDeclaration, which is a string that represents a variable declaration in the Babel AST.

If the object is determined to represent a variable declaration, then @babel/types.isVariableDeclaration returns true. Otherwise, it returns false.

By using @babel/types.isVariableDeclaration, Babel plugin developers can programmatically determine whether a given object represents a variable declaration in the code they are processing, allowing them to perform operations on variable declarations with confidence.

90
91
92
93
94
95
96
97
98
99
}
path.traverse({
    ThisExpression (thisPath) {
        const parentNode = thisPath.parentPath.parentPath.parent;
        const isValid = t.isExpressionStatement(parentNode) || 
            t.isVariableDeclaration(parentNode) ||
            t.isBlockStatement(parentNode) || 
            t.isJSXElement(parentNode) || 
            t.isCallExpression(parentNode) || 
            (t.isJSXAttribute(parentNode) && !parentNode.name.name.startsWith('on'));
fork icon65
star icon1
watch icon1

162
163
164
165
166
167
168
169
170
171
  if(t.isExpressionStatement(path.parentPath.parent)){
    candidates.unshift(path.parentPath.parent);
  }
}

if(t.isVariableDeclarator(path.parent) && t.isVariableDeclaration(path.parentPath.parent)){
  candidates.unshift(path.parentPath.parent);
}

let annotateEverything = getAnnotations(candidates);
fork icon156
star icon0
watch icon0

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
const { parse } = require("@babel/parser");
const { isVariableDeclaration } = require("@babel/types");

const code = 'const x = 42, y = "hello";';
const ast = parse(code);

ast.program.body.forEach((node) => {
  if (isVariableDeclaration(node)) {
    console.log("Found a variable declaration!");
    console.log(node);
  }
});

In this example, we're using @babel/types.isVariableDeclaration to check whether each node in an Abstract Syntax Tree (AST) represents a variable declaration. We first define some code that contains a variable declaration that declares two variables, x and y, using the const keyword. We then use @babel/parser to parse this code into an AST. We then iterate over each node in the AST's program.body array, checking whether each node represents a variable declaration using @babel/types.isVariableDeclaration. If a node represents a variable declaration, we log a message to the console along with the details of the node. When we run this code, it will output the following message to the console: bash Copy code

116
117
118
119
120
121
122
123
124
125
FunctionDeclaration(path) {
    const checkFunction = function (p) {
        const name = p.node.id.name;
        const body = p.node.body.body;
        return body.length === 3 &&
            types.isVariableDeclaration(body[0]) && body[0].declarations.length === 1 && (types.isArrayExpression(body[0].declarations[0].init) || types.isCallExpression(body[0].declarations[0].init)) &&
            types.isExpressionStatement(body[1]) && types.isAssignmentExpression(body[1].expression) && body[1].expression.left.name === name &&
            types.isReturnStatement(body[2]);
    }
    if (!checkFunction(path)) {
fork icon2
star icon1
watch icon1

+ 82 other calls in file

437
438
439
440
441
442
443
444
445
446
});

if (t.isFile(ast) && t.isProgram(ast.program) && ast.program.body.length === 1) {
  const exported = ast.program.body[0];

  if (t.isExportNamedDeclaration(exported) && t.isVariableDeclaration(exported.declaration) && exported.declaration.declarations.length === 1) {
    const declaration = exported.declaration.declarations[0];

    if (t.isVariableDeclarator(declaration) && t.isIdentifier(declaration.id)) {
      const {
fork icon1
star icon2
watch icon1

517
518
519
520
521
522
523
524
525
526

if (typeExp === "DECLARE" && types.isFunctionDeclaration(path.node)) {
  childToPut = types.callExpression(types.identifier(path.node.id.name), initAnnotationParser(immidateTopComment.value));
}

if (typeExp === "DECLARE" && types.isVariableDeclaration(path.node) && (types.isArrowFunctionExpression(path.node.declarations[0].init) || types.isFunctionExpression(path.node.declarations[0].init))) {
  // console.log("variable declarator", path.node);
  // if (
  //   types.isArrowFunctionExpression(path.node.declarations[0].init) ||
  //   types.isFunctionExpression(path.node.declarations[0].init)
fork icon0
star icon0
watch icon1

226
227
228
229
230
231
232
233
234
235
236
237
238


const getEnclosingProperty = path => path.findParent((path) => path.isProperty());


const isTopLevelDeclaredObject = (path) =>
  types.isVariableDeclarator(path.parentPath) &&
  types.isVariableDeclaration(path.parentPath.parentPath) &&
  types.isProgram(path.parentPath.parentPath.parentPath);


const handleCallExpressionEnter = (path) => {
  if (path.node.callee.type !== 'MemberExpression' ||
fork icon0
star icon0
watch icon0

+ 21 other calls in file

Other functions in @babel/types

Sorted by popularity

function icon

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