This is a quick guide to help you implement the fix option for a rule.
Reports are made by calling this.log
in a rule visitor. To inform, the linter
instance that a rule is fixable, update the report with the isFixable
option
to true.
this.log({
message,
line,
column,
source,
isFixable: true, // <-- add this, the linter will do another pass to actually fix the fixable issue
});
In the rule context, a mode
is present. If this.mode
is equal to fix
, it
means that the linter is currently running in fix mode.
So, this code:
class MyAwesomeRule extends Rule {
visitor() {
return {
AnyNode(node) {
this.log({
message,
line,
column,
source,
isFixable: true,
});
},
};
}
}
can be transformed to:
const {
builders: { attr, text },
} = require('ember-template-recast');
class MyAwesomeRule extends Rule {
visitor() {
return {
ElementNode(node) {
if (this.mode === 'fix') {
node.attributes.push(attr('zomg', text('foo'))); // <-- fix the node
} else {
this.log({
message,
line,
column,
source,
isFixable: true,
});
}
},
};
}
}
To mention that the rule has a fixer.
To complete the fixer addition, you can update the tests from:
generateRuleTests({
bad: [
{
template,
result: {
message,
source,
line,
column,
},
},
],
});
to:
generateRuleTests({
bad: [
{
template,
fixedTemplate, // <-- add the fixed template
result: {
message,
source,
line,
column,
isFixable: true, // <-- add isFixable: true
},
},
],
});