Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Rule proposal: functions should not be async unless they await #9284

Closed as not planned
@benmccann

Description

@benmccann

Before You File a Proposal Please Confirm You Have Done The Following...

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

From https://github.com/sveltejs/svelte/blob/862949d22abf2996594b4315c04fc97e18bc4408/packages/svelte/src/compiler/preprocess/replace_in_code.js#L23:

/**
 * @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement: new plugin ruleNew rule request for eslint-pluginlocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-pluginwontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions