eslint-plugin-rulesdir
eslint-plugin-rulesdir copied to clipboard
Allow default and named exports for rules
Hi! 👋
Firstly, thanks for your work on this project! 🙂
Today I used patch-package to patch [email protected] for the project I'm working on.
I have been refactoring our custom ESLint rules to use @typescript-eslint's RuleCreator. Previously, our rules used separate named exports for each of the rule's parts, like create, meta, etc. With the RuleCreator, we need to have a single export. Their docs show this as export const rule.
However, with that change I ran into an issue with our refactored rules because ESLint reported that "rule.create is not a function" for these rules. I added some logging and determined that the custom rules were not in the shape that ESLint was expecting them, and rule.create was indeed not a function.
Here is the diff that solved my problem:
diff --git a/node_modules/eslint-plugin-rulesdir/index.js b/node_modules/eslint-plugin-rulesdir/index.js
index 3aa2a39..8aa1d91 100644
--- a/node_modules/eslint-plugin-rulesdir/index.js
+++ b/node_modules/eslint-plugin-rulesdir/index.js
@@ -39,7 +39,9 @@ module.exports = {
if (rulesObject[ruleName]) {
throw new Error(`eslint-plugin-rulesdir found two rules with the same name: ${ruleName}`);
}
- rulesObject[ruleName] = require(absolutePath);
+ const ruleModule = require(absolutePath);
+ const ruleExport = ruleModule.default || ruleModule;
+ rulesObject[ruleName] = ruleExport.rule || ruleExport;
});
});
cache[cacheKey] = rulesObject;
This adds support for both default exports (export default createRule(...)) and named rule exports (export const rule = createRule(...)). The named export is the main one that is necessary, but I figured it was easy enough to also add default export support so I went with it as well.
What do you think about adding this or something similar to this plugin?
This issue body was partially generated by patch-package.