Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin): [await-thenable] added suggestion fixer#7437

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletionspackages/eslint-plugin/src/rules/await-thenable.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
import type { TSESLint } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';

import * as util from '../util';
Expand All@@ -10,8 +11,10 @@ export default util.createRule({
recommended: 'recommended',
requiresTypeChecking: true,
},
hasSuggestions: true,
messages: {
await: 'Unexpected `await` of a non-Promise (non-"Thenable") value.',
removeAwait: 'Remove unnecessary `await`.',
},
schema: [],
type: 'problem',
Expand All@@ -35,6 +38,23 @@ export default util.createRule({
context.report({
messageId: 'await',
node,
suggest: [
{
messageId: 'removeAwait',
fix(fixer): TSESLint.RuleFix {
const sourceCode = context.getSourceCode();
const awaitKeyword = util.nullThrows(
sourceCode.getFirstToken(node, util.isAwaitKeyword),
util.NullThrowsReasons.MissingToken(
'await',
'await expression',
),
);

return fixer.remove(awaitKeyword);
},
},
],
});
}
},
Expand Down
182 changes: 129 additions & 53 deletionspackages/eslint-plugin/tests/rules/await-thenable.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
import { RuleTester } from '@typescript-eslint/rule-tester';
import {noFormat,RuleTester } from '@typescript-eslint/rule-tester';

import rule from '../../src/rules/await-thenable';
import { getFixturesRootDir } from '../RuleTester';
Expand DownExpand Up@@ -202,33 +202,83 @@ const doSomething = async (

invalid: [
{
code: `
async function test() {
await 0;
await 'value';

await (Math.random() > 0.5 ? '' : 0);

class NonPromise extends Array {}
await new NonPromise();
}
`,
code: 'await 0;',
errors: [
{
line: 3,
line: 1,
messageId,
suggestions: [
{
messageId: 'removeAwait',
output: ' 0;',
},
],
},
],
},
{
code: "await 'value';",
errors: [
{
line: 1,
messageId,
suggestions: [
{
messageId: 'removeAwait',
output: " 'value';",
},
],
},
],
},
{
code: "async () => await (Math.random() > 0.5 ? '' : 0);",
errors: [
{
line:4,
line:1,
messageId,
suggestions: [
{
messageId: 'removeAwait',
output: "async () => (Math.random() > 0.5 ? '' : 0);",
},
],
},
],
},
{
code: noFormat`async () => await(Math.random() > 0.5 ? '' : 0);`,
errors: [
{
line:6,
line:1,
messageId,
suggestions: [
{
messageId: 'removeAwait',
output: "async () => (Math.random() > 0.5 ? '' : 0);",
},
],
},
],
},
{
code: `
class NonPromise extends Array {}
await new NonPromise();
`,
errors: [
{
line:9,
line:3,
messageId,
suggestions: [
{
messageId: 'removeAwait',
output: `
class NonPromise extends Array {}
new NonPromise();
`,
},
],
},
],
},
Expand All@@ -247,58 +297,84 @@ async function test() {
{
line: 8,
messageId,
suggestions: [
{
messageId: 'removeAwait',
output: `
async function test() {
class IncorrectThenable {
then() {}
}
const thenable = new IncorrectThenable();

thenable;
}
`,
},
],
},
],
},
{
code: `
const doSomething = async (
obj1: { a?: { b?: { c?: () => void } } },
obj2: { a?: { b?: { c: () => void } } },
obj3: { a?: { b: { c?: () => void } } },
obj4: { a: { b: { c?: () => void } } },
obj5: { a?: () => { b?: { c?: () => void } } },
obj6?: { a: { b: { c?: () => void } } },
callback?: () => void,
): Promise<void> => {
await obj1.a?.b?.c?.();
await obj2.a?.b?.c();
await obj3.a?.b.c?.();
await obj4.a.b.c?.();
await obj5.a?.().b?.c?.();
await obj6?.a.b.c?.();

await callback?.();
};
declare const callback: (() => void) | undefined;
await callback?.();
`,
errors: [
{
line: 11,
messageId,
},
{
line: 12,
messageId,
},
{
line: 13,
messageId,
},
{
line: 14,
messageId,
},
{
line: 15,
line: 3,
messageId,
suggestions: [
{
messageId: 'removeAwait',
output: `
declare const callback: (() => void) | undefined;
callback?.();
`,
},
],
},
],
},
{
code: `
declare const obj: { a?: { b?: () => void } };
await obj.a?.b?.();
`,
errors: [
{
line:16,
line:3,
messageId,
suggestions: [
{
messageId: 'removeAwait',
output: `
declare const obj: { a?: { b?: () => void } };
obj.a?.b?.();
`,
},
],
},
],
},
{
code: `
declare const obj: { a: { b: { c?: () => void } } } | undefined;
await obj?.a.b.c?.();
`,
errors: [
{
line:18,
line:3,
messageId,
suggestions: [
{
messageId: 'removeAwait',
output: `
declare const obj: { a: { b: { c?: () => void } } } | undefined;
obj?.a.b.c?.();
`,
},
],
},
],
},
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp