Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow default and named exports for rules #9

Open
lencioni opened this issue Feb 1, 2024 · 0 comments
Open

Allow default and named exports for rules #9

lencioni opened this issue Feb 1, 2024 · 0 comments

Comments

@lencioni
Copy link
Contributor

lencioni commented Feb 1, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant