-
-
Notifications
You must be signed in to change notification settings - Fork 35.6k
tools: add lint rule for aborted AbortController #63541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,91 @@ | ||||||||||||||||||||||||||
| 'use strict'; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const common = require('../common'); | ||||||||||||||||||||||||||
| if ((!common.hasCrypto) || (!common.hasIntl)) { | ||||||||||||||||||||||||||
| common.skip('ESLint tests require crypto and Intl'); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| common.skipIfEslintMissing(); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const RuleTester = require('../../tools/eslint/node_modules/eslint').RuleTester; | ||||||||||||||||||||||||||
| const rule = require('../../tools/eslint-rules/prefer-abort-signal-abort'); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const message = 'Use AbortSignal.abort() instead of creating and aborting an AbortController.'; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| new RuleTester().run('prefer-abort-signal-abort', rule, { | ||||||||||||||||||||||||||
| valid: [ | ||||||||||||||||||||||||||
| 'const signal = AbortSignal.abort();', | ||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||
| const controller = new AbortController(); | ||||||||||||||||||||||||||
| controller.abort(); | ||||||||||||||||||||||||||
| controller.abort(); | ||||||||||||||||||||||||||
| fn(controller.signal); | ||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||
|
Comment on lines
+18
to
+23
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For readability sake, let's indent
Suggested change
|
||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||
| const controller = new AbortController(); | ||||||||||||||||||||||||||
| controller.abort(); | ||||||||||||||||||||||||||
| fn(controller.signal, controller.signal); | ||||||||||||||||||||||||||
|
Comment on lines
+25
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto, and why is this a valid case? |
||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||
| const controller = new AbortController(); | ||||||||||||||||||||||||||
| controller.abort(); | ||||||||||||||||||||||||||
| console.log(controller); | ||||||||||||||||||||||||||
| fn(controller.signal); | ||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||
| const controller = new AbortController(); | ||||||||||||||||||||||||||
| // This comment should not be removed. | ||||||||||||||||||||||||||
| controller.abort(); | ||||||||||||||||||||||||||
| fn(controller.signal); | ||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||
| const controller = new AbortController(); | ||||||||||||||||||||||||||
| setImmediate(() => controller.abort()); | ||||||||||||||||||||||||||
| fn(controller.signal); | ||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||
| const controller = new AbortController(); | ||||||||||||||||||||||||||
| controller.abort('reason', 'extra'); | ||||||||||||||||||||||||||
| fn(controller.signal); | ||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||
| invalid: [ | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| code: ` | ||||||||||||||||||||||||||
| const controller = new AbortController(); | ||||||||||||||||||||||||||
| controller.abort(); | ||||||||||||||||||||||||||
| fn(controller.signal); | ||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||
| errors: [{ message }], | ||||||||||||||||||||||||||
| output: ` | ||||||||||||||||||||||||||
| fn(AbortSignal.abort()); | ||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| code: ` | ||||||||||||||||||||||||||
| const abortController = new AbortController(); | ||||||||||||||||||||||||||
| abortController.abort(new Error('aborted')); | ||||||||||||||||||||||||||
| fn({ signal: abortController.signal }); | ||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||
| errors: [{ message }], | ||||||||||||||||||||||||||
| output: ` | ||||||||||||||||||||||||||
| fn({ signal: AbortSignal.abort(new Error('aborted')) }); | ||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| code: ` | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| const ac = new AbortController(); | ||||||||||||||||||||||||||
| ac.abort(); | ||||||||||||||||||||||||||
| await wait({ signal: ac.signal }); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||
| errors: [{ message }], | ||||||||||||||||||||||||||
| output: ` | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| await wait({ signal: AbortSignal.abort() }); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,147 @@ | ||||||||
| /** | ||||||||
| * @file Prefer AbortSignal.abort() for already-aborted signals. | ||||||||
| */ | ||||||||
| 'use strict'; | ||||||||
|
|
||||||||
| const message = 'Use AbortSignal.abort() instead of creating and aborting an AbortController.'; | ||||||||
|
|
||||||||
| function isAbortControllerConstruction(node) { | ||||||||
| return node?.type === 'NewExpression' && | ||||||||
| node.callee.type === 'Identifier' && | ||||||||
| node.callee.name === 'AbortController' && | ||||||||
| node.arguments.length === 0; | ||||||||
| } | ||||||||
|
|
||||||||
| function isIdentifier(node, name) { | ||||||||
| return node?.type === 'Identifier' && node.name === name; | ||||||||
| } | ||||||||
|
|
||||||||
| function isProperty(node, name) { | ||||||||
| return !node.computed && isIdentifier(node.property, name); | ||||||||
| } | ||||||||
|
|
||||||||
| function isAbortCallStatement(node, name) { | ||||||||
| const expression = node?.expression; | ||||||||
| const callee = expression?.callee; | ||||||||
| return node?.type === 'ExpressionStatement' && | ||||||||
| expression.type === 'CallExpression' && | ||||||||
| callee.type === 'MemberExpression' && | ||||||||
| isIdentifier(callee.object, name) && | ||||||||
| isProperty(callee, 'abort') && | ||||||||
| expression.arguments.length <= 1; | ||||||||
| } | ||||||||
|
|
||||||||
| function isSignalReference(reference, name) { | ||||||||
| const { identifier } = reference; | ||||||||
| const parent = identifier.parent; | ||||||||
| return isIdentifier(identifier, name) && | ||||||||
| parent?.type === 'MemberExpression' && | ||||||||
| parent.object === identifier && | ||||||||
| isProperty(parent, 'signal'); | ||||||||
| } | ||||||||
|
|
||||||||
| function isAbortReference(reference, abortStatement, name) { | ||||||||
| const { identifier } = reference; | ||||||||
| const parent = identifier.parent; | ||||||||
| return isIdentifier(identifier, name) && | ||||||||
| parent?.type === 'MemberExpression' && | ||||||||
| parent.object === identifier && | ||||||||
| isProperty(parent, 'abort') && | ||||||||
| parent.parent === abortStatement.expression; | ||||||||
| } | ||||||||
|
|
||||||||
| module.exports = { | ||||||||
| meta: { | ||||||||
| fixable: 'code', | ||||||||
| }, | ||||||||
|
|
||||||||
| create(context) { | ||||||||
| const sourceCode = context.sourceCode; | ||||||||
| const candidates = []; | ||||||||
|
|
||||||||
| function hasCommentsBetween(left, right) { | ||||||||
| return sourceCode.getCommentsBefore(right) | ||||||||
| .some((comment) => comment.range[0] > left.range[1]); | ||||||||
| } | ||||||||
|
|
||||||||
| function rangeIncludingTrailingLine(statement) { | ||||||||
| const tokenAfter = sourceCode.getTokenAfter(statement, { includeComments: true }); | ||||||||
| if (tokenAfter && tokenAfter.loc.start.line > statement.loc.end.line) { | ||||||||
| return [statement.range[0], tokenAfter.range[0]]; | ||||||||
| } | ||||||||
| return statement.range; | ||||||||
| } | ||||||||
|
|
||||||||
| return { | ||||||||
| VariableDeclarator(node) { | ||||||||
| if (node.id.type !== 'Identifier' || | ||||||||
| !isAbortControllerConstruction(node.init) || | ||||||||
| node.parent.declarations.length !== 1) { | ||||||||
| return; | ||||||||
| } | ||||||||
|
|
||||||||
| const variableDeclaration = node.parent; | ||||||||
| const parent = variableDeclaration.parent; | ||||||||
| if (parent.type !== 'BlockStatement' && parent.type !== 'Program') { | ||||||||
| return; | ||||||||
| } | ||||||||
|
|
||||||||
| const index = parent.body.indexOf(variableDeclaration); | ||||||||
| const abortStatement = parent.body[index + 1]; | ||||||||
| if (!isAbortCallStatement(abortStatement, node.id.name) || | ||||||||
| hasCommentsBetween(variableDeclaration, abortStatement)) { | ||||||||
| return; | ||||||||
| } | ||||||||
|
|
||||||||
| candidates.push({ | ||||||||
| abortStatement, | ||||||||
| declarator: node, | ||||||||
| variableDeclaration, | ||||||||
| }); | ||||||||
| }, | ||||||||
|
|
||||||||
| 'Program:exit'() { | ||||||||
| for (const { abortStatement, declarator, variableDeclaration } of candidates) { | ||||||||
| const [variable] = sourceCode.scopeManager.getDeclaredVariables(declarator); | ||||||||
| if (!variable) { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| const name = declarator.id.name; | ||||||||
| const references = variable.references.filter((reference) => { | ||||||||
| return reference.identifier !== declarator.id; | ||||||||
| }); | ||||||||
| const signalReferences = references.filter((reference) => { | ||||||||
| return isSignalReference(reference, name); | ||||||||
| }); | ||||||||
| const abortReferences = references.filter((reference) => { | ||||||||
| return isAbortReference(reference, abortStatement, name); | ||||||||
| }); | ||||||||
|
|
||||||||
| if (references.length !== 2 || | ||||||||
| signalReferences.length !== 1 || | ||||||||
|
Comment on lines
+121
to
+122
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| abortReferences.length !== 1) { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| const signalNode = signalReferences[0].identifier.parent; | ||||||||
| const abortArguments = abortStatement.expression.arguments; | ||||||||
| const abortReason = abortArguments.length === 0 ? | ||||||||
| '' : sourceCode.getText(abortArguments[0]); | ||||||||
|
|
||||||||
| context.report({ | ||||||||
| node: signalNode, | ||||||||
| message, | ||||||||
| fix(fixer) { | ||||||||
| return [ | ||||||||
| fixer.removeRange(rangeIncludingTrailingLine(variableDeclaration)), | ||||||||
| fixer.removeRange(rangeIncludingTrailingLine(abortStatement)), | ||||||||
| fixer.replaceText(signalNode, `AbortSignal.abort(${abortReason})`), | ||||||||
| ]; | ||||||||
| }, | ||||||||
| }); | ||||||||
| } | ||||||||
| }, | ||||||||
| }; | ||||||||
| }, | ||||||||
| }; | ||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is testing
AbortController(see the file name), notAbortSignal; it should be reverted and excluded via a// eslint-disable-next-linecomment