johnsoncodehk / tsslint

🔋⚡️ The fastest and lightest TypeScript semantic linting solution
MIT License
287 stars 2 forks source link

[@typescript-eslint/return-await] convert eslint not correctly #11

Closed tjx666 closed 4 months ago

tjx666 commented 4 months ago

I add some more test cases in my fork, most of them works fine except the rule @typescript-eslint/return-await.

eslint playground

test code:

// @ts-nocheck

async function invalidInTryCatch1() {
    try {
        return Promise.reject('try');
    } catch (e) {
        // Doesn't execute due to missing await.
    }
}

async function invalidInTryCatch2() {
    try {
        throw new Error('error');
    } catch (e) {
        // Unnecessary await; rejections here don't impact control flow.
        return await Promise.reject('catch');
    }
}

// Prints 'starting async work', 'cleanup', 'async work done'.
async function invalidInTryCatch3() {
    async function doAsyncWork(): Promise<void> {
        console.log('starting async work');
        await new Promise(resolve => setTimeout(resolve, 1000));
        console.log('async work done');
    }

    try {
        throw new Error('error');
    } catch (e) {
        // Missing await.
        return doAsyncWork();
    } finally {
        console.log('cleanup');
    }
}

async function invalidInTryCatch4() {
    try {
        throw new Error('error');
    } catch (e) {
        throw new Error('error2');
    } finally {
        // Unnecessary await; rejections here don't impact control flow.
        return await Promise.reject('finally');
    }
}

async function invalidInTryCatch5() {
    return await Promise.resolve('try');
}

async function invalidInTryCatch6() {
    return await 'value';
}
tjx666 commented 4 months ago

🌚 My config problem, just remove extra empty object in options array.

change from:

'return-await': convertRule((await import('./node_modules/@typescript-eslint/eslint-plugin/dist/rules/return-await.js')).default.default, [{}], 0),

to:

'return-await': convertRule((await import('./node_modules/@typescript-eslint/eslint-plugin/dist/rules/return-await.js')).default.default, [], 0),
johnsoncodehk commented 4 months ago

I found that the following case did not report. Fixed by https://github.com/johnsoncodehk/tsslint/commit/8c74b397cb5f444ad88d8689c3ab4fad9fdf3a22.

// Prints 'starting async work', 'cleanup', 'async work done'.
async function invalidInTryCatch3() {
    async function doAsyncWork(): Promise<void> {
        console.log('starting async work');
        await new Promise(resolve => setTimeout(resolve, 1000));
        console.log('async work done');
    }

    try {
        throw new Error('error');
    } catch (e) {
        // Missing await.
        return doAsyncWork();
    } finally {
        console.log('cleanup');
    }
}