|
| 1 | +'use strict'; |
| 2 | +const {getStaticValue} = require('eslint-utils'); |
| 3 | + |
| 4 | +const isStaticProperties = (node, object, properties) => |
| 5 | + node.type === 'MemberExpression' |
| 6 | + && !node.computed |
| 7 | + && !node.optional |
| 8 | + && node.object.type === 'Identifier' |
| 9 | + && node.object.name === object |
| 10 | + && node.property.type === 'Identifier' |
| 11 | + && properties.has(node.property.name); |
| 12 | +const isFunctionCall = (node, functionName) => node.type === 'CallExpression' |
| 13 | + && !node.optional |
| 14 | + && node.callee.type === 'Identifier' |
| 15 | + && node.callee.name === functionName; |
| 16 | + |
| 17 | +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math#static_properties |
| 18 | +const mathProperties = new Set([ |
| 19 | + 'E', |
| 20 | + 'LN2', |
| 21 | + 'LN10', |
| 22 | + 'LOG2E', |
| 23 | + 'LOG10E', |
| 24 | + 'PI', |
| 25 | + 'SQRT1_2', |
| 26 | + 'SQRT2', |
| 27 | +]); |
| 28 | + |
| 29 | +// `Math.{E,LN2,LN10,LOG2E,LOG10E,PI,SQRT1_2,SQRT2}` |
| 30 | +const isMathProperty = node => isStaticProperties(node, 'Math', mathProperties); |
| 31 | + |
| 32 | +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math#static_methods |
| 33 | +const mathMethods = new Set([ |
| 34 | + 'abs', |
| 35 | + 'acos', |
| 36 | + 'acosh', |
| 37 | + 'asin', |
| 38 | + 'asinh', |
| 39 | + 'atan', |
| 40 | + 'atanh', |
| 41 | + 'atan2', |
| 42 | + 'cbrt', |
| 43 | + 'ceil', |
| 44 | + 'clz32', |
| 45 | + 'cos', |
| 46 | + 'cosh', |
| 47 | + 'exp', |
| 48 | + 'expm1', |
| 49 | + 'floor', |
| 50 | + 'fround', |
| 51 | + 'hypot', |
| 52 | + 'imul', |
| 53 | + 'log', |
| 54 | + 'log1p', |
| 55 | + 'log10', |
| 56 | + 'log2', |
| 57 | + 'max', |
| 58 | + 'min', |
| 59 | + 'pow', |
| 60 | + 'random', |
| 61 | + 'round', |
| 62 | + 'sign', |
| 63 | + 'sin', |
| 64 | + 'sinh', |
| 65 | + 'sqrt', |
| 66 | + 'tan', |
| 67 | + 'tanh', |
| 68 | + 'trunc', |
| 69 | +]); |
| 70 | +// `Math.{abs, …, trunc}(…)` |
| 71 | +const isMathMethodCall = node => |
| 72 | + node.type === 'CallExpression' |
| 73 | + && !node.optional |
| 74 | + && isStaticProperties(node.callee, 'Math', mathMethods); |
| 75 | + |
| 76 | +// `Number(…)` |
| 77 | +const isNumberCall = node => isFunctionCall(node, 'Number'); |
| 78 | + |
| 79 | +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#static_properties |
| 80 | +const numberProperties = new Set([ |
| 81 | + 'EPSILON', |
| 82 | + 'MAX_SAFE_INTEGER', |
| 83 | + 'MAX_VALUE', |
| 84 | + 'MIN_SAFE_INTEGER', |
| 85 | + 'MIN_VALUE', |
| 86 | + 'NaN', |
| 87 | + 'NEGATIVE_INFINITY', |
| 88 | + 'POSITIVE_INFINITY', |
| 89 | +]); |
| 90 | +const isNumberProperty = node => isStaticProperties(node, 'Number', numberProperties); |
| 91 | + |
| 92 | +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#static_methods |
| 93 | +const numberMethods = new Set([ |
| 94 | + 'parseFloat', |
| 95 | + 'parseInt', |
| 96 | +]); |
| 97 | +const isNumberMethodCall = node => |
| 98 | + node.type === 'CallExpression' |
| 99 | + && !node.optional |
| 100 | + && isStaticProperties(node.callee, 'Number', numberMethods); |
| 101 | +const isGlobalParseToNumberFunctionCall = node => isFunctionCall(node, 'parseInt') || isFunctionCall(node, 'parseFloat'); |
| 102 | + |
| 103 | +// `+x`, `-x` |
| 104 | +const numberUnaryOperators = new Set(['-', '+', '~']); |
| 105 | +const isNumberUnaryExpression = node => |
| 106 | + node.type === 'UnaryExpression' |
| 107 | + && node.prefix |
| 108 | + && numberUnaryOperators.has(node.operator); |
| 109 | + |
| 110 | +const isStaticNumber = (node, scope) => { |
| 111 | + const staticResult = getStaticValue(node, scope); |
| 112 | + return staticResult !== null && typeof staticResult.value === 'number'; |
| 113 | +}; |
| 114 | + |
| 115 | +const isNumberLiteral = node => node.type === 'Literal' && typeof node.value === 'number'; |
| 116 | +const isLengthProperty = node => |
| 117 | + node.type === 'MemberExpression' |
| 118 | + && !node.computed |
| 119 | + && !node.optional |
| 120 | + && node.property.type === 'Identifier' |
| 121 | + && node.property.name === 'length'; |
| 122 | + |
| 123 | +const mathOperators = new Set(['-', '*', '/', '%', '**', '<<', '>>', '>>>', '|', '^', '&']); |
| 124 | +function isNumber(node, scope) { |
| 125 | + if ( |
| 126 | + isNumberLiteral(node) |
| 127 | + || isMathProperty(node) |
| 128 | + || isMathMethodCall(node) |
| 129 | + || isNumberCall(node) |
| 130 | + || isNumberProperty(node) |
| 131 | + || isNumberMethodCall(node) |
| 132 | + || isGlobalParseToNumberFunctionCall(node) |
| 133 | + || isNumberUnaryExpression(node) |
| 134 | + || isLengthProperty(node) |
| 135 | + ) { |
| 136 | + return true; |
| 137 | + } |
| 138 | + |
| 139 | + switch (node.type) { |
| 140 | + case 'BinaryExpression': |
| 141 | + case 'AssignmentExpression': { |
| 142 | + let {operator} = node; |
| 143 | + |
| 144 | + if (node.type === 'AssignmentExpression') { |
| 145 | + operator = operator.slice(0, -1); |
| 146 | + } |
| 147 | + |
| 148 | + if (operator === '+') { |
| 149 | + return isNumber(node.left, scope) && isNumber(node.right, scope); |
| 150 | + } |
| 151 | + |
| 152 | + // `a + b` can be `BigInt`, we need make sure at least one side is number |
| 153 | + if (mathOperators.has(operator)) { |
| 154 | + return isNumber(node.left, scope) || isNumber(node.right, scope); |
| 155 | + } |
| 156 | + |
| 157 | + break; |
| 158 | + } |
| 159 | + |
| 160 | + case 'ConditionalExpression': |
| 161 | + return isNumber(node.consequent, scope) && isNumber(node.alternate, scope); |
| 162 | + case 'SequenceExpression': |
| 163 | + return isNumber(node.expressions[node.expressions.length - 1], scope); |
| 164 | + // No default |
| 165 | + } |
| 166 | + |
| 167 | + return isStaticNumber(node, scope); |
| 168 | +} |
| 169 | + |
| 170 | +module.exports = isNumber; |
0 commit comments