How to use the isNumericLiteral function from @babel/types

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

89
90
91
92
93
94
95
96
97
98
      return;
    }

    // Handle the case we ignore above.
    if (path.node.operator === '-' &&
        babelTypes.isNumericLiteral(path.node.argument)) {
      thisMutator.randomReplace(path, -path.node.argument.value);
    }
  }
};
fork icon39
star icon94
watch icon0

285
286
287
288
289
290
291
292
293
294
} else {
  name = path.node.id.name
  initValue = path.node.init
}

if (t.isStringLiteral(initValue) || t.isNumericLiteral(initValue)) {
  let binding = path.scope.getBinding(name)
  if (binding && binding.constant && binding.constantViolations.length == 0) {
    for (let i = 0; i < binding.referencePaths.length; i++) {
      binding.referencePaths[i].replaceWith(initValue)
fork icon43
star icon73
watch icon5

+ 2 other calls in file

163
164
165
166
167
168
169
170
171
172
scope.traverse(scope.block, {
  CallExpression: (path_) => {
    const node_ = path_.node;
    if (
      node_.arguments.length === SHUFFLE_ARRAY_FUNCTION_ARGS_LENGTH &&
      t.isNumericLiteral(node_.arguments[1]) &&
      t.isIdentifier(node_.arguments[0]) &&
      node_.arguments[0].name === 'a'
    ) {
      shuffleIndex = node_.arguments[1].value; // + 1; // + 1?
fork icon5
star icon15
watch icon1

+ 31 other calls in file

142
143
144
145
146
147
148
149
150
151
) {
  // get every '![]'
  // replace with 'false'
  p.replaceWith(t.booleanLiteral(false));
} else if (
  t.isNumericLiteral(p.node.argument) &&
  p.node.argument.value == 1
) {
  // get every '!1'
  // replace with 'false'
fork icon0
star icon19
watch icon1

+ 3 other calls in file

122
123
124
125
126
127
128
129
130
131
if (t.isArrayPattern(defineVarId.node)) {
  prevState = defineVarId.node.elements[0];
  updateFn = defineVarId.node.elements[1];
  canMerge = t.isIdentifier(prevState);
} else if (t.isObjectPattern(defineVarId.node)) {
  const find = defineVarId.node.properties.find((n) => t.isNumericLiteral(n.key) && n.key.value === 1);
  if (find) {
    prevState = defineVarId.node.properties.find((n) => t.isNumericLiteral(n.key) && n.key.value === 0).value;
    canMerge = t.isIdentifier(prevState);
    updateFn = find.value;
fork icon13
star icon10
watch icon0

+ 7 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 icon2
star icon12
watch icon4

+ 158 other calls in file

35
36
37
38
39
40
41
42
43
44
// 获取Props默认值
function getDefaultVal(node) {
  if (
    types.isRegExpLiteral(node) ||
    types.isBooleanLiteral(node) ||
    types.isNumericLiteral(node) ||
    types.isStringLiteral(node)
  ) {
    return node.value
  } else if (
fork icon2
star icon8
watch icon2

+ 5 other calls in file

11
12
13
14
15
16
17
18
19
20
} = require('./strategy')

// a+1+'s'+2+b
function isVariableNumerLiteral (left, right) {
  return (t.isCallExpression(left) || t.isIdentifier(left)) &&
        t.isNumericLiteral(right)
}

function mergeResult (...targets) {
  let dest = {
fork icon0
star icon7
watch icon4

+ 15 other calls in file

336
337
338
339
340
341
342
343
344
345
traverse(ast, {
    enter(path) {
        if (path.node.type === 'SwitchCase' || path.node.type === 'WhileStatement') {
            return;
        }
        if (types.isBooleanLiteral(path.node.test) || types.isNumericLiteral(path.node.test)) {
            if (path.node.test.value) {
                path.replaceInline(path.node.consequent);
            } else {
                if (path.node.alternate) {
fork icon1
star icon0
watch icon1

+ 82 other calls in file

232
233
234
235
236
237
238
239
240
241
traverse(this._ast, {
  MemberExpression: (path) => {
    const { node } = path;
    if (t.isCallExpression(node.property) &&
      node.property.arguments.length === 1 &&
      t.isNumericLiteral(node.property.arguments[0])
    ) {
      let callNode = node.property;

      const scopeData = {
fork icon0
star icon6
watch icon1

+ 147 other calls in file

124
125
126
127
128
129
130
131
132
133
  }
  return t.stringLiteral('$' + (extraArrayElements.length - 1))
} else if ( // +1=>1
  t.isUnaryExpression(element) &&
  element.operator === '+' &&
  t.isNumericLiteral(element.argument)
) {
  element = t.numericLiteral(element.argument.value)
} else if (t.isObjectExpression(element)) {
  // {name:'a',b:'c',d:123}=>[['name','a'],['b','c'],['d',123]]
fork icon0
star icon1
watch icon1

+ 3 other calls in file

22
23
24
25
26
27
28
29
30
31
  return false
}
const expr = parseExpression(str)
if (
  t.isStringLiteral(expr) ||
  t.isNumericLiteral(expr) ||
  t.isBooleanLiteral(expr) ||
  t.isNullLiteral(expr)
) {
  return false
fork icon0
star icon2
watch icon0

+ 27 other calls in file

8
9
10
11
12
13
14
15
16
17
/**
 * Choose parse method
 * @param {JSXElement|String|Number} el
 */
function parseElement(el) {
  if (t.isStringLiteral(el) || t.isNumericLiteral(el)) {
    return parseBaseStructure(el);
  } else if (t.isNullLiteral(el) || t.isBooleanLiteral(el) || t.isIdentifier(el, { name: 'undefined' } )) {
    return null;
  } else if (t.isArrayExpression(el)) {
fork icon663
star icon0
watch icon2

+ 3 other calls in file

456
457
458
459
460
461
462
463
464
465
 */
function unaryExpressionHandle (ast) {
    traverse(ast, {
        UnaryExpression (path) {
            const { operator, argument } = path.node
            if (operator === "!" && t.isNumericLiteral(argument)) {
                var value = argument.value
                if (value === 1 || value === 0) {
                    path.replaceWith(t.booleanLiteral(value === 0))
                }
fork icon220
star icon0
watch icon0

204
205
206
207
208
209
210
211
212
213
const find = defineVarId.node.properties.find((n) => (
  t.isNumericLiteral(n.key) && n.key.value === 1
));
if (find) {
  prevState = defineVarId.node.properties.find((n) => (
    t.isNumericLiteral(n.key) && n.key.value === 0
  )).value;
  canMerge = t.isIdentifier(prevState);
  updateFn = find.value;
}
fork icon13
star icon0
watch icon0

+ 3 other calls in file

106
107
108
109
110
111
112
113
114
115
}
/**
 * 对象声明的属性不能是数字
 * a = {0: 0} 是非法的!
 */
if (path.isObjectProperty() && babelTypes.isNumericLiteral(path.node.key)) {
  let message = `${filePath}:${path.node.key.loc.start.line + startLine - 1}:${path.node.key.loc.start.column}: Unexpected token \`:\`\n`;
  wccError = (new error.WccError(-1, message));
  path.stop();
  return;
fork icon10
star icon0
watch icon0

+ 5 other calls in file

49
50
51
52
53
54
55
56
57
58
visitor2 = {
    BinaryExpression(path)
    {
        // console.log(path.node.left);
        let {left, operator, right} = path.node;
        if (types.isNumericLiteral(left) && operator == '+' && types.isNumericLiteral(right)){
            value = left.value + right.value;
            // 替换结点的值
            path.replaceWith(types.valueToNode(value))
        }
fork icon0
star icon0
watch icon1

+ 276 other calls in file

10
11
12
13
14
15
16
17
18
19
function getCallExpressionZIndexValue(node) {
  for (let i = 1; i < node.arguments.length; i++) {
    const argument = node.arguments[i];
    const previous = node.arguments[i - 1];
    if (
      (types.isStringLiteral(argument) || types.isNumericLiteral(argument)) &&
      argument.value !== '' &&
      types.isStringLiteral(previous) &&
      zIndexRegExp.test(previous.value)
    ) {
fork icon0
star icon0
watch icon2

284
285
286
287
288
289
290
291
292
293
 * e['sdf' + 2] => 
 * e[((nt_1 = ('sdf' + 2), null == nt_1 ? undefined : 'number' === typeof nt_1 ? nt_1 : "nv_" + nt_1))]
 */
if (path.isMemberExpression() && path.node.computed) {
  let oldProperty = path.node.property;
  if (!(babelTypes.isStringLiteral(oldProperty) || babelTypes.isNumericLiteral(oldProperty))) {
    let tmpVar = 'nt_1';
    let newProperty = babelTypes.sequenceExpression([
      babelTypes.assignmentExpression(
        '=',
fork icon0
star icon0
watch icon1

+ 21 other calls in file

112
113
114
115
116
117
118
119
120
121
function check_identifier(node){
    if (t.isBinaryExpression(node)){
        let left = check_identifier(node.left);
        let right = check_identifier(node.right);
        return left || right;
    } else if (t.isStringLiteral(node) || t.isNumericLiteral(node) || t.isUnaryExpression(node)){
        return false;
    }
    return true;
}
fork icon0
star icon0
watch icon0

+ 4 other calls in file

Other functions in @babel/types

Sorted by popularity

function icon

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