ec0lint-plugin-ec0lint-plugin
Version:
An ec0lint plugin for linting ec0lint plugins
81 lines (69 loc) • 2.61 kB
JavaScript
/**
* @author Brad Zacher <https://github.com/bradzacher>
*/
;
const utils = require('../utils');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow rule exports where the export is a function',
category: 'Rules',
recommended: true,
url: 'https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/prefer-object-rule.md',
},
fixable: 'code',
schema: [],
messages: {
preferObject: 'Rules should be declared using the object style.',
},
},
create(context) {
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
const sourceCode = context.getSourceCode();
const ruleInfo = utils.getRuleInfo(sourceCode);
return {
Program() {
if (!ruleInfo || ruleInfo.isNewStyle) {
return;
}
context.report({
node: ruleInfo.create,
messageId: 'preferObject',
*fix(fixer) {
// note - we intentionally don't worry about formatting here, as otherwise we have
// to indent the function correctly
if (
ruleInfo.create.type === 'FunctionExpression' ||
ruleInfo.create.type === 'FunctionDeclaration'
) {
const openParenToken = sourceCode.getFirstToken(
ruleInfo.create,
(token) => token.type === 'Punctuator' && token.value === '('
);
/* istanbul ignore if */
if (!openParenToken) {
// this shouldn't happen, but guarding against crashes just in case
return null;
}
yield fixer.replaceTextRange(
[ruleInfo.create.range[0], openParenToken.range[0]],
'{create'
);
yield fixer.insertTextAfter(ruleInfo.create, '}');
} else if (ruleInfo.create.type === 'ArrowFunctionExpression') {
yield fixer.insertTextBefore(ruleInfo.create, '{create: ');
yield fixer.insertTextAfter(ruleInfo.create, '}');
}
},
});
},
};
},
};