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

Enhancement: Add defaults for TSESLint.RuleContext type parameters #8147

Closed
4 tasks done
JoshuaKGoldberg opened this issue Dec 27, 2023 · 2 comments
Closed
4 tasks done
Labels
enhancement New feature or request triage Waiting for maintainers to take a look

Comments

@JoshuaKGoldberg
Copy link
Member

JoshuaKGoldberg commented Dec 27, 2023

Before You File a Proposal Please Confirm You Have Done The Following...

Relevant Package

utils

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Description

Right now, RuleContext doesn't provide defaults for its type parameters:

interface RuleContext<
TMessageIds extends string,
TOptions extends readonly unknown[],
> {

That means if you want to describe any arbitrary rule context the way e.g. #8146 does, you have to explicitly type out RuleContext<string, unknown[]>. It's a little inconvenient.

Proposal: let's add defaults to make referring to an "unknown" rule context a little easier?

interface RuleContext<
  TMessageIds extends string = string,
  TOptions extends readonly unknown[] = unknown[],
> {

Additional Info

#8148 is a draft PR showing the changes internally.

@JoshuaKGoldberg JoshuaKGoldberg added enhancement New feature or request triage Waiting for maintainers to take a look labels Dec 27, 2023
@bradzacher
Copy link
Member

bradzacher commented Dec 27, 2023

I think it's fine to not provide defaults because it means that you have to specifically opt-in to the unbounded, unsafe behaviour.

This is good because it means that if someone is writing a rule and wants to pass the context around they have to pass the parameters - preventing unsafe code.

For example this is a very common pattern I've seen:

const someUtil = (context: RuleContext<MessageIds, Options>) => { ... };

const rule = createRule<MessageIds, Options>({
  // ...
  create(context) {
    someUtil(context);
  },
});

You might say "but if they move the function into the create body they could remove the param entirely!" and you're not wrong - but who are we to dictate others preferred style?

If we default the type params then it's valid for the user to define context: RuleContext for said function - which means that when that function calls eg contex.report it will have zero safety around the message ids!

I'd much rather export an obviously unsafe version of the type like we do for the RuleModule

type AnyRuleModule = RuleModule<string, readonly unknown[]>;

@JoshuaKGoldberg
Copy link
Member Author

Makes sense to me! Closing as wontfix. Thanks!

@JoshuaKGoldberg JoshuaKGoldberg closed this as not planned Won't fix, can't repro, duplicate, stale Dec 29, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request triage Waiting for maintainers to take a look
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants