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

Rule Change: no-restricted-exports to accept Regex patterns #18360

Open
1 task done
chrisvltn opened this issue Apr 18, 2024 · 7 comments
Open
1 task done

Rule Change: no-restricted-exports to accept Regex patterns #18360

chrisvltn opened this issue Apr 18, 2024 · 7 comments
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules

Comments

@chrisvltn
Copy link

What rule do you want to change?

no-restricted-exports

What change do you want to make?

Generate more warnings

How do you think the change should be implemented?

Other

Example code

/*eslint no-restricted-exports: ["error", {
    "restrictedNamedExports": ["/^(?!foo)(?!bar).+$/g"]
}]*/

// Success
export const foo = 1;
export function bar() {return 2;}

// Error
const a = {};
export { a };

What does the rule currently do for this code?

The rule currently accepts only hardcoded strings as restrictions

What will the rule do after it's changed?

The rule will accept also regex patterns as name restrictions

Participation

  • I am willing to submit a pull request to implement this change.

Additional comments

Remix framework recommends not exporting anything other than their main route functions in route files, otherwise HMR won't work. Sadly, there is no way to enforce that with ESLint currently.

@chrisvltn chrisvltn added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules labels Apr 18, 2024
@Tanujkanti4441
Copy link
Contributor

Hi @chrisvltn, thanks for the issue.

I think this is a good idea to add regex for no-restricted-exports rule but according to your specification we can also think about an option that would contain only those export names that are allowed and other will be restricted.

@eslint/eslint-team, should we consider this?

@chrisvltn
Copy link
Author

@Tanujkanti4441 great idea. A rule to allow only certain named exports would also fit well to solve the problem :)

@nzakas
Copy link
Member

nzakas commented Apr 23, 2024

I don't feel strongly either way, but if we do make this change, I think we should match how regexes are defined in no-restricted-imports.

@JoshuaKGoldberg
Copy link
Contributor

+1 in general to this request and keeping consistent with equivalent rules. Especially when there's already mirroring in the rule name: e.g. no-restricted-(exports|imports).

For specifically mirroring no-restricted-imports, is there a need to mirror paths? I'd think everything would be doable with patterns that use ^ and/or $ to delineate full matchers.

@mdjermanovic
Copy link
Member

I don't see much similarity between these two rules, despite the similar names.

no-restricted-imports is about disallowing the use of existing modules by specifying string paths or .gitignore-style patterns that match the import source (e.g., "mod" in import { foo } from "mod"). Optionally, only certain names (foo) can be disallowed by additionally specifying importNames or importNamePattern.

no-restricted-exports is about naming new exports. There's no import source to which paths/patterns would correspond.

We could add an option restrictedNamedExportsPattern (a string that will be used as a regex pattern)?

@nzakas
Copy link
Member

nzakas commented Apr 24, 2024

We could add an option restrictedNamedExportsPattern (a string that will be used as a regex pattern)?

I suppose that does go along with the existing restrictedNamedExports option. 👍

Marking as accepted.

@chrisvltn would you like to submit a pull request?

@nzakas nzakas added the accepted There is consensus among the team that this change meets the criteria for inclusion label Apr 24, 2024
@chrisvltn
Copy link
Author

Sure 👍 thanks for the quick discussion. I'll find some time soon to open a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
Status: Ready to Implement
Development

No branches or pull requests

5 participants