|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const tcIdentifiers = new Set([ |
| 4 | + 'isArguments', |
| 5 | + 'isArray', |
| 6 | + 'isArrayBuffer', |
| 7 | + 'isArrayLike', |
| 8 | + 'isArrayLikeObject', |
| 9 | + 'isBoolean', |
| 10 | + 'isBuffer', |
| 11 | + 'isDate', |
| 12 | + 'isElement', |
| 13 | + 'isError', |
| 14 | + 'isFinite', |
| 15 | + 'isFunction', |
| 16 | + 'isInteger', |
| 17 | + 'isLength', |
| 18 | + 'isMap', |
| 19 | + 'isNaN', |
| 20 | + 'isNative', |
| 21 | + 'isNil', |
| 22 | + 'isNull', |
| 23 | + 'isNumber', |
| 24 | + 'isObject', |
| 25 | + 'isObjectLike', |
| 26 | + 'isPlainObject', |
| 27 | + 'isPrototypeOf', |
| 28 | + 'isRegExp', |
| 29 | + 'isSafeInteger', |
| 30 | + 'isSet', |
| 31 | + 'isString', |
| 32 | + 'isSymbol', |
| 33 | + 'isTypedArray', |
| 34 | + 'isUndefined', |
| 35 | + 'isView', |
| 36 | + 'isWeakMap', |
| 37 | + 'isWeakSet', |
| 38 | + 'isWindow', |
| 39 | + 'isXMLDoc' |
| 40 | +]); |
| 41 | + |
| 42 | +const tcGlobalIdentifiers = new Set([ |
| 43 | + 'isNaN', |
| 44 | + 'isFinite' |
| 45 | +]); |
| 46 | + |
| 47 | +const isTypecheckingIdentifier = (node, callExpression, isMemberExpression) => |
| 48 | + callExpression !== undefined && |
| 49 | + callExpression.arguments.length > 0 && |
| 50 | + node.type === 'Identifier' && |
| 51 | + ((isMemberExpression === true && |
| 52 | + tcIdentifiers.has(node.name)) || |
| 53 | + (isMemberExpression === false && |
| 54 | + tcGlobalIdentifiers.has(node.name))); |
| 55 | + |
| 56 | +const throwsErrorObject = node => |
| 57 | + node.argument.type === 'NewExpression' && |
| 58 | + node.argument.callee.type === 'Identifier' && |
| 59 | + node.argument.callee.name === 'Error'; |
| 60 | + |
| 61 | +const isLone = node => |
| 62 | + node.parent !== null && |
| 63 | + node.parent.body !== null && |
| 64 | + node.parent.body.length === 1; |
| 65 | + |
| 66 | +const isTypecheckingMemberExpression = (node, callExpression) => { |
| 67 | + if (isTypecheckingIdentifier(node.property, callExpression, true)) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + if (node.object.type === 'MemberExpression') { |
| 72 | + return isTypecheckingMemberExpression(node.object, callExpression); |
| 73 | + } |
| 74 | + |
| 75 | + return false; |
| 76 | +}; |
| 77 | + |
| 78 | +const isTypecheckingExpression = (node, callExpression) => { |
| 79 | + switch (node.type) { |
| 80 | + case 'Identifier': |
| 81 | + return isTypecheckingIdentifier(node, callExpression, false); |
| 82 | + case 'MemberExpression': |
| 83 | + return isTypecheckingMemberExpression(node, callExpression); |
| 84 | + case 'CallExpression': |
| 85 | + return isTypecheckingExpression(node.callee, node); |
| 86 | + case 'UnaryExpression': |
| 87 | + return node.operator === 'typeof' || |
| 88 | + (node.operator === '!' && |
| 89 | + isTypecheckingExpression(node.argument)); |
| 90 | + case 'BinaryExpression': |
| 91 | + return node.operator === 'instanceof' || |
| 92 | + isTypecheckingExpression(node.left, callExpression) || |
| 93 | + isTypecheckingExpression(node.right, callExpression); |
| 94 | + case 'LogicalExpression': |
| 95 | + return isTypecheckingExpression(node.left, callExpression) && |
| 96 | + isTypecheckingExpression(node.right, callExpression); |
| 97 | + default: |
| 98 | + return false; |
| 99 | + } |
| 100 | +}; |
| 101 | + |
| 102 | +const isTypechecking = node => node.type === 'IfStatement' && isTypecheckingExpression(node.test); |
| 103 | + |
| 104 | +const create = context => { |
| 105 | + return { |
| 106 | + ThrowStatement: node => { |
| 107 | + if (throwsErrorObject(node) && |
| 108 | + isLone(node) && |
| 109 | + node.parent.parent !== null && |
| 110 | + isTypechecking(node.parent.parent)) { |
| 111 | + context.report({ |
| 112 | + node, |
| 113 | + message: '`new Error()` is too unspecific for a type check. Use `new TypeError()` instead.', |
| 114 | + fix: fixer => fixer.replaceText(node.argument.callee, 'TypeError') |
| 115 | + }); |
| 116 | + } |
| 117 | + } |
| 118 | + }; |
| 119 | +}; |
| 120 | + |
| 121 | +module.exports = { |
| 122 | + create, |
| 123 | + meta: { |
| 124 | + fixable: 'code' |
| 125 | + } |
| 126 | +}; |
0 commit comments