How to use the numericLiteral function from @babel/types
Find comprehensive JavaScript @babel/types.numericLiteral code examples handpicked from public code repositorys.
The @babel/types.numericLiteral is a utility for creating an Abstract Syntax Tree (AST) node for numeric literals in JavaScript.
GitHub: qooxdoo/qooxdoo
122 123 124 125 126 127 128 129 130 131
} if (typeof value == "boolean") { return types.booleanLiteral(value); } if (typeof value == "number") { return types.numericLiteral(value); } if (typeof value == "string") { return types.stringLiteral(value); }
+ 65 other calls in file
15 16 17 18 19 20 21 22 23 24
} else if (typeof data === 'undefined') { return t.identifier('undefined') } else if (typeof data === 'string') { return t.stringLiteral(data) } else if (typeof data === 'number') { return t.numericLiteral(data) } else if (Array.isArray(data)) { return t.arrayExpression(data.map(stringifyRecursive)) } else if (data === Object(data)) { return t.objectExpression(
+ 5 other calls in file
How does @babel/types.numericLiteral work?
@babel/types.numericLiteral is a module in the Babel compiler that creates an Abstract Syntax Tree (AST) node representing a numeric literal in JavaScript, such as an integer or a floating-point number. When provided with a number value, the module returns an AST node that can be used in further processing, such as generating code or performing transformations on the AST.
93 94 95 96 97 98 99 100 101 102
traverse(this.ast, { NumericLiteral(path) { let value = path.node.value let key = parseInt(Math.random() * (999999 - 100000) + 100000, 10) let cipher = value ^ key path.replaceWith(t.binaryExpression('^', t.numericLiteral(cipher), t.numericLiteral(key))) path.skip() // 因为内部也有numericLiteral 就会导致死循环 }, }) }
+ 23 other calls in file
GitHub: youtube/cobalt
25 26 27 28 29 30 31 32 33 34 35
function createRandomNumber(value) { // TODO(ochang): Maybe replace with variable. const probability = random.random(); if (probability < 0.01) { return babelTypes.numericLiteral( random.randInt(MIN_SAFE_INTEGER, MAX_SAFE_INTEGER)); } else if (probability < 0.06) { return common.randomInterestingNumber(); } else {
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14
const { numericLiteral } = require("@babel/types"); module.exports = function ({ types: t }) { return { visitor: { NumericLiteral(path) { const { node } = path; const value = parseFloat(node.value); const replacement = numericLiteral(value * 2); path.replaceWith(replacement); }, }, }; };
In this example, the plugin doubles the value of any number literal it encounters using @babel/types.numericLiteral.
GitHub: 2833844911/cy_jsvmp
22 23 24 25 26 27 28 29 30 31
for (let i =0; i < path.node.left.declarations[0].id.elements.length; i++){ isduolist.push(path.node.left.declarations[0].id.elements[i]) hxdd.push(tee.variableDeclarator(path.node.left.declarations[0].id.elements[i], null)) } hxdd.push(tee.variableDeclarator(tee.identifier("cbbiyhh"), tee.numericLiteral(0))) initOfmyfor = tee.variableDeclaration("var", hxdd) }else if(path.node.left.type == "Identifier"){ isduolist.push(path.node.left) initOfmyfor = tee.variableDeclaration("var", [tee.variableDeclarator(tee.identifier("cbbiyhh"), tee.numericLiteral(0))])
+ 15 other calls in file
252 253 254 255 256 257 258 259 260 261
); } else { return types.objectProperty( types.stringLiteral(key), typeof value === 'number' ? types.numericLiteral(value) : types.stringLiteral(value) ); } });
19 20 21 22 23 24 25 26 27 28
} return t.stringLiteral(defaultValue); } if (_.isNumber(defaultValue)) { return t.numericLiteral(defaultValue); } return t.nullLiteral(); }
+ 15 other calls in file
GitHub: caiwuu/Typex
65 66 67 68 69 70 71 72 73 74
t.memberExpression( t.identifier('arguments'), t.binaryExpression( '-', t.memberExpression(t.identifier('arguments'), t.identifier('length'), false), t.numericLiteral(1) ), true ) ),
18 19 20 21 22 23 24 25 26 27
t.assertNumericLiteral(firstNumber); t.assertNumericLiteral(secondNumber); const sum = firstNumber.value + secondNumber.value; return t.numericLiteral(sum); }; ``` Use by importing and calling with two non-null-assertion operators.
+ 9 other calls in file
129 130 131 132 133 134 135 136 137
prevState = defineVarId.node.properties.find((n) => t.isNumericLiteral(n.key) && n.key.value === 0).value; canMerge = t.isIdentifier(prevState); updateFn = find.value; } } else if (t.isIdentifier(defineVarId.node)) { prevState = builder.buildMemberExpression(defineVarId.node, t.numericLiteral(0)); updateFn = builder.buildMemberExpression(defineVarId.node, t.numericLiteral(1)); canMerge = true; }
+ 7 other calls in file
GitHub: Leman-li/tanfu.js
178 179 180 181 182 183 184 185 186 187
function getLiteral(value) { if (value instanceof TanFuObjectExpression) return value.expression; if (value instanceof TanFuArrayExpression) return value.expression; if (t.isExpression(value)) return value; if (typeof value === 'string') return t.stringLiteral(value) if (typeof value === 'number') return t.numericLiteral(value) if (typeof value === 'boolean') return t.booleanLiteral(value) if (Object.prototype.toString.call(value)?.includes('Object')) return ObjectExpression(value) if (Object.prototype.toString.call(value).includes('Array')) return ArrayExpression(value) return t.nullLiteral()
+ 3 other calls in file
GitHub: gxlmyacc/react-vue-like
81 82 83 84 85 86 87 88 89 90
if (v === undefined) return; if (Array.isArray(v)) return arr2Expression(v, parent); switch (typeof v) { case 'string': return t.stringLiteral(v); case 'boolean': return t.booleanLiteral(v); case 'number': return t.numericLiteral(v); case 'object': if (v === null) return t.nullLiteral(); if (v instanceof RegExp) return t.regExpLiteral(v.source, v.flags); if (v instanceof Date) return template('new Date(TIME)')({ TIME: t.numericLiteral(v.getTime()) });
+ 17 other calls in file
51 52 53 54 55 56 57 58 59 60
if (item.sorter) { exArray.push(t.objectProperty(t.identifier('sorter'), t.booleanLiteral(item.sorter))) } if (item.width) { exArray.push(t.objectProperty(t.identifier('width'), _.isNumber(item.width) ? t.numericLiteral(item.width) : t.stringLiteral(item.width))) } if (item.filters) { exArray.push(t.objectProperty(t.identifier('filters'), getItemFilterExpression(item.filters))) }
GitHub: N0taN3rd/Emu
209 210 211 212 213 214 215 216 217 218
return btypes.binaryExpression( '===', btypes.callExpression(EH.memberExpression(id, 'indexOf'), [ EH.makeStringLiteral(what) ]), btypes.numericLiteral(0) ) } static setterOverride (sname, pi, rwcode) {
+ 11 other calls in file
48 49 50 51 52 53 54 55 56 57
"===", t.memberExpression( t.identifier('response'), t.identifier('errorCode') ), t.numericLiteral(0) ) ), t.blockStatement( // consequent [
GitHub: Leman-li/tanfu.js
241 242 243 244 245 246 247 248 249 250
if (value instanceof TanFuObjectExpression) return value.expression; if (value instanceof TanFuArrayExpression) return value.expression; if (t.isExpression(value)) return value; if (typeof value === 'string') return t.stringLiteral(value); if (typeof value === 'number') return t.numericLiteral(value); if (typeof value === 'boolean') return t.booleanLiteral(value); if ((_Object$prototype$toS = Object.prototype.toString.call(value)) === null || _Object$prototype$toS === void 0 ? void 0 : _Object$prototype$toS.includes('Object')) return ObjectExpression(value); if (Object.prototype.toString.call(value).includes('Array')) return ArrayExpression(value); return t.nullLiteral();
+ 8 other calls in file
GitHub: ruleenginejs/ruleengine
118 119 120 121 122 123 124 125 126 127
case 'null': return t.nullLiteral(); case 'boolean': return t.booleanLiteral(value); case 'number': return t.numericLiteral(value); case 'string': if (isSubstitutions) { if (!scope) { throw new Error('if isSubstitutions true scope is required');
+ 57 other calls in file
42 43 44 45 46 47 48 49 50 51
case tokens.DECIMAL_INTEGER: case tokens.OCT_INTEGER: case tokens.HEX_INTEGER: case tokens.BIN_INTEGER: case tokens.FLOAT_NUMBER: return t.numericLiteral(ctx.getText); case tokens.STRING_LITERAL: return t.stringLiteral(ctx.getText); default: return t.identifier(ctx.getText());
1494 1495 1496 1497 1498 1499 1500 1501 1502
dependency: InternalDependency, state: State, ) { return t.memberExpression( nullthrows(state.dependencyMapIdentifier), t.numericLiteral(dependency.index), true, ); }
+ 166 other calls in file
707 708 709 710 711 712 713 714 715 716
dependency: InternalDependency, state: State, ): BabelNodeExpression { return types.memberExpression( nullthrows(state.dependencyMapIdentifier), types.numericLiteral(dependency.index), true, ); }
+ 323 other calls in file
@babel/types.identifier is the most popular function in @babel/types (20936 examples)