Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Rule Proposal: Override checking #293

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

Closed
mzyil opened this issue Feb 18, 2019 · 17 comments
Closed

Rule Proposal: Override checking #293

mzyil opened this issue Feb 18, 2019 · 17 comments
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@mzyil
Copy link

mzyil commented Feb 18, 2019

Hi, we would like to have an officially supported rule to check for overrides. In a massive code base that is written entirely in TypeScript, small functionalities like these makes a big difference.

There is already a plugin we use (and contribute to) for tslint: https://github.com/hmil/tslint-override

There was a lot of issues surrounding this, but since this project aims to re-design some of the functionality may be it's possible to include support for this as well?

This is the excerpt from the said package README.md: (notice the new lines, we believe it was much prettier)

export class Basic {
    public overridePlease(): void { }
    public doNotOverride(): void { }
}

export class Child extends Basic {
    public doNotOverride(): void { } // ERROR: Method Child#doNotOverride is overriding Basic#doNotOverride. Use the @override JSDoc tag if the override is intended

    // Make it explicit that you intend to override this member
    /** @override */
    public overridePlease(): void { }

    // Alternatively, you can use the decorator syntax
    @override
    public overridePlease(): void { }

    // Typos won't bother you anymore
    /** @override */ public overidePlease(): void { } // ERROR: Method with @override tag is not overriding anything
}

We would also want to contribute to this project if this proposal is seen acceptable.

@mzyil mzyil added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Feb 18, 2019
@bradzacher
Copy link
Member

the biggest problem I see with this proposal: comment attachment is no longer supported by eslint.

without comment attachment support, we would have to implement our own logic to attach jsdoc comments to the methods, as well as our own logic to parse jsdoc comments.

that being said, a decorator version is pretty simple to implement.

@bradzacher bradzacher added enhancement: new plugin rule New rule request for eslint-plugin and removed triage Waiting for team members to take a look labels Feb 19, 2019
@platinumazure
Copy link
Contributor

@bradzacher ESLint still attempts to attach comments to nodes although the API might be different than how esprima/espree used to do it. ESLint stopped supporting JSDoc specifically, but it should be possible to get the comments themselves and check for JSDoc (just check that the first character in the comment value is *, representing /**). Am I missing something?

@mzyil
Copy link
Author

mzyil commented Feb 19, 2019

@bradzacher one problem with the decorator solution was that the rule became sensitive of the order of the declaration inside the same file. It's possible that the rule's algorithm had a bug. Nevertheless that was why we opted for the JSDoc option in our project. Plus AFAIK decorators are still experimental and not every project uses them.

If detecting overrides is not an issue I don't think adding a comment would be harder.

@bradzacher
Copy link
Member

@mzyil - it's relatively straightforward to check overrides though, as it's a clear algorithm.

Decorators have first-class support in the parser, but comment attachment does not. However if it's supported by in eslint (like @platinumazure says it is), then it shouldn't be too hard?

@platinumazure - is comment attachment provided by the underlying parser or by eslint itself? If it's the former - we haven't added support for it afaik.

@mysticatea
Copy link
Member

Probably we can use sourceCode.getCommentsBefore(nodeOrToken) API.

@platinumazure
Copy link
Contributor

@bradzacher ESLint does it and it should work, assuming the parser's nodes, tokens, and comments (or maybe just nodes and comments) have proper location information. I think it uses range but I'm not 100% sure.

As @mysticatea says, sourceCode.getCommentsBefore(nodeOrToken) should get you very close to what you need. You'll just need to filter the results by whether or not they are JSDoc comments (i.e., start with /** slash value starts with *).

@bradzacher
Copy link
Member

awesome. I didn't realise it was that simple.
we could definitely consider supporting comments then!

(just spitballing so we're covering out bases)

If comments are that simple I'd rather support comments and not decorators considering that the decorator isn't (and can't be) provided by us - it'd rely on the user implementing (or importing a 3rd party implementation).
An @overrides decorator would be a noop (i.e. does nothing at runtime), which creates a useless runtime artefact. Considering we're about promoting best practices, it feels like it'd be the wrong thing for us to endorse?

@eyedean
Copy link

eyedean commented Jun 19, 2019

I just found https://github.com/bet365/override-linting-rule as a 3rd party that supports decorators.

Is this something that can be imported as a @typescript-eslint/ rule?

@bradzacher
Copy link
Member

unfortunately not. that is a tslint plugin which does not interop with eslint.

@InExtremaRes
Copy link

Have there been any progress on this? Is it accepted o rejected?

@bradzacher
Copy link
Member

This is a community run project.
It's waiting for someone from the community to work on it.
So far, nobody has found it hi-pri enough to pick up.

If you want to have a go at it, feel free to raise a pr!

@InExtremaRes
Copy link

Thank you @bradzacher for the quick answer. Sadly I've never worked with the internals of tslint or eslint, I don't feel prepared to send a PR. Is there any other way to support the project and a feature (like this) in particular?

@bradzacher
Copy link
Member

Not hugely, no. This pr has few reactions, so it's pretty low in the priority queue for the maintainers.

For now the only way it will be implemented any time soon is via a community contributor.

@achimmihca
Copy link

This pr has few reactions

I came here from the "override keyword discussion" for TypeScript.
I think there is a huge demand for this, but it does not show in this ESLint thread. However, an ESLint rule would be a good alternative to a native language feature.

Please re-consider implementing this rule, and when you do, also consider the amount of feedback from the TypeScript issue.

@bradzacher
Copy link
Member

There's nothing to reconsider.
It just needs someone to build it.

See prior comment about this being a community maintained project. That means there's no team being paid to work and build features out on this project - it relies on the support from the open source community to volunteer their time to contribute fixed and enhancements.

The volunteer maintainers spend most of their time reviewing issues and PRs.

If someone from the community wants to tackle it, happy to accept a PR!!! That would be the best way to move this forward and get a rule built.

@ark120202
Copy link

Note that there is microsoft/TypeScript#39669

@bradzacher
Copy link
Member

This will be released with TS4.3
https://github.com/microsoft/TypeScript-wiki/blob/cf2951bcd15733844aed53b31020ab2fe50c15ed/Roadmap.md#43-may-2021

There will be a new flag (noImplicitOverride) and a new keyword (override)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 30, 2021
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 package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

8 participants