custom rule checking source code

229 views Asked by At

I am making a custom rule with ESLint.

Basically:

module.exports = function (context) {
    var file = context.getSource();
    var fileName = context.getFilename();
    var lines = file.split(/\n/);
    lines.forEach(function(line, i){
        // [...] validation logic

        var report = {
            message: 'Code style error.'
        };

        report.loc = {
            line: i + 1,
            col: 1 // I have some logic for this working
        };

        context.report(report);
    });

    return {}; // do I need this?
};

My code can find the errors I'm looking for, but I'm having problems reporting them to ESLint.

I get:

Error while loading rule 'test-rule': Cannot read property 'type' of undefined

How should I configure the context.report(report); and should this module have a return since I am not using AST at all?

Any suggestions on what I am missing?

1

There are 1 answers

0
Gyandeep On BEST ANSWER

As of today, you have to supply node key also. But I know docs say otherwise thats why I have opened an issue there: https://github.com/eslint/eslint/issues/4220