|
| 1 | +/** |
| 2 | + * @file Prefer AbortSignal.abort() for already-aborted signals. |
| 3 | + */ |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +constmessage='Use AbortSignal.abort() instead of creating and aborting an AbortController.'; |
| 7 | + |
| 8 | +functionisAbortControllerConstruction(node){ |
| 9 | +returnnode?.type==='NewExpression'&& |
| 10 | +node.callee.type==='Identifier'&& |
| 11 | +node.callee.name==='AbortController'&& |
| 12 | +node.arguments.length===0; |
| 13 | +} |
| 14 | + |
| 15 | +functionisIdentifier(node,name){ |
| 16 | +returnnode?.type==='Identifier'&&node.name===name; |
| 17 | +} |
| 18 | + |
| 19 | +functionisProperty(node,name){ |
| 20 | +return!node.computed&&isIdentifier(node.property,name); |
| 21 | +} |
| 22 | + |
| 23 | +functionisAbortCallStatement(node,name){ |
| 24 | +constexpression=node?.expression; |
| 25 | +constcallee=expression?.callee; |
| 26 | +returnnode?.type==='ExpressionStatement'&& |
| 27 | +expression.type==='CallExpression'&& |
| 28 | +callee.type==='MemberExpression'&& |
| 29 | +isIdentifier(callee.object,name)&& |
| 30 | +isProperty(callee,'abort')&& |
| 31 | +expression.arguments.length<=1; |
| 32 | +} |
| 33 | + |
| 34 | +functionisSignalReference(reference,name){ |
| 35 | +const{ identifier }=reference; |
| 36 | +constparent=identifier.parent; |
| 37 | +returnisIdentifier(identifier,name)&& |
| 38 | +parent?.type==='MemberExpression'&& |
| 39 | +parent.object===identifier&& |
| 40 | +isProperty(parent,'signal'); |
| 41 | +} |
| 42 | + |
| 43 | +functionisAbortReference(reference,abortStatement,name){ |
| 44 | +const{ identifier }=reference; |
| 45 | +constparent=identifier.parent; |
| 46 | +returnisIdentifier(identifier,name)&& |
| 47 | +parent?.type==='MemberExpression'&& |
| 48 | +parent.object===identifier&& |
| 49 | +isProperty(parent,'abort')&& |
| 50 | +parent.parent===abortStatement.expression; |
| 51 | +} |
| 52 | + |
| 53 | +module.exports={ |
| 54 | +meta: { |
| 55 | +fixable: 'code', |
| 56 | +}, |
| 57 | + |
| 58 | +create(context){ |
| 59 | +constsourceCode=context.sourceCode; |
| 60 | +constcandidates=[]; |
| 61 | + |
| 62 | +functionhasCommentsBetween(left,right){ |
| 63 | +returnsourceCode.getCommentsBefore(right) |
| 64 | +.some((comment)=>comment.range[0]>left.range[1]); |
| 65 | +} |
| 66 | + |
| 67 | +functionrangeIncludingTrailingLine(statement){ |
| 68 | +consttokenAfter=sourceCode.getTokenAfter(statement,{includeComments: true}); |
| 69 | +if(tokenAfter&&tokenAfter.loc.start.line>statement.loc.end.line){ |
| 70 | +return[statement.range[0],tokenAfter.range[0]]; |
| 71 | +} |
| 72 | +returnstatement.range; |
| 73 | +} |
| 74 | + |
| 75 | +return{ |
| 76 | +VariableDeclarator(node){ |
| 77 | +if(node.id.type!=='Identifier'|| |
| 78 | +!isAbortControllerConstruction(node.init)|| |
| 79 | +node.parent.declarations.length!==1){ |
| 80 | +return; |
| 81 | +} |
| 82 | + |
| 83 | +constvariableDeclaration=node.parent; |
| 84 | +constparent=variableDeclaration.parent; |
| 85 | +if(parent.type!=='BlockStatement'&&parent.type!=='Program'){ |
| 86 | +return; |
| 87 | +} |
| 88 | + |
| 89 | +constindex=parent.body.indexOf(variableDeclaration); |
| 90 | +constabortStatement=parent.body[index+1]; |
| 91 | +if(!isAbortCallStatement(abortStatement,node.id.name)|| |
| 92 | +hasCommentsBetween(variableDeclaration,abortStatement)){ |
| 93 | +return; |
| 94 | +} |
| 95 | + |
| 96 | +candidates.push({ |
| 97 | + abortStatement, |
| 98 | +declarator: node, |
| 99 | + variableDeclaration, |
| 100 | +}); |
| 101 | +}, |
| 102 | + |
| 103 | +'Program:exit'(){ |
| 104 | +for(const{ abortStatement, declarator, variableDeclaration }ofcandidates){ |
| 105 | +const[variable]=sourceCode.scopeManager.getDeclaredVariables(declarator); |
| 106 | +if(!variable){ |
| 107 | +continue; |
| 108 | +} |
| 109 | + |
| 110 | +constname=declarator.id.name; |
| 111 | +constreferences=variable.references.filter((reference)=>{ |
| 112 | +returnreference.identifier!==declarator.id; |
| 113 | +}); |
| 114 | +constsignalReferences=references.filter((reference)=>{ |
| 115 | +returnisSignalReference(reference,name); |
| 116 | +}); |
| 117 | +constabortReferences=references.filter((reference)=>{ |
| 118 | +returnisAbortReference(reference,abortStatement,name); |
| 119 | +}); |
| 120 | + |
| 121 | +if(references.length!==(1+signalReferences.length)|| |
| 122 | +abortReferences.length!==1){ |
| 123 | +continue; |
| 124 | +} |
| 125 | + |
| 126 | +constsignalNode=signalReferences[0].identifier.parent; |
| 127 | +constabortArguments=abortStatement.expression.arguments; |
| 128 | +constabortReason=abortArguments.length===0 ? |
| 129 | +'' : sourceCode.getText(abortArguments[0]); |
| 130 | + |
| 131 | +context.report({ |
| 132 | +node: declarator, |
| 133 | + message, |
| 134 | +fix(fixer){ |
| 135 | +constabortSignalCreationCall=`AbortSignal.abort(${abortReason})`; |
| 136 | +if(signalReferences.length>1){ |
| 137 | +return[ |
| 138 | +fixer.replaceText(declarator.init,abortSignalCreationCall), |
| 139 | +fixer.removeRange(rangeIncludingTrailingLine(abortStatement)), |
| 140 | + ...signalReferences.map(({identifier: { parent }})=>fixer.replaceText(parent,name)), |
| 141 | +]; |
| 142 | +} |
| 143 | +return[ |
| 144 | +fixer.removeRange(rangeIncludingTrailingLine(variableDeclaration)), |
| 145 | +fixer.removeRange(rangeIncludingTrailingLine(abortStatement)), |
| 146 | +fixer.replaceText(signalNode,abortSignalCreationCall), |
| 147 | +]; |
| 148 | +}, |
| 149 | +}); |
| 150 | +} |
| 151 | +}, |
| 152 | +}; |
| 153 | +}, |
| 154 | +}; |
0 commit comments