|
| 1 | +import {findVariable} from '@eslint-community/eslint-utils'; |
| 2 | +import { |
| 3 | + isMethodCall, |
| 4 | + isMemberExpression, |
| 5 | +} from './ast/index.js'; |
| 6 | +import {} from './fix/index.js'; |
| 7 | +import {} from './utils/index.js'; |
| 8 | +import builtinErrors from './shared/builtin-errors.js'; |
| 9 | + |
| 10 | +const MESSAGE_ID_ERROR = 'no-useless-error-capture-stack-trace/error'; |
| 11 | +const messages = { |
| 12 | + [MESSAGE_ID_ERROR]: 'Unnecessary `Error.captureStackTrace(…)` call.', |
| 13 | +}; |
| 14 | + |
| 15 | +// TODO: Make sure the super class is global |
| 16 | +// https://github.com/eslint/eslint/pull/19695 |
| 17 | +const isSubclassOfBuiltinErrors = node => |
| 18 | + node?.superClass |
| 19 | + && node.superClass.type === 'Identifier' |
| 20 | + && builtinErrors.includes(node.superClass.name); |
| 21 | + |
| 22 | +const isClassReference = (node, classNode, context) => { |
| 23 | + // `new.target` |
| 24 | + if ( |
| 25 | + node.type === 'MetaProperty' |
| 26 | + && node.meta.type === 'Identifier' |
| 27 | + && node.meta.name === 'new' |
| 28 | + && node.property.type === 'Identifier' |
| 29 | + && node.property.name === 'target' |
| 30 | + ) { |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + // `this.constructor` |
| 35 | + if ( |
| 36 | + isMemberExpression(node, { |
| 37 | + property: 'constructor', |
| 38 | + computed: false, |
| 39 | + optional: false, |
| 40 | + }) |
| 41 | + && node.object.type === 'ThisExpression' |
| 42 | + ) { |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + if (node.type !== 'Identifier' || !classNode.id) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + const scope = context.sourceCode.getScope(node); |
| 51 | + const variable = findVariable(scope, node); |
| 52 | + |
| 53 | + return variable |
| 54 | + && variable.defs.length === 1 |
| 55 | + && variable.defs[0].type === 'ClassName' |
| 56 | + && variable.defs[0].node === classNode; |
| 57 | +}; |
| 58 | + |
| 59 | +const isClassConstructor = (node, classNode) => |
| 60 | + node |
| 61 | + && node.parent.type === 'MethodDefinition' |
| 62 | + && node.parent.kind === 'constructor' |
| 63 | + && node.parent.value === node |
| 64 | + && classNode.body.body.includes(node.parent); |
| 65 | + |
| 66 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 67 | +const create = context => { |
| 68 | + const classStack = []; |
| 69 | + const thisScopeStack = []; |
| 70 | + |
| 71 | + context.on(['ClassDeclaration', 'ClassExpression'], classNode => { |
| 72 | + classStack.push(classNode); |
| 73 | + thisScopeStack.push(classNode); |
| 74 | + }); |
| 75 | + |
| 76 | + context.onExit(['ClassDeclaration', 'ClassExpression'], () => { |
| 77 | + classStack.pop(); |
| 78 | + thisScopeStack.pop(); |
| 79 | + }); |
| 80 | + |
| 81 | + context.on(['FunctionDeclaration', 'FunctionExpression'], functionNode => { |
| 82 | + thisScopeStack.push(functionNode); |
| 83 | + }); |
| 84 | + |
| 85 | + context.onExit(['FunctionDeclaration', 'FunctionExpression'], () => { |
| 86 | + thisScopeStack.pop(); |
| 87 | + }); |
| 88 | + |
| 89 | + context.on('CallExpression', callExpression => { |
| 90 | + const errorClass = classStack.at(-1); |
| 91 | + |
| 92 | + if (!( |
| 93 | + isSubclassOfBuiltinErrors(errorClass) |
| 94 | + && isClassConstructor(thisScopeStack.at(-1), errorClass) |
| 95 | + && isMethodCall(callExpression, { |
| 96 | + object: 'Error', |
| 97 | + method: 'captureStackTrace', |
| 98 | + argumentsLength: 2, |
| 99 | + optionalMember: false, |
| 100 | + }) |
| 101 | + // TODO: Make sure the `object` is global |
| 102 | + )) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const [firstArgument, secondArgument] = callExpression.arguments; |
| 107 | + |
| 108 | + if ( |
| 109 | + firstArgument.type !== 'ThisExpression' |
| 110 | + || !isClassReference(secondArgument, errorClass, context) |
| 111 | + ) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + const problem = { |
| 116 | + node: callExpression, |
| 117 | + messageId: MESSAGE_ID_ERROR, |
| 118 | + }; |
| 119 | + |
| 120 | + const maybeExpressionStatement = callExpression.parent === 'ChainExpression' |
| 121 | + ? callExpression.parent.parent |
| 122 | + : callExpression.parent; |
| 123 | + |
| 124 | + if ( |
| 125 | + maybeExpressionStatement.type === 'ExpressionStatement' |
| 126 | + && maybeExpressionStatement.parent.type === 'BlockStatement' |
| 127 | + ) { |
| 128 | + problem.fix = fixer => fixer.remove(maybeExpressionStatement); |
| 129 | + } |
| 130 | + |
| 131 | + return problem; |
| 132 | + }); |
| 133 | +}; |
| 134 | + |
| 135 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 136 | +const config = { |
| 137 | + create, |
| 138 | + meta: { |
| 139 | + type: 'suggestion', |
| 140 | + docs: { |
| 141 | + description: 'Disallow unnecessary `Error.captureStackTrace(…)`.', |
| 142 | + recommended: true, |
| 143 | + }, |
| 144 | + fixable: 'code', |
| 145 | + messages, |
| 146 | + }, |
| 147 | +}; |
| 148 | + |
| 149 | +export default config; |
0 commit comments