How to use the isPureish function from @babel/types

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

@babel/types.isPureish is a function provided by the Babel compiler that checks whether a given AST node represents a "pure" expression in JavaScript, i.e. an expression that has no side effects and always evaluates to the same result for a given set of inputs.

676
677
678
679
680
681
682
683
684
685
      if (!this.isPure(expression, constantsOnly)) return false;
    }

    return true;
  } else {
    return t.isPureish(node);
  }
}

setData(key, val) {
fork icon0
star icon1
watch icon1

+ 16 other calls in file

How does @babel/types.isPureish work?

@babel/types.isPureish works by examining a given AST node and determining whether it represents a "pure" expression in JavaScript.

When called, @babel/types.isPureish takes a Babel AST node as input and checks whether its type property corresponds to a pure expression type, such as ts.isIdentifier, ts.isLiteral, or ts.isBinaryExpression.

The function then examines the properties of the node to determine whether it has any side effects, such as modifying state, performing I/O operations, or relying on external state. If the node is determined to have no side effects, and to always evaluate to the same result for a given set of inputs, then @babel/types.isPureish returns true. Otherwise, it returns false.

Pure expressions are important in JavaScript because they allow developers to reason about the behavior of their code more easily, and can be optimized by compilers and runtimes to improve performance. By using @babel/types.isPureish, Babel plugin developers can programmatically identify and manipulate pure expressions in their code, allowing for more efficient and powerful transformations.

Ai Example

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

// define a JavaScript expression to analyze
const sourceCode = "a + 3";

// parse the expression into a Babel AST using @babel/parser
const ast = parse(sourceCode);

// check whether the root node of the AST is a pure expression
const isPure = t.isPureish(ast.program.body[0].expression);

console.log(`Is the expression "${sourceCode}" pure? ${isPure}`);

In this example, we're using @babel/types.isPureish to check whether a JavaScript expression is pure. We first define a string containing a simple expression that adds the variable a to the literal value 3. We then use the @babel/parser package to parse the expression into a Babel AST, creating a Program node with a single ExpressionStatement child. We then use @babel/types.isPureish to check whether the expression inside the ExpressionStatement is pure. In this case, the expression is a BinaryExpression node, which is considered pure because it has no side effects and always evaluates to the same result for a given set of inputs. When we run this code, it will output the following message to the console: vbnet Copy code

Other functions in @babel/types

Sorted by popularity

function icon

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