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

Add lint checks for common coding issues arising in code reviews. #3905

Closed
28 of 34 tasks
seanlip opened this issue Sep 27, 2017 · 107 comments
Closed
28 of 34 tasks

Add lint checks for common coding issues arising in code reviews. #3905

seanlip opened this issue Sep 27, 2017 · 107 comments

Comments

@seanlip
Copy link
Member

seanlip commented Sep 27, 2017

In code reviews, I've noticed a lot of the same style issues come up over and over again, and in order to conserve reviewer time and give contributors faster feedback, these issues should really be addressed by pre-review lint checks. Adding these will also help maintain consistency in our codebase, and ensure that this state of affairs continues to be preserved in the future.

We can handle some of these issues by importing third-party libraries (such as pycodestyle) that might already have support for these checks, but it might also be a good idea to have a framework for adding additional custom lint checks that are specific to our codebase and aren't easily handled by third-party libraries.

For reference, here is a partial list of the issues I've observed. Note to contributors: it is absolutely fine to submit a PR that gets rid of just one or two of these. Note that the corresponding lint errors will need to be fixed before the PR goes in; this should be a good test of the checker that you've implemented.

@Oishikatta
Copy link
Contributor

Here is a start to the design doc, I'm not sure on the formatting/what it should have.

https://docs.google.com/document/d/18VHyxIfvts5LycKOwLU6-fDT0aRx30XEl1ErafdvD18/edit?usp=sharing

@seanlip
Copy link
Member Author

seanlip commented Oct 4, 2017

Thanks @Oishikatta! I left some feedback in the doc.

@anthkris
Copy link
Contributor

Howdy @Oishikatta Any progress on this?

@jacobdavis11
Copy link
Member

@Oishikatta how is this going?

@seanlip
Copy link
Member Author

seanlip commented Nov 15, 2017

Deassigning @Oishikatta due to lack of response.

@seanlip
Copy link
Member Author

seanlip commented Nov 15, 2017

Also adding notes from @Oishikatta's doc to the issue thread, for easy reference in the future:

Javascript

Currently in use: eslint 3.18.0. This version is behind the current (4.x) by one major release, but uses the same rule format. Rules written for this version will not require changes if updated to 4.x in the future.

Plugins already exist to cover some of the issues: https://www.npmjs.com/package/eslint-plugin-angular -- but may require eslint 4.x. It is possible to pick out specific rules and use them with the 3.x version.

Suggestion: eslint appears suitably extendable with rules/plugins that it can meet the requirements. Enable built-in rules, retrieve some from open source packages, and write others.

Specific issues:

Python

Currently in use: pylint. Supports adding AST and raw/stream based checkers.

Suggestion: Enable relevant built-in pylint checkers, and write others.

Specific issues:

  • Invalid / incorrectly-formatted type info in docstrings: Pylint can check docstrings in Google style, but may be difficult to integrate with existing codebase and Oppia practices. Will need to test before determining if it is possible to use. https://pylint.readthedocs.io/en/latest/technical_reference/extensions.html#id3
  • In docstrings, don't have a space after the initial """: We can check for empty lines after """ by enabling pylint.extensions.docstyle, but would need a custom checker for detecting just a space(s) after.

For both file types:
If checks are simple and fit with existing checks in the pre_commit_linter.py script (e.g. regexp), consider adding there first.

@1995YogeshSharma
Copy link
Contributor

@seanlip can I go ahead and implement custom lint checks in pre_commit_linter or does this still need for looking online available options?

@seanlip
Copy link
Member Author

seanlip commented Dec 22, 2017

I think some of these can be solved with online available options, and some can be solved by adding custom lint checks. I'd recommend handling the former before the latter, since that might save work (e.g. adding a single line to the eslint config is better than writing a new function in the pre_commit_linter.py file).

@apb7
Copy link
Contributor

apb7 commented Jan 1, 2018

Hello @seanlip! I am willing to work on this issue. Can you please tell me how should I go about this?
Thanks!

@seanlip
Copy link
Member Author

seanlip commented Jan 1, 2018

Hi @apb7! The instructions given above should be clear, so please follow them. Feel free to take just one of the checkboxes for your first PR.

In general, here's what to do:
(1) Pick an issue in the list above to tackle.
(2) Make that error in a file. Then run scripts/pre_commit_linter.py and see if it catches that error. If it does, great -- it means that error has somehow been fixed and we don't need to fix it anymore. If it doesn't, find a way to make the script catch it, so that scripts/pre_commit_linter.py fails with an error. (Look at the source code for that script to see what it does, and follow through on the references to various files like .eslintrc etc.)

In general I would advise picking an issue that's simple to solve for your first PR here -- it may be adding a new rule in .eslintrc or pylint, or doing some customization of an existing rule. But if you can't do that then you may need to write some kind of function that checks for these things manually. There are some examples of that in the linter script. The less code you write, the better, though.

@apb7
Copy link
Contributor

apb7 commented Jan 1, 2018

Hello @seanlip! For checking alphabetized imports in Python, we can use flake-8-import-order. This package will add 4 new flake8 warnings.
Thanks!

@apb7
Copy link
Contributor

apb7 commented Jan 2, 2018

@seanlip: For 'checking alphabetized imports' in Python, instead of flake-8-import-order we can use isort. To use it, we first need to install it using install_third_party.sh , create a .isort.cfg in the root directory and then implement the respective function in pre_commit_linter.py.
I have tested the above method and it works.

@seanlip
Copy link
Member Author

seanlip commented Jan 2, 2018

isort seems reasonable to me. Could you please submit a PR? Note that, by default, it shouldn't automatically mutate the code; it should just flag the errors when being run in the linter script. (Although it's fine if you want to use the auto-fix option to clean up existing code.)

Thanks!

@apb7
Copy link
Contributor

apb7 commented Jan 2, 2018

@seanlip: isort gives both the options - auto-fix as well as a check for import order. We can set it according to our requirements.
I'll submit a PR soon!
Thanks!

@seanlip
Copy link
Member Author

seanlip commented Mar 31, 2018

oppia.directive(...)?

@apb7
Copy link
Contributor

apb7 commented Mar 31, 2018

Yes, got it. Thanks!

@apb7
Copy link
Contributor

apb7 commented Apr 1, 2018

Hi @seanlip, I have implemented the scope check using pyjsparser. This parser creates an AST tree for JS files using esprima settings. There are a number of directives which do not have a scope. What should be done for them? Also, some have scope set to true. What needs to be done for them as well?
Thanks!

@seanlip
Copy link
Member Author

seanlip commented Apr 1, 2018 via email

@apb7
Copy link
Contributor

apb7 commented Apr 1, 2018

Okay, understood. Thanks!

@apb7
Copy link
Contributor

apb7 commented Apr 1, 2018

@seanlip: For directives not having any scope, I'm adding scope: false which is the default value of scope but I had a doubt. I think some directives having scope: true in extensions/objects/templates are not being used. For these, can we have scope: {}? Please correct me if I am going wrong somewhere.
Thanks!

@seanlip
Copy link
Member Author

seanlip commented Apr 2, 2018

By default all scopes should be scope: {}.

One thing to be careful about is that this might break scope inheritance, in which case we'll need to find some sort of workaround. In some cases that might be simple and in other cases that might be harder, so it may be worth sorting out the easy ones first and leaving the harder ones for further discussion or a separate PR.

@apb7
Copy link
Contributor

apb7 commented Apr 2, 2018

@seanlip: If any directive does not have an explicitly defined scope, then it is equivalent to scope: false (Please see this link). Therefore we should use this instead of scope: {} for such directives. Please correct me if I am going wrong somewhere. Also, I am planning to make an initial PR for such directives along with the checker (not enabled though) for a review. How does that sound?
Thanks!

@seanlip
Copy link
Member Author

seanlip commented Apr 2, 2018

Actually, what I want to introduce is an explicit isolate scope for all directives, rather than allowing them to inherit parent scope. Hence the default of scope: {}.

Initial PR sounds good, thanks!

@DubeySandeep
Copy link
Member

Hi @WickedBrat, I think @apb7 is working on it (As mentioned here #4592), if so then maybe you can take up another part of this issue else you can start working on this :).
(Let's wait for @apurv response)

@DubeySandeep
Copy link
Member

DubeySandeep commented Apr 10, 2018

@WickedBrat, I think apurv is working on "directives should have explicit isolated scope" so yeah you can work on "All directives should ave restrict: 'E'" (Also, you can take #4592, #4734 for reference)

@apb7
Copy link
Contributor

apb7 commented Apr 10, 2018

Hi @WickedBrat! You can go ahead with it. I've picked up scope checks for now. Also, I suggest you go through the review comments in PR #4592 once. It would surely help!
Thanks!
(Thank you @DubeySandeep :) )

@apb7
Copy link
Contributor

apb7 commented Apr 11, 2018

Hi @WickedBrat, please go through these comments in the above issue thread. Also, you don't need to write any check in pre-commit linter. You have to enable the directive-restrict rule in eslint-plugin-angular. Another important thing is to change the HTML attributes of the directives to elements in the HTML files where these directives are being used. This needs to be done without breaking any test (Also, test this manually as well). You can refer to PR #4592 and PR #4734 for the procedure. Thanks!

@apb7 apb7 mentioned this issue Apr 21, 2018
6 tasks
@apb7 apb7 self-assigned this May 15, 2018
kevinlee12 pushed a commit that referenced this issue May 20, 2018
* Basic parsing

* Integrate directive check

* Add directive name to msg

* Comment out directive scope messages

* Remove nesting

* Lint fixes

* Fix lint errors

* Add explanatory comments

* Remove trailing space

* Review changes(1)

* Review changes(2)

* Review changes(3)

* Disable check
@apb7 apb7 added devflow and removed backend labels Jun 16, 2018
@apb7 apb7 removed their assignment Jul 7, 2018
seanlip pushed a commit that referenced this issue Jul 9, 2018
…dependencies and controller function parameters (#5225)

* Add check and fix problems

* Change in messages
@apb7
Copy link
Contributor

apb7 commented Aug 9, 2018

This issue has been broken down into smaller sub-issues for the remaining parts. Therefore closing this. Thanks!

@apb7 apb7 closed this as completed Aug 9, 2018
@ghost ghost deleted a comment Jan 24, 2019
@ghost ghost deleted a comment Jan 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests