Description
Before You File a Proposal Please Confirm You Have Done The Following...
- I have searched for related issues and found none that match my proposal.
- I have searched the current rule list and found no rules that match my proposal.
- I have read the FAQ and my problem is not listed.
My proposal is suitable for this project
- My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
- My proposal is not a "formatting rule"; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
- I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).
Description
The rule would check that we don't cause unnecessary performance losses. The async
keyword should not be used where TypeScript can already determine that a Promise
is being returned and there is no await
as adding async
in that case will only cause a slight performance hit
Fail Cases
/**
* @param {RegExp} re
* @param {(...match: any[]) => Promise<MappedCode>} get_replacement
* @param {string} source
*/
async function calculate_replacements(re, get_replacement, source) {
/**
* @type {Array<Promise<import('./private.js').Replacement>>}
*/
const replacements = [];
source.replace(re, (...match) => {
replacements.push(
get_replacement(...match).then((replacement) => {
const matched_string = match[0];
const offset = match[match.length - 2];
return { offset, length: matched_string.length, replacement };
})
);
return '';
});
return Promise.all(replacements);
}
Pass Cases
/**
* @param {RegExp} re
* @param {(...match: any[]) => Promise<MappedCode>} get_replacement
* @param {string} source
*/
function calculate_replacements(re, get_replacement, source) {
/**
* @type {Array<Promise<import('./private.js').Replacement>>}
*/
const replacements = [];
source.replace(re, (...match) => {
replacements.push(
get_replacement(...match).then((replacement) => {
const matched_string = match[0];
const offset = match[match.length - 2];
return { offset, length: matched_string.length, replacement };
})
);
return '';
});
return Promise.all(replacements);
}
Additional Info
There is https://typescript-eslint.io/rules/promise-function-async, but it's almost exactly opposite of this. I recently turned on a handful of promise-related rules in sveltejs/svelte
, but got a ton of pushback on @typescript-eslint/promise-function-async
. The consensus seemed to be that the rule seemed like bad practice. The description of the rule in the docs doesn't seem to match at all what it actually does. I think either its docs could be improved or that rule could be removed