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

feat(eslint-plugin): added new rule no-untyped-public-signature #801

Merged
merged 36 commits into from Nov 12, 2019
Merged

Conversation

eranshabi
Copy link
Contributor

@eranshabi eranshabi commented Aug 4, 2019

A rule we use in our company with good benefit.
This rule prevents adding a public method with untyped parameters or return type.

Public methods are meant to be used in scopes external to the class itself, and having them typed makes them more readable and easier to use.

There is also the ignoredMethods option so that you can ignore methods you don't care to explicit type (for example react lifecycle hooks)

"@typescript-eslint/no-untyped-public-signature": ["error", { "ignoredMethods": ["ignoredMethodName"] }]

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @eranshabi!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint

@bradzacher bradzacher added the enhancement: new plugin rule New rule request for eslint-plugin label Aug 5, 2019
@octogonz
Copy link
Contributor

octogonz commented Aug 8, 2019

How is this different from "@typescript-eslint/typedef" + "@typescript-eslint/explicit-function-return-type"?

@eranshabi
Copy link
Contributor Author

eranshabi commented Aug 9, 2019

How is this different from "@typescript-eslint/typedef" + "@typescript-eslint/explicit-function-return-type"?

This rule is for public methods. Public methods are meant to be used in scopes external to the class itself, and having them typed makes them more readable and easier to use. Sometimes you don't want to type everything but only what you think is important.

Also this rule has the option to ignore specific methods which you don't care to type (in our case we use it for react lifecycle hooks).

@bradzacher
Copy link
Member

Put another way, the intention here is to provide a way to enforce explicit typing of module boundaries so that you can be sure of the exact types you're exposing.

In the same vein as @octogonz's question - would this be better served as an option on explicit-function-return-type, instead of an entirely new rule?
I like the idea, for sure, but it seems like a user would use either this rule, or e-f-r-t, as this rule is a subset of e-f-r-t.

@eranshabi
Copy link
Contributor Author

Thanks for the feedback.
I have no problem merging this rule with another, yet I still think we don't fully understand each other - This rule checks both the return type and the parameters type of all public class methods, so it's not a subset of another rule afaik.

@bradzacher
Copy link
Member

This rule checks both the return type and the parameters type of all public class methods

I realised that a little after I wrote the comment (but I wasn't at my keyboard), sorry.

It is probably worth keeping this rule as is. In future we can augment it (and rename it) so that it supports more cases of explicitly typing module boundaries.

@octogonz
Copy link
Contributor

octogonz commented Aug 9, 2019

BTW it's worth pointing out that TSLint's typedef handled all the different kinds of type annotations (parameters, return values, properties, variables, etc). I always found a little strange for ESLint to treat them as separate rules.

If we followed TSLint's model, then "don't require types for private members" could be an option for the one rule, rather than having these criteria split across 3 rules. For the end user experience, it might make configuration easier, since people wouldn't have to understand the exclusive-or between typedef + explicit-function-return-type versus no-untyped-public-signature. Just a thought.

@bradzacher
Copy link
Member

The TL;DR is that nobody wanted to implement the full typedef rule until very recently, but efrt was wanted much earlier.

Most of typedef is covered by noImplicitAny (esp in the latest version), and there are a number of rare use cases (like explicitly typing destructuring), so nobody saw the need in implementing the entire rule.

But ~2 years ago someone saw value in enforcing efrt, and so that got implemented.
Since then efrt has grown larger than what typedef supports. No doubt it will probably continue evolve as people want more cases / flexibility (eg I've been thinking about making it more accurate by making it "type-aware").

I tend to want to avoid monolithic rules as well. It sounds great being able to configure a single rule to do all of these things, but users often avoid the rules because they're big and scary (documentation is long, and there are many options to configure), and maintaining the rule can be hard because you're putting X distinct code paths into a single file.

There's tradeoffs to both approaches.

@eranshabi
Copy link
Contributor Author

so if I understand you correctly @bradzacher, this can be merged?
If not, what changes are required?

@bradzacher
Copy link
Member

I haven't had a chance to review the code yet, sorry.
I'll be sure to comment with required changes (if any) once I've had a chance to review

@eranshabi
Copy link
Contributor Author

fixed the build 🙃

@eranshabi
Copy link
Contributor Author

@bradzacher please?

@bradzacher
Copy link
Member

This PR is in the queue for me to review.
I haven't had any spare time over the last few weeks due to family things.
I'll get to it soon.

Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few comments, code otherwise looks good.
Thanks for your contribution!

bradzacher
bradzacher previously approved these changes Nov 12, 2019
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for this

@codecov
Copy link

codecov bot commented Nov 12, 2019

Codecov Report

Merging #801 into master will increase coverage by 0.03%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #801      +/-   ##
==========================================
+ Coverage   94.02%   94.05%   +0.03%     
==========================================
  Files         122      123       +1     
  Lines        5253     5283      +30     
  Branches     1456     1468      +12     
==========================================
+ Hits         4939     4969      +30     
  Misses        179      179              
  Partials      135      135
Impacted Files Coverage Δ
...nt-plugin/src/rules/no-untyped-public-signature.ts 100% <100%> (ø)
packages/eslint-plugin/src/rules/index.ts 100% <100%> (ø) ⬆️

@bradzacher bradzacher merged commit c5835f3 into typescript-eslint:master Nov 12, 2019
@akoidan
Copy link

akoidan commented Nov 18, 2019

Your new change @bradzacher breaks consctuctor description:

  public constructor() {
    this.httpLogger = loggerFactory.getLoggerColor("http", "#680061");
  }

16:3 warning Public method has no return type @typescript-eslint/no-untyped-public-signature

@bradzacher
Copy link
Member

please raise a new issue for this.
Comments on closed PRs is not a great way to report issues or track work.

@typescript-eslint typescript-eslint locked as resolved and limited conversation to collaborators Nov 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants