summaryrefslogtreecommitdiff
path: root/tools/node_modules/eslint/lib/rules/no-return-await.js
diff options
context:
space:
mode:
authorMichaël Zasso <targos@protonmail.com>2017-12-22 16:53:42 +0100
committerMichaël Zasso <targos@protonmail.com>2018-01-11 09:48:05 +0100
commit3dc30632755713179f345f4af024bd904c6162d0 (patch)
treef28c4f6dd6dfc5992edf301449d1a371d229755b /tools/node_modules/eslint/lib/rules/no-return-await.js
parenta2c7085dd4a8e60d1a47572aca8bb6fcb7a32f88 (diff)
downloadandroid-node-v8-3dc30632755713179f345f4af024bd904c6162d0.tar.gz
android-node-v8-3dc30632755713179f345f4af024bd904c6162d0.tar.bz2
android-node-v8-3dc30632755713179f345f4af024bd904c6162d0.zip
tools: move eslint from tools to tools/node_modules
This is required because we need to add the babel-eslint dependency and it has to be able to resolve "eslint". babel-eslint is required to support future ES features such as async iterators and import.meta. Refs: https://github.com/nodejs/node/pull/17755 PR-URL: https://github.com/nodejs/node/pull/17820 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Diffstat (limited to 'tools/node_modules/eslint/lib/rules/no-return-await.js')
-rw-r--r--tools/node_modules/eslint/lib/rules/no-return-await.js94
1 files changed, 94 insertions, 0 deletions
diff --git a/tools/node_modules/eslint/lib/rules/no-return-await.js b/tools/node_modules/eslint/lib/rules/no-return-await.js
new file mode 100644
index 0000000000..2f06b61108
--- /dev/null
+++ b/tools/node_modules/eslint/lib/rules/no-return-await.js
@@ -0,0 +1,94 @@
+/**
+ * @fileoverview Disallows unnecessary `return await`
+ * @author Jordan Harband
+ */
+"use strict";
+
+const astUtils = require("../ast-utils");
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+const message = "Redundant use of `await` on a return value.";
+
+module.exports = {
+ meta: {
+ docs: {
+ description: "disallow unnecessary `return await`",
+ category: "Best Practices",
+ recommended: false // TODO: set to true
+ },
+ fixable: null,
+ schema: [
+ ]
+ },
+
+ create(context) {
+
+ /**
+ * Reports a found unnecessary `await` expression.
+ * @param {ASTNode} node The node representing the `await` expression to report
+ * @returns {void}
+ */
+ function reportUnnecessaryAwait(node) {
+ context.report({
+ node: context.getSourceCode().getFirstToken(node),
+ loc: node.loc,
+ message
+ });
+ }
+
+ /**
+ * Determines whether a thrown error from this node will be caught/handled within this function rather than immediately halting
+ * this function. For example, a statement in a `try` block will always have an error handler. A statement in
+ * a `catch` block will only have an error handler if there is also a `finally` block.
+ * @param {ASTNode} node A node representing a location where an could be thrown
+ * @returns {boolean} `true` if a thrown error will be caught/handled in this function
+ */
+ function hasErrorHandler(node) {
+ let ancestor = node;
+
+ while (!astUtils.isFunction(ancestor) && ancestor.type !== "Program") {
+ if (ancestor.parent.type === "TryStatement" && (ancestor === ancestor.parent.block || ancestor === ancestor.parent.handler && ancestor.parent.finalizer)) {
+ return true;
+ }
+ ancestor = ancestor.parent;
+ }
+ return false;
+ }
+
+ /**
+ * Checks if a node is placed in tail call position. Once `return` arguments (or arrow function expressions) can be a complex expression,
+ * an `await` expression could or could not be unnecessary by the definition of this rule. So we're looking for `await` expressions that are in tail position.
+ * @param {ASTNode} node A node representing the `await` expression to check
+ * @returns {boolean} The checking result
+ */
+ function isInTailCallPosition(node) {
+ if (node.parent.type === "ArrowFunctionExpression") {
+ return true;
+ }
+ if (node.parent.type === "ReturnStatement") {
+ return !hasErrorHandler(node.parent);
+ }
+ if (node.parent.type === "ConditionalExpression" && (node === node.parent.consequent || node === node.parent.alternate)) {
+ return isInTailCallPosition(node.parent);
+ }
+ if (node.parent.type === "LogicalExpression" && node === node.parent.right) {
+ return isInTailCallPosition(node.parent);
+ }
+ if (node.parent.type === "SequenceExpression" && node === node.parent.expressions[node.parent.expressions.length - 1]) {
+ return isInTailCallPosition(node.parent);
+ }
+ return false;
+ }
+
+ return {
+ AwaitExpression(node) {
+ if (isInTailCallPosition(node) && !hasErrorHandler(node)) {
+ reportUnnecessaryAwait(node);
+ }
+ }
+ };
+ }
+};