How to use the isLiteral function from @babel/types
Find comprehensive JavaScript @babel/types.isLiteral code examples handpicked from public code repositorys.
GitHub: qooxdoo/qooxdoo
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) ||
257
758
0
+ 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() }
223
0
26
+ 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) ) }
5
25
6
GitHub: lk-li/spider_reverse
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();
54
85
6
GitHub: Ntrashh/BossASTDecode
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)
6
18
1
+ 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;
0
6
1
+ 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) ) {
0
3
3
+ 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; }
0
1
1
+ 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();
0
2
1
+ 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) {
0
0
1
+ 260 other calls in file
GitHub: c323fff25/Spider
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();
0
0
0
+ 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) {
0
0
0
GitHub: c323fff25/Spider
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()
0
0
0
+ 2 other calls in file
GitHub: dcloudio/uni-app
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()
0
0
506
@babel/types.identifier is the most popular function in @babel/types (20936 examples)