How to use the isNodesEquivalent function from @babel/types

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

@babel/types.isNodesEquivalent is a function that checks whether two nodes in a Babel AST are equivalent in structure and content.

18
19
20
21
22
23
24
25
26
27
function addToCypressConfigPlugin(toAdd, opts = {}) {
    debug(`adding %s`, toAdd);
    const { shouldThrow = true } = opts;
    function canAddKey(path, props, toAdd) {
        for (const prop of props) {
            if (t.isObjectProperty(prop) && t.isNodesEquivalent(prop['key'], toAdd['key'])) {
                if (shouldThrow) {
                    throw new Error(`Cannot add, the existing config has a ${(0, recast_1.print)(prop['key']).code} property`);
                }
                else {
fork icon0
star icon0
watch icon1

+ 4 other calls in file

128
129
130
131
132
133
134
135
136
137
138
139
function isObjectAssignment(path, objectId) {
    var expression = t.isAssignmentExpression(path) ? path : findExpression(path);
    var isMemberAssignment = t.isAssignmentExpression(expression) && t.isMemberExpression(expression.get('left'));


    if (isMemberAssignment) {
        return t.isNodesEquivalent(expression.get('left.object').node, objectId);
    }


    return false;
}
fork icon0
star icon0
watch icon0

+ 4 other calls in file

How does @babel/types.isNodesEquivalent work?

@babel/types.isNodesEquivalent is a function provided by the Babel library that checks whether two nodes in a Babel AST are equivalent in structure and content. The function takes two arguments, which are the nodes to be compared. It recursively walks through the nodes and their child nodes to compare their structure and content. If all nodes and their children are equivalent in structure and content, the function returns true, otherwise it returns false. The function is useful in situations where you need to compare two nodes in a Babel AST to determine whether they are the same or similar. This can be useful in tasks such as refactoring, code analysis, and code transformation. It's worth noting that @babel/types.isNodesEquivalent only compares the structure and content of the nodes, and not their position within the AST. This means that two nodes that are structurally equivalent but appear in different parts of the AST will still be considered equivalent by the function. By using @babel/types.isNodesEquivalent, developers can programmatically compare nodes in a Babel AST, which can be useful in a wide range of scenarios where they need to work with and manipulate ASTs.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const babel = require("@babel/core");
const types = require("@babel/types");

const code = `
const a = 1 + 2;
const b = 3 + 4;
`;

const ast = babel.parseSync(code);

const node1 = ast.program.body[0];
const node2 = ast.program.body[1];

console.log(types.isNodesEquivalent(node1, node2)); // false

In this example, we have a simple JavaScript code that consists of two variable declarations with different expressions. We use the babel.parseSync function to parse the code into an abstract syntax tree (AST). We then use the ast.program.body property to access the two nodes that represent the variable declarations. We store them in node1 and node2, respectively. Finally, we call types.isNodesEquivalent with node1 and node2 as arguments. Since the two nodes have different expressions, the function returns false. This demonstrates how @babel/types.isNodesEquivalent can be used to compare two nodes in a Babel AST and determine whether they are equivalent in structure and content.

Other functions in @babel/types

Sorted by popularity

function icon

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