SamVerschueren / clinton

Project style linter
MIT License
122 stars 9 forks source link

Refactor fixers #59

Closed SamVerschueren closed 7 years ago

SamVerschueren commented 7 years ago

Currently it is possible to write fixers for a rule, for instance the gitignore fixer which is quite easy.

A downside however is that the fixer directly interacts with the file on the filesystem. This means that they are quite hard to test.

This is what I suggest as an improvement. When a rule reports an error or a warning, it also specifies the file in which the error occurred. This means that our core knows which file the fixer will operate on. So instead of reading and writing those files inside the rule, it could easily happen in the core of clinton.

ctx.report({
    message: 'Some error',
    file: 'path/to/package.json',
    fix: pkg => {
        pkg.description = 'Change the description';
        return pkg;
    }
});

The content of the file will be passed to the fixer as an argument. The fixer can easily return the new content of the file, or a promise that resolves the new content of the file. In case of json, yml/yaml files, the object is parsed beforehand to make it easier to work with. The core will use detect-indent to write the file back in the correct format. If it's not one of those files, it will just pass the raw contents to the fixer and writes the content back to disk as returned by the fixer. No parsing will be done whatsoever.