skip to Main Content

I’m encountering an issue with integrating async/await operations within a custom ESLint rule. Here’s a simplified version of my code:

module.exports = {
    meta: {
        type: "problem",
        docs: {
            description: "This rule checks if any vulnerabilities appear in an imported package"
        },
        fixable: false,
        schema: []
    },
    create(context) {
        return {
            async ImportDeclaration(node) {
                const data = await apiCall();
                context.report({
                    node,
                    message: "message"
                });
            }
        };
    }
}

The problem arises when I include an async operation (await apiCall()) within the ImportDeclaration callback. The context.report() method doesn’t seem to work as expected. However, when I remove the async/await operation, context.report() functions properly.

Can someone guide me on how to properly handle async operations within ESLint rule callbacks, ensuring that context.report() functions correctly? Any insights or alternative approaches would be greatly appreciated. Thank you!

2

Answers


  1. Chosen as BEST ANSWER

    https://www.youtube.com/watch?v=XbpS8UCQa7E&t=190s

    made this 👆 for the problem, Check out this if you guys have the same problem


  2. The create() function provided to module.exports should be synchronous. It cannot be an async function. You can use asynchronous operations inside rule callbacks, but the create() function itself should not be asynchronous

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search