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
New: Add no-promise-executor-return rule (fixes #12640) #12648
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Disallow returning values from Promise executor functions (no-promise-executor-return) | ||
|
||
The `new Promise` constructor accepts a single argument, called an *executor*. | ||
|
||
```js | ||
const myPromise = new Promise(function executor(resolve, reject) { | ||
readFile('foo.txt', function(err, result) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(result); | ||
} | ||
}); | ||
}); | ||
``` | ||
|
||
The executor function usually initiates some asynchronous operation. Once it is finished, the executor should call `resolve` with the result, or `reject` if an error occurred. | ||
|
||
The return value of the executor is ignored. Returning a value from an executor function is a possible error because the returned value cannot be used and it doesn't affect the promise in any way. | ||
|
||
## Rule Details | ||
|
||
This rule disallows returning values from Promise executor functions. | ||
|
||
Only `return` without a value is allowed, as it's a control flow statement. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/*eslint no-promise-executor-return: "error"*/ | ||
|
||
new Promise((resolve, reject) => { | ||
if (someCondition) { | ||
return defaultResult; | ||
} | ||
getSomething((err, result) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(result); | ||
} | ||
}); | ||
}); | ||
|
||
new Promise((resolve, reject) => getSomething((err, data) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(data); | ||
} | ||
})); | ||
|
||
new Promise(() => { | ||
return 1; | ||
}); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/*eslint no-promise-executor-return: "error"*/ | ||
|
||
new Promise((resolve, reject) => { | ||
if (someCondition) { | ||
resolve(defaultResult); | ||
return; | ||
} | ||
getSomething((err, result) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(result); | ||
} | ||
}); | ||
}); | ||
|
||
new Promise((resolve, reject) => { | ||
getSomething((err, data) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(data); | ||
} | ||
}); | ||
}); | ||
|
||
Promise.resolve(1); | ||
``` | ||
|
||
## Further Reading | ||
|
||
* [MDN Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) | ||
|
||
## Related Rules | ||
|
||
* [no-async-promise-executor](no-async-promise-executor.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/** | ||
* @fileoverview Rule to disallow returning values from Promise executor functions | ||
* @author Milos Djermanovic | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const { findVariable } = require("eslint-utils"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
|
||
const functionTypesToCheck = new Set(["ArrowFunctionExpression", "FunctionExpression"]); | ||
|
||
/** | ||
* Determines whether the given identifier node is a reference to a global variable. | ||
* @param {ASTNode} node `Identifier` node to check. | ||
* @param {Scope} scope Scope to which the node belongs. | ||
* @returns {boolean} True if the identifier is a reference to a global variable. | ||
*/ | ||
function isGlobalReference(node, scope) { | ||
const variable = findVariable(scope, node); | ||
|
||
return variable !== null && variable.scope.type === "global" && variable.defs.length === 0; | ||
} | ||
|
||
/** | ||
* Finds function's outer scope. | ||
* @param {Scope} scope Function's own scope. | ||
* @returns {Scope} Function's outer scope. | ||
*/ | ||
function getOuterScope(scope) { | ||
const upper = scope.upper; | ||
|
||
if (upper.type === "function-expression-name") { | ||
return upper.upper; | ||
} | ||
return upper; | ||
} | ||
|
||
/** | ||
* Determines whether the given function node is used as a Promise executor. | ||
* @param {ASTNode} node The node to check. | ||
* @param {Scope} scope Function's own scope. | ||
* @returns {boolean} `true` if the node is a Promise executor. | ||
*/ | ||
function isPromiseExecutor(node, scope) { | ||
const parent = node.parent; | ||
|
||
return parent.type === "NewExpression" && | ||
parent.arguments[0] === node && | ||
parent.callee.type === "Identifier" && | ||
parent.callee.name === "Promise" && | ||
isGlobalReference(parent.callee, getOuterScope(scope)); | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: "problem", | ||
|
||
docs: { | ||
description: "disallow returning values from Promise executor functions", | ||
category: "Possible Errors", | ||
recommended: false, | ||
url: "https://eslint.org/docs/rules/no-promise-executor-return" | ||
}, | ||
|
||
schema: [], | ||
|
||
messages: { | ||
returnsValue: "Promise executor functions cannot return values." | ||
} | ||
}, | ||
|
||
create(context) { | ||
|
||
let funcInfo = null; | ||
|
||
/** | ||
* Reports the given node. | ||
* @param {ASTNode} node Node to report. | ||
* @returns {void} | ||
*/ | ||
function report(node) { | ||
context.report({ node, messageId: "returnsValue" }); | ||
} | ||
|
||
return { | ||
|
||
onCodePathStart(_, node) { | ||
funcInfo = { | ||
upper: funcInfo, | ||
shouldCheck: functionTypesToCheck.has(node.type) && isPromiseExecutor(node, context.getScope()) | ||
}; | ||
|
||
if (funcInfo.shouldCheck && node.type === "ArrowFunctionExpression" && node.expression) { | ||
report(node.body); | ||
} | ||
}, | ||
|
||
onCodePathEnd() { | ||
funcInfo = funcInfo.upper; | ||
}, | ||
|
||
ReturnStatement(node) { | ||
if (funcInfo.shouldCheck && node.argument) { | ||
report(node); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this accurate? I think they can return values but the values are never exposed anywhere. Maybe, "Return values from promise executor functions cannot be read."?
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.
Agreed, this message might be seen as technically incorrect. It's changed now, thanks!