How to use the isNumericLiteral function from babel-types

Find comprehensive JavaScript babel-types.isNumericLiteral code examples handpicked from public code repositorys.

babel-types.isNumericLiteral is a function that determines whether a given node is a numeric literal or not.

25
26
27
28
29
30
31
32
33
34
module.exports = function(code, existsScope = () => false, el) {
  const ast = babylon.parse(code);
  const iters = getIters(el);

  function isLiteral(node) {
    return t.isNumericLiteral(node) || t.isStringLiteral(node);
  }

  const visitor = {
    MemberExpression(path) {
fork icon664
star icon0
watch icon2

+ 13 other calls in file

3
4
5
6
7
8
9
10
11
12
const visitor = {
  BinaryExpression(path) {
    const node = path.node;
    let result;
    // 判断表达式两边,是否都是数字
    if (t.isNumericLiteral(node.left) && t.isNumericLiteral(node.right)) {
      // 根据不同的操作符作运算
      switch (node.operator) {
        case "+":
          result = node.left.value + node.right.value;
fork icon37
star icon84
watch icon5

+ 16 other calls in file

How does babel-types.isNumericLiteral work?

babel-types.isNumericLiteral is a function that takes an AST node and checks if it represents a numeric literal (i.e., a literal value representing a number) in the code. It returns true if the node is a numeric literal and false otherwise. It checks for both integer and floating point numbers.

75
76
77
78
79
80
81
82
83
84
    }
},
MemberExpression(path) {
    const node = path.node;
    const property = node.property;
    if (!t.isCallExpression(path.parentPath.node) && t.isNumericLiteral(property)) {
        path.replaceWith(memberExpression({
            OBJECT: node.object,
            PROPERTY: property
        }));
fork icon1
star icon0
watch icon0

+ 7 other calls in file

57
58
59
60
61
62
63
64
65
66
67
      }
    },
  };
}


var isLiteral = (n) => t.isNumericLiteral(n) ||
t.isBooleanLiteral(n) ||
t.isNullLiteral(n) ||
t.isStringLiteral(n) ||
t.isRegExpLiteral(n);
fork icon0
star icon1
watch icon2

+ 2 other calls in file

Ai Example

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

const node = t.numericLiteral(42);

if (t.isNumericLiteral(node)) {
  console.log("It's a numeric literal!");
} else {
  console.log("It's not a numeric literal!");
}

In this example, we use the numericLiteral() method from the @babel/types package to create a new numeric literal node with a value of 42. We then use the isNumericLiteral() method to check if the node we created is a numeric literal or not. In this case, the method will return true, so we print "It's a numeric literal!" to the console.

Other functions in babel-types

Sorted by popularity

function icon

babel-types.identifier is the most popular function in babel-types (4076 examples)