After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 699967 - Linting via JSHint
Linting via JSHint
Status: RESOLVED FIXED
Product: gnome-maps
Classification: Applications
Component: general
git master
Other Linux
: Normal enhancement
: ---
Assigned To: gnome-maps-maint
gnome-maps-maint
Depends on:
Blocks:
 
 
Reported: 2013-05-08 23:18 UTC by Mattias Bengtsson
Modified: 2014-10-29 12:19 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Cleanup: Remove unused imports (8.17 KB, patch)
2014-10-16 03:17 UTC, Mattias Bengtsson
committed Details | Review
JSHint: Allow multi line strings (1.25 KB, patch)
2014-10-16 03:17 UTC, Mattias Bengtsson
committed Details | Review
Cleanup: Remove type coercion compares (1.72 KB, patch)
2014-10-16 03:17 UTC, Mattias Bengtsson
committed Details | Review
Cleanup: Add some missing semicolons (1.66 KB, patch)
2014-10-16 03:17 UTC, Mattias Bengtsson
committed Details | Review
JSHint: Allow late function definitions (1.50 KB, patch)
2014-10-16 03:17 UTC, Mattias Bengtsson
committed Details | Review
Sidebar: Reorder definitions (2.60 KB, patch)
2014-10-16 03:18 UTC, Mattias Bengtsson
committed Details | Review
JSHint: Allow unsafe lineendings (1.36 KB, patch)
2014-10-16 03:18 UTC, Mattias Bengtsson
committed Details | Review
HTTP: Supress loopfunc warning for now (1.04 KB, patch)
2014-10-16 03:18 UTC, Mattias Bengtsson
reviewed Details | Review

Description Mattias Bengtsson 2013-05-08 23:18:11 UTC
It would be nice to have linting for our JavaScript code. JSHint (http://www.jshint.com/) is a good and widely used tool for that. It depends on Node.js though which might or might not be ok.
Comment 1 Mattias Bengtsson 2013-05-09 02:04:27 UTC
Currently working on this in wip/jshint
Comment 2 Jonas Danielsson 2014-03-17 12:06:55 UTC
Anything new on this?
Comment 3 Mattias Bengtsson 2014-03-17 15:02:55 UTC
Well, the jshint-file is there (.jshintrc in the root) but I don't think anyone is using it (including me). 
Maybe we should add it to the test suite and ask people to run the suite before putting patches on bz. Unfortunately we don't have fancy integration of the test suite with the patch review so we'll have to remember to ask if the patches has been run. 

Keep this open and I'll cook up a patch soonish to fix the errors from jshint and then we'll start using it for real.
Comment 4 Mattias Bengtsson 2014-10-16 03:17:29 UTC
Created attachment 288625 [details] [review]
Cleanup: Remove unused imports

We import a bunch of modules we don't use in a lot of files. Remove all
such imports.
Comment 5 Mattias Bengtsson 2014-10-16 03:17:36 UTC
Created attachment 288626 [details] [review]
JSHint: Allow multi line strings

JSHint defaults to not allow multi line strings, probably because of
buggy browser implementations.

This shouldn't affect us, and is handy when doing dbus calls so allow
this.
Comment 6 Mattias Bengtsson 2014-10-16 03:17:42 UTC
Created attachment 288627 [details] [review]
Cleanup: Remove type coercion compares

The '==' and '!=' operators in JS is a bit weird since it tries to do
automatic and sometimes unexpected type coercions instead of a strict
comparison.

In Maps we have decided to use the '===' and '!==' operators instead
since they do stricter checks.

This patch converts two cases where '!=' passed through review.
Comment 7 Mattias Bengtsson 2014-10-16 03:17:48 UTC
Created attachment 288628 [details] [review]
Cleanup: Add some missing semicolons

Semi-colons aren't strictly necessary in JavaScript, but not using them
can sometimes bite you, so we have decided to use semi-colons throughout
our code base.

Fix three cases where semi-colons were missing.
Comment 8 Mattias Bengtsson 2014-10-16 03:17:57 UTC
Created attachment 288629 [details] [review]
JSHint: Allow late function definitions

By default JSHint warns when you use functions before they are defined.
That's not really necessary, and it's sometimes convenient to define
functions in "backwards" order.

This patch makes JSHint only complain about using variables before they
are declared, not functions.
Comment 9 Mattias Bengtsson 2014-10-16 03:18:06 UTC
Created attachment 288630 [details] [review]
Sidebar: Reorder definitions

Define InstructionRow before Sidebar instead of after it. Fixing a
JSHint error about usages before definitions.
Comment 10 Mattias Bengtsson 2014-10-16 03:18:12 UTC
Created attachment 288631 [details] [review]
JSHint: Allow unsafe lineendings

This is meant as a way to protect oneself from beeing bitten by
JavaScripts Automatic Semicolon Insertion.

Since this is already covered by the "asi" option in JSHint and it
warns on existing code with operators first we'll relax this check.
Comment 11 Mattias Bengtsson 2014-10-16 03:18:20 UTC
Created attachment 288632 [details] [review]
HTTP: Supress loopfunc warning for now

JSHint warns when you create functions inside loops since closures in
combination with function scoping can be very unintuitive.

We should probably keep that behaviour but that means we get a warning
in the http module (for entirely safe code), so supress that warning.
Comment 12 Mattias Bengtsson 2014-10-16 03:20:25 UTC
These patches should fix all warnings from JSHint.

Regarding how and when to run this, I think the simplest is just to document somewhere on how to install jshint, since then it's just a matter of running:
  
  $ jshint src/*.js

from the root folder of gnome-maps.
Comment 13 Mattias Bengtsson 2014-10-16 03:34:22 UTC
A quick guide on how to install jshint (so that whoever reviews this can test):

Fedora specific:

  $ pkcon install npm

Ubuntu specific:
  
  $ sudo apt-get install npm nodejs-legacy

Add ~/.local/bin/ to the path if it isn't already

  $ echo "export PATH=$HOME/.local/bin:\$PATH" >> ~/.bashrc

Set up node.js to use ~/.local/:

  $ echo "prefix = ${HOME}/.local" >> ~/.npmrc
  $ echo "export NODE_PATH=$HOME/.local/lib/node_modules/:\$NODE_PATH" >> ~/.bashrc
  $ source ~/.bashrc

Install jshint

  $ npm install -g jshint
Comment 14 Jonas Danielsson 2014-10-16 04:40:55 UTC
Review of attachment 288625 [details] [review]:

That's a lot of imports, Batman!
Comment 15 Jonas Danielsson 2014-10-16 04:41:17 UTC
Review of attachment 288626 [details] [review]:

Ack
Comment 16 Jonas Danielsson 2014-10-16 04:42:17 UTC
Review of attachment 288627 [details] [review]:

I am surprised we didn't have more of these, actually. Way to go us!

For reference: http://www.sitepoint.com/javascript-truthy-falsy/
Comment 17 Jonas Danielsson 2014-10-16 04:42:39 UTC
Review of attachment 288628 [details] [review]:

Okay!
Comment 18 Jonas Danielsson 2014-10-16 04:43:20 UTC
Review of attachment 288629 [details] [review]:

Sure.

In the future, feel free to commit stuff like this (tuning of .jsinitrc) without going through bz.
Comment 19 Jonas Danielsson 2014-10-16 04:43:35 UTC
Review of attachment 288629 [details] [review]:

Noop
Comment 20 Jonas Danielsson 2014-10-16 04:44:14 UTC
Review of attachment 288630 [details] [review]:

Hmm, is this the only case of this in the code base?
Comment 21 Jonas Danielsson 2014-10-16 04:44:34 UTC
Review of attachment 288631 [details] [review]:

Ok!
Comment 22 Jonas Danielsson 2014-10-16 04:45:36 UTC
Review of attachment 288632 [details] [review]:

Not sure about this. Are we likely to get more of comments like this? I don't want to see them all over the place.
Is this a super special case?
Comment 23 Mattias Bengtsson 2014-10-16 05:24:36 UTC
Review of attachment 288630 [details] [review]:

According to jshint: yes.

We access functions before they are defined a lot but that's fine so long as they are defined like:
   function f() { }
instead of
   let f = function() {}
because the JS implementation will find them before running any other code in the file I believe.
Comment 24 Mattias Bengtsson 2014-10-16 05:27:37 UTC
Review of attachment 288632 [details] [review]:

I understand that, and felt a bit unsure about this. 

Thing is I plan on making a patch converting all forEach to for..of-loops after this[1] so didn't want to spend time to refactor this.

1: gnome-shell has been using for..of-loops for a while now so I think it's safe for us to move over as well.
Comment 25 Mattias Bengtsson 2014-10-16 05:31:00 UTC
Attachment 288625 [details] pushed as 18aca41 - Cleanup: Remove unused imports
Attachment 288626 [details] pushed as 57369a3 - JSHint: Allow multi line strings
Attachment 288627 [details] pushed as f343b9b - Cleanup: Remove type coercion compares
Attachment 288628 [details] pushed as e0daa16 - Cleanup: Add some missing semicolons
Attachment 288629 [details] pushed as c96417c - JSHint: Allow late function definitions
Attachment 288631 [details] pushed as be4e84d - JSHint: Allow unsafe lineendings
Comment 26 Mattias Bengtsson 2014-10-22 23:30:06 UTC
Added the guide above to the wiki[1].

Would be nice to get the other changes pushed so that JSHint produces 0 warnings.

1: https://wiki.gnome.org/Apps/Maps/JSHint
Comment 27 Jonas Danielsson 2014-10-29 11:54:57 UTC
Review of attachment 288630 [details] [review]:

Allright.
Comment 28 Jonas Danielsson 2014-10-29 11:56:09 UTC
Review of attachment 288632 [details] [review]:

So this will not be needed after converting to for...of-loops? Then do we really need to add this now?
Comment 29 Mattias Bengtsson 2014-10-29 11:57:53 UTC
Review of attachment 288632 [details] [review]:

No, we can definitely live with one warning for now I think. So let's skip this one, push the other and close this. :)
Comment 30 Mattias Bengtsson 2014-10-29 12:18:58 UTC
Attachment 288630 [details] pushed as 71f8ea6 - Sidebar: Reorder definitions