How to use the unaryExpression function from @babel/types

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

112
113
114
115
116
117
118
119
120
121
    throw new CodeError(code, path.node, path.node.loc, 'Logical operator only support && or ||');
  }
} else if (rightIsJSX || hasJSX(rightPath)) {
  let test;
  if (operator === '||') {
    test = t.unaryExpression('!', left);
  } else if (operator === '&&') {
    test = left;
  } else {
    throw new CodeError(code, path.node, path.node.loc, 'Logical operator only support && or ||');
fork icon32
star icon66
watch icon12

+ 11 other calls in file

92
93
94
95
96
97
98
99
100
101
: mustNotBeFunction
? getValue(false)
: t.conditionalExpression(
    t.binaryExpression(
      '===',
      t.unaryExpression('typeof', id),
      t.stringLiteral('function')
    ),
    getValue(true),
    getValue(false)
fork icon27
star icon429
watch icon6

+ 15 other calls in file

321
322
323
324
325
326
327
328
329
330
} else if (operator === "||") {
    //TODO:好像不需要转换!!!!!
    // 转换前: "" == str || /\d/.test(num) || global.log();
    // 转换后: if(!("" == str || /\d/.test(num))) global.log();
    // 判断条件需取反
    // var unaryExpression = t.unaryExpression("!", exp.left)
    // var alternateBlockStatement = t.blockStatement([t.expressionStatement(exp.right)], [])
    // var ifStatement = t.ifStatement(unaryExpression, alternateBlockStatement)
    // try {
    //     path.replaceWith(ifStatement)
fork icon220
star icon0
watch icon0

72
73
74
75
76
77
78
79
80
81
//let
let dispenser = t.variableDeclaration('let', [varArray, varIndex]);
//拼装switch (+_array[_index++])
let uptExp = t.updateExpression('++', index);
let memExp = t.memberExpression(array, uptExp, true);
let discriminant = t.unaryExpression('+', memExp);
let switchSta = t.switchStatement(discriminant, cases);
//拼装while (!![])
let unaExp = t.unaryExpression('!', t.arrayExpression());
unaExp = t.unaryExpression('!', unaExp);
fork icon1
star icon6
watch icon1

+ 11 other calls in file

114
115
116
117
118
119
120
121
122
123

static ifTypeOfXEqWhat (prop, body, type) {
  return btypes.ifStatement(
    btypes.binaryExpression(
      '===',
      btypes.unaryExpression('typeof', EH.makeIdentifier(prop)),
      EH.makeStringLiteral(type)
    ),
    EH.ensureBlockStatement(body)
  )
fork icon1
star icon1
watch icon2

+ 11 other calls in file

545
546
547
548
549
550
551
552
553
554
    this.registerBinding("unknown", path);
  }
}

buildUndefinedNode() {
  return t.unaryExpression("void", t.numericLiteral(0), true);
}

registerConstantViolation(path) {
  const ids = path.getBindingIdentifiers();
fork icon0
star icon1
watch icon1

+ 16 other calls in file

303
304
305
306
307
308
309
310
311
312
babelTypes.identifier('undefined'),
babelTypes.conditionalExpression(
  babelTypes.binaryExpression(
    '===',
    babelTypes.stringLiteral('number'),
    babelTypes.unaryExpression(
      'typeof',
      babelTypes.identifier(tmpVar),
      true
    )
fork icon0
star icon0
watch icon1

+ 10 other calls in file

42
43
44
45
46
47
48
49
50
51
let varIndex = t.variableDeclarator(index, t.numericLiteral(0));
let dispenser = t.variableDeclaration('let',[varArray, varIndex]);
//生成switch中的表达式 +_array[_index++] 
let updExp = t.updateExpression('++', index);
let memExp = t.memberExpression(array, updExp, true);
let discriminant = t.unaryExpression("+", memExp);
//构建整个switch语句
let switchSta = t.switchStatement(discriminant, cases);
//生成while循环中的条件 !![]
let unaExp = t.unaryExpression("!", t.arrayExpression());
fork icon0
star icon0
watch icon0

+ 14 other calls in file

25
26
27
28
29
30
31
32
33
34
function asPure(root) {
        if (!t.isObjectExpression(root)) {
                throw new Error(`Invalid argument type: ${JSON.stringify(root && root.type)}, ObjectExpression expected`);
        }

        return t.unaryExpression('!', t.unaryExpression('!', root));
}

function getProp(obj, name) {
        if (!Array.isArray(obj.properties)) {
fork icon0
star icon0
watch icon2

+ 5 other calls in file

97
98
99
100
101
102
103
104
105
106
      }
    }
  })
  test = test.elements[0]
  if (path.key === 'alternate') {
    test = t.unaryExpression('!', test)
  }
  tests = tests ? t.logicalExpression('&&', test, tests) : test
}
path = path.parentPath
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)