How to pass data to Custom ESLint rule?

335 Views Asked by At

I have written a custom ESLint rule as follows:

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
    meta: {
        type: "suggestion",
        docs: {
            description: "Test Description",
            category: "Common",
            recommended: true,
            url: ""
        },
        fixable: "code",
        schema: [] // no options
    },
    create: function (context) {
        return {
            ClassDeclaration(node) {
            var data = ""// I want to pass value here from CMD
                if (node.id.name === data)
                {
                    context.report({
                                node: node,
                                message: "ESLint rule triggered"
                            });
                }
            }
        }
    }
}

And I am running ESLint for a Test.js file using following command in CMD.

npx eslint Test.js

Is there a way in which I can pass data to the rule from CMD while executing eslint command? I want to pass a value to the variable 'data' from

1

There are 1 best solutions below

0
On

Yes, it's possible with Node.js environment variables. process.env includes environment variables you set manually:

EXAMPLE=abc123 npx eslint Test.js
// in your rule file
console.log(process.env.EXAMPLE);

You might also consider using rule options set in the ESLint config. Those can be set with environment variables, too.

(if you're on a Windows bash prompt, see cross-env to set EXAMPLE=abc123)