How to use the isLiteral function from @babel/types

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

987
988
989
990
991
992
993
994
995
996
},

LogicalExpression: {
  exit(path) {
    let node = path.node;
    if (types.isLiteral(node.left) && types.isLiteral(node.right)) {
      let result =
        (node.operator == "&&" &&
          node.left.value &&
          node.right.value) ||
fork icon257
star icon758
watch icon0

+ 65 other calls in file

115
116
117
118
119
120
121
122
123
124
return {
    BinaryExpression (path) {
        const { left, right } = path.node
        const hasIdentifier = [left, right].some((a) => t.isIdentifier(a))
        if (hasIdentifier) return
        if (t.isLiteral(left) && t.isLiteral(right)) {
            const { confident, value } = path.evaluate()
            confident && path.replaceWith(t.valueToNode(value))
            path.skip()
        }
fork icon223
star icon0
watch icon26

+ 29 other calls in file

104
105
106
107
108
109
110
111
112
113
isMatchedRequireCall(node) {
  const re = new RegExp(/@mfelibs\/hybrid-common/, 'g')

  return (
    t.isIdentifier(node.callee, { name: 'require' }) &&
    t.isLiteral(node.arguments[0]) &&
    !t.isTemplateLiteral(node.arguments[0]) &&
    node.arguments[0].value.match(re)
  )
}
fork icon5
star icon25
watch icon6

31
32
33
34
35
36
37
38
39
40

function eval_constant(path) {
    // 常量计算
    if (path.type == "UnaryExpression") {
        const {operator, argument} = path.node;
        if (operator == "-" && t.isLiteral(argument)) {
            return;
        }
    }
    const {confident, value} = path.evaluate();
fork icon54
star icon85
watch icon6

83
84
85
86
87
88
89
90
91
92

if (switchNode != null && types.isMemberExpression(switchNode.discriminant) && switchNode.discriminant.property.operator == "++") {
    //获取当前节点的上一级节点
    lastNode = path.getPrevSibling().node
    //判断上一级属性的type是否是VariableDeclaration,declarations的长度是否为2,declarations[0]的init是否是Literal declarations[1]的init是否是数组
    if (types.isVariableDeclaration(lastNode) && lastNode.declarations.length == 2 && types.isLiteral(lastNode.declarations[0].init) && types.isArrayExpression(lastNode.declarations[1].init)) {

        //获取上一级节点的初始值和列表
        let index = lastNode.declarations[0].init.value;
        let arr = getArr(lastNode.declarations[1].init.elements)
fork icon6
star icon18
watch icon1

+ 2 other calls in file

381
382
383
384
385
386
387
388
389
390
AssignmentExpression: (path) => {
  const { node } = path;

  if (
    t.isMemberExpression(node.left) &&
    t.isLiteral(node.right) &&
    node.left.object
  ) {
    const constantIdentifier = node.left.object.name;
    if (!constantIdentifier) return;
fork icon0
star icon6
watch icon1

+ 36 other calls in file

209
210
211
212
213
214
215
216
217
218
  (statement &&
    (t.isCallExpression(argumentPath.node) ||
      t.isNewExpression(argumentPath.node) ||
      t.isIdentifier(argumentPath.node))) ||
  t.isMemberExpression(argumentPath.node) ||
  t.isLiteral(argumentPath.node) ||
  t.isObjectExpression(argumentPath.node) ||
  t.isFunction(argumentPath.node) ||
  t.isClass(argumentPath.node)
) {
fork icon0
star icon3
watch icon3

+ 28 other calls in file

151
152
153
154
155
156
157
158
159
160
  return;
}

let name;

if (id && t.isLiteral(id)) {
  name = getNameFromLiteralId(id);
} else if (id && t.isIdentifier(id)) {
  name = id.name;
}
fork icon0
star icon1
watch icon1

+ 17 other calls in file

513
514
515
516
517
518
519
520
521
522
523


function eval_constant(path) {
    // 常量计算
    if (path.type === "UnaryExpression") {
        const {operator, argument} = path.node;
        if (operator === "-" && t.isLiteral(argument)) {
            return;
        }
    }
    const {confident, value} = path.evaluate();
fork icon0
star icon2
watch icon1

+ 14 other calls in file

370
371
372
373
374
375
376
377
378
379

if (isAnyMemberExpression(id)) {
  if (
    isAnyIdentifier(id.object) &&
    id.object.name === "Symbol" &&
    (isAnyIdentifier(id.property) || t.isLiteral(id.property))
  ) {
    const propertyName = getNameFromId(id.property);

    if (propertyName) {
fork icon0
star icon0
watch icon1

+ 260 other calls in file

14
15
16
17
18
19
20
21
22
23
24


function param_reddefinition(path){
    // 形参重定义
    let name = path.node.left.name;
    let binding = path.scope.getOwnBinding(name);
    if (binding && binding.constantViolations.length === 1 && t.isLiteral(path.node.right)) {
        for (let p of binding.referencePaths) {
            p.replaceWith(path.node.right);
        }
        path.remove();
fork icon0
star icon0
watch icon0

+ 4 other calls in file

42
43
44
45
46
47
48
49
50
51
52
53
    return false;
  }
}


function isType(node) {
  return t.isLiteral(node) || t.isObjectExpression(node) || t.isArrayExpression(node) || t.isIdentifier(node) || t.isMemberExpression(node);
}


const nodes = {
  AssignmentExpression(node) {
fork icon0
star icon0
watch icon0

47
48
49
50
51
52
53
54
55
56
// function形参定义后函数体内赋值混乱还原
const params_str = {
    AssignmentExpression(path){
        let name = path.node.left.name
        let binding = path.scope.getOwnBinding(name)
        if (binding && binding.constantViolations.length === 1 && t.isLiteral(path.node.right)){
            for (let i = 0; i < binding.referencePaths.length; i++){
                binding.referencePaths[i].replaceWith(path.node.right)
            }
            path.remove()
fork icon0
star icon0
watch icon0

+ 2 other calls in file

72
73
74
75
76
77
78
79
80
81
if ( // t.msg || t['msg']
  t.isMemberExpression(parentNode) &&
  parentNode.object === path.node &&
  (
    t.isIdentifier(parentNode.property) ||
    t.isLiteral(parentNode.property)
  )
) {
  isFilter = true
  path.stop()
fork icon0
star icon0
watch icon506

Other functions in @babel/types

Sorted by popularity

function icon

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