Skip to content
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

feat: add suggestion for no-return-await #16637

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/rules/no-return-await.js
Expand Up @@ -13,6 +13,7 @@ const astUtils = require("./utils/ast-utils");
/** @type {import('../shared/types').Rule} */
module.exports = {
meta: {
hasSuggestions: true,
type: "suggestion",

docs: {
Expand All @@ -29,6 +30,7 @@ module.exports = {
],

messages: {
removeAwait: "Remove await after return statement.",
dbartholomae marked this conversation as resolved.
Show resolved Hide resolved
redundantUseOfAwait: "Redundant use of `await` on a return value."
}
},
Expand All @@ -44,7 +46,19 @@ module.exports = {
context.report({
node: context.getSourceCode().getFirstToken(node),
loc: node.loc,
messageId: "redundantUseOfAwait"
messageId: "redundantUseOfAwait",
suggest: [
{
messageId: "removeAwait",
data: { type: "block" },
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
fix(fixer) {
const range = [node.range[0], node.range[0] + 6];
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

return fixer.replaceTextRange(range, "");
dbartholomae marked this conversation as resolved.
Show resolved Hide resolved
}
}
]

});
}

Expand Down
122 changes: 91 additions & 31 deletions tests/lib/rules/no-return-await.js
Expand Up @@ -16,8 +16,24 @@ const { RuleTester } = require("../../../lib/rule-tester");
// Tests
//------------------------------------------------------------------------------

// pending https://github.com/eslint/espree/issues/304, the type should be "Keyword"
const errors = [{ messageId: "redundantUseOfAwait", type: "Identifier" }];
/**
* Creates the list of errors that should be found by this rule
* @param {Object} options Options for creating errors
* @param {string} options.suggestionOutput The suggested output
* @returns {Array} the list of errors
*/
function createErrorList({ suggestionOutput: output }) {

// pending https://github.com/eslint/espree/issues/304, the type should be "Keyword"
return [{
messageId: "redundantUseOfAwait",
type: "Identifier",
suggestions: [{
messageId: "removeAwait", data: { type: "block" }, output
}]
}];
}


const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2017 } });

Expand Down Expand Up @@ -138,99 +154,99 @@ ruleTester.run("no-return-await", rule, {
invalid: [
{
code: "\nasync function foo() {\n\treturn await bar();\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync function foo() {\n\treturn bar();\n}\n" })
},
{
code: "\nasync function foo() {\n\treturn (a, await bar());\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync function foo() {\n\treturn (a, bar());\n}\n" })
},
{
code: "\nasync function foo() {\n\treturn (a, b, await bar());\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync function foo() {\n\treturn (a, b, bar());\n}\n" })
},
{
code: "\nasync function foo() {\n\treturn (a && await bar());\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync function foo() {\n\treturn (a && bar());\n}\n" })
},
{
code: "\nasync function foo() {\n\treturn (a && b && await bar());\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync function foo() {\n\treturn (a && b && bar());\n}\n" })
},
{
code: "\nasync function foo() {\n\treturn (a || await bar());\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync function foo() {\n\treturn (a || bar());\n}\n" })
},
{
code: "\nasync function foo() {\n\treturn (a, b, (c, d, await bar()));\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync function foo() {\n\treturn (a, b, (c, d, bar()));\n}\n" })
},
{
code: "\nasync function foo() {\n\treturn (a, b, (c && await bar()));\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync function foo() {\n\treturn (a, b, (c && bar()));\n}\n" })
},
{
code: "\nasync function foo() {\n\treturn (await baz(), b, await bar());\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync function foo() {\n\treturn (await baz(), b, bar());\n}\n" })
},
{
code: "\nasync function foo() {\n\treturn (baz() ? await bar() : b);\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync function foo() {\n\treturn (baz() ? bar() : b);\n}\n" })
},
{
code: "\nasync function foo() {\n\treturn (baz() ? a : await bar());\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync function foo() {\n\treturn (baz() ? a : bar());\n}\n" })
},
{
code: "\nasync function foo() {\n\treturn (baz() ? (a, await bar()) : b);\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync function foo() {\n\treturn (baz() ? (a, bar()) : b);\n}\n" })
},
{
code: "\nasync function foo() {\n\treturn (baz() ? a : (b, await bar()));\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync function foo() {\n\treturn (baz() ? a : (b, bar()));\n}\n" })
},
{
code: "\nasync function foo() {\n\treturn (baz() ? (a && await bar()) : b);\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync function foo() {\n\treturn (baz() ? (a && bar()) : b);\n}\n" })
},
{
code: "\nasync function foo() {\n\treturn (baz() ? a : (b && await bar()));\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync function foo() {\n\treturn (baz() ? a : (b && bar()));\n}\n" })
},
{
code: "\nasync () => { return await bar(); }\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync () => { return bar(); }\n" })
},
{
code: "\nasync () => await bar()\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync () => bar()\n" })
},
{
code: "\nasync () => (a, b, await bar())\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync () => (a, b, bar())\n" })
},
{
code: "\nasync () => (a && await bar())\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync () => (a && bar())\n" })
},
{
code: "\nasync () => (baz() ? await bar() : b)\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync () => (baz() ? bar() : b)\n" })
},
{
code: "\nasync () => (baz() ? a : (b, await bar()))\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync () => (baz() ? a : (b, bar()))\n" })
},
{
code: "\nasync () => (baz() ? a : (b && await bar()))\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync () => (baz() ? a : (b && bar()))\n" })
},
{
code: "\nasync function foo() {\nif (a) {\n\t\tif (b) {\n\t\t\treturn await bar();\n\t\t}\n\t}\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync function foo() {\nif (a) {\n\t\tif (b) {\n\t\t\treturn bar();\n\t\t}\n\t}\n}\n" })
},
{
code: "\nasync () => {\nif (a) {\n\t\tif (b) {\n\t\t\treturn await bar();\n\t\t}\n\t}\n}\n",
errors
errors: createErrorList({ suggestionOutput: "\nasync () => {\nif (a) {\n\t\tif (b) {\n\t\t\treturn bar();\n\t\t}\n\t}\n}\n" })
},
{
code: `
Expand All @@ -241,7 +257,16 @@ ruleTester.run("no-return-await", rule, {
}
}
`,
errors
errors: createErrorList({
suggestionOutput: `
async function foo() {
try {}
finally {
return bar();
}
}
`
})
},
{
code: `
Expand All @@ -252,7 +277,16 @@ ruleTester.run("no-return-await", rule, {
}
}
`,
errors
errors: createErrorList({
suggestionOutput: `
async function foo() {
try {}
catch (e) {
return bar();
}
}
`
})
},
{
code: `
Expand All @@ -262,15 +296,29 @@ ruleTester.run("no-return-await", rule, {
}
} catch (e) {}
`,
errors
errors: createErrorList({
suggestionOutput: `
try {
async function foo() {
return bar();
}
} catch (e) {}
`
})
},
{
code: `
try {
async () => await bar();
} catch (e) {}
`,
errors
errors: createErrorList({
suggestionOutput: `
try {
async () => bar();
} catch (e) {}
`
})
},
{
code: `
Expand All @@ -284,7 +332,19 @@ ruleTester.run("no-return-await", rule, {
}
}
`,
errors
errors: createErrorList({
suggestionOutput: `
async function foo() {
try {}
catch (e) {
try {}
catch (e) {
return bar();
}
}
}
`
})
}
]
});