A utility to extend existing ESLint rules.
npm install eslint-rule-extender
or
yarn add eslint-rule-extender
The default export is a function that takes two arguments and returns the modified rule.
ruleExtender(originalRule: ESLintRule, options: Object) => ESLintRule;
originalRule
- The original rule to extend- options - An object with the desired overrides (options can be viewed below)
const ruleExtender = require('eslint-rule-extender');
const { originalRule } = require('eslint-plugin-example');
const extendedRule = ruleExtender(originalRule, options);
module.exports = {
extendedRule,
};
Overrides for the original rule's meta
property. The properties of the meta
object can be found here.
const extendedRule = ruleExtender(originalRule, {
metaOverrides: {
type: 'suggestion',
fixable: false,
},
});
A function that has the same signature as ESLint rules' create
method. It is passed the context
object and should return an object of visitor callbacks. See the official ESLint docs for more details!
Note that this will not override existing visitors with the same node names. To modify an existing visitor's report results, please use reportOverrides
.
const extendedRule = ruleExtender(originalRule, {
createAdditionalVisitors(context) {
return {
ArrowFunctionExpression(node) {
context.report({ node, messageId: 'anAdditionalSuggestion' });
},
};
},
});
A function that is called with the report metadata of the original rule's context.report()
calls and a modified context
object. The return value of this function is a trinary with the following behavior:
true
: report with original metadata (unchanged)false
: do not report- modified report metadata object: report with this metadata instead
const extendedRule = ruleExtender(originalRule, {
reportOverrides(meta, context) {
if (
meta.node.type === 'FunctionExpression'
&& context.options[1].ignoreFunctionExpression
) {
// These reports will be filtered out.
return false;
}
if (
meta.node.parent.arguments[0] === meta.node
&& meta.node.parent.type === 'CallExpression'
&& meta.node.parent.callee.type === 'Identifier'
&& meta.node.parent.callee.name === context.options[1].disallowedCallExpressionName
) {
// Report with this metadata instead.
return {
...meta,
node: meta.node.parent,
message: `${meta.node.parent.callee.name} no longer supports a callback for the first argument.`,
};
}
// Otherwise, report as usual.
return true;
},
});
const ruleExtender = require('eslint-rule-extender');
const { originalRule } = require('eslint-plugin-example');
const extendedRule = ruleExtender(originalRule, {
metaOverrides: {
type: 'suggestion',
fixable: false,
},
createAdditionalVisitors(context) {
return {
ArrowFunctionExpression(node) {
context.report({ node, messageId: 'anAdditionalSuggestion' });
},
};
},
reportOverrides(meta) {
return meta.node.type !== 'ThisExpression';
},
});
module.exports = {
extendedRule,
};