How to use the valueToNode function from @babel/types

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

The @babel/types.valueToNode function in Babel is used to create an AST (Abstract Syntax Tree) node from a given value.

145
146
147
148
149
150
151
152
153
let binary
for (let i = 0; i < newExpressions.length; i++) {
  let left = binary
  let right = newExpressions[i]
  if (i === 0) {
    left = t.valueToNode(right.value.raw)
    binary = left
    continue
  }
fork icon44
star icon86
watch icon6

+ 47 other calls in file

2
3
4
5
6
7
8
9
10
11

module.exports = {
  join: function () {
    var args = Array.prototype.slice.call(arguments);
    var str = path.join.apply(path, args);
    return t.valueToNode(str);
  },
  resolve: function () {
    var args = Array.prototype.slice.call(arguments);
    var str = path.resolve.apply(path, args);
fork icon11
star icon33
watch icon8

+ 15 other calls in file

How does @babel/types.valueToNode work?

@babel/types.valueToNode() is a method provided by Babel that takes a JavaScript value and converts it into an AST node that can be used for manipulation by other Babel plugins or transformers. The output AST node is determined based on the type of the input value, with various types of values (such as numbers, strings, booleans, and objects) being mapped to corresponding AST nodes (such as NumericLiteral, StringLiteral, BooleanLiteral, and ObjectExpression).

36
37
38
39
40
41
42
43
44
// Replace identifiers with known values
path.traverse({
  Identifier: function (ident) {
    var key = ident.node.name;
    if (key in vars) {
      replaceWith(ident, t.valueToNode(vars[key]));
    }
  }
});
fork icon11
star icon33
watch icon8

+ 39 other calls in file

8
9
10
11
12
13
14
15
16
17
readFile: notSupported('readFile'),
readdir: notSupported('readdir'),

readdirSync: function (file) {
  var list = fs.readdirSync(file);
  return t.valueToNode(list);
},

readFileSync: function (file, enc) {
  var isBuffer = false;
fork icon11
star icon33
watch icon8

Ai Example

1
2
3
4
5
6
const t = require("@babel/types");

const value = "Hello, world!";

const stringLiteralNode = t.valueToNode(value);
console.log(stringLiteralNode);

In this example, @babel/types is used to convert the JavaScript string "Hello, world!" into an AST node using the valueToNode function. The resulting AST node is a string literal node, which can be printed to the console using console.log.

52
53
54
55
56
57
58
59
60
61
// Evaluate the binary expression
let { confident, value } = path.evaluate();
// Skip if not confident
if (!confident) return;
// Create a new node, infer the type
let actualVal = t.valueToNode(value);
// Skip if not a Literal type (e.g. StringLiteral, NumericLiteral, Boolean Literal etc.)
if (!t.isStringLiteral(actualVal)) return;
// Replace the BinaryExpression with the simplified value
path.replaceWith(actualVal);
fork icon1
star icon5
watch icon1

+ 19 other calls in file

103
104
105
106
107
108
109
110
111
112
  )
    return;
  let { confident, value } = path.evaluate(); // Evaluate the binary expression
  if (!confident || value == Infinity || value == -Infinity) return; // Skip if not confident

  const newNode = t.valueToNode(value);

  if (t.isBinaryExpression(newNode) || t.isUnaryExpression(newNode)) return;
  path.replaceWith(newNode); // Replace the BinaryExpression with a new node of inferred type
},
fork icon0
star icon19
watch icon1

+ 7 other calls in file

357
358
359
360
361
362
363
364
365
366
        } else {
            throw new Error;
        }
    }
} catch {
        path.node.arguments[0] = types.valueToNode(eval(generator(path.node.arguments[0]).code));
        let  newname = decodeAstNode.id.name + eval(generator(path.node.arguments[0]).code);
        //深拷贝
        let newDecodAstNode = parser.parse(generator(decodeAstNode).code).program.body[0];
        newDecodAstNode.id.name = newname;
fork icon6
star icon18
watch icon1

+ 5 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)