Finding and Fixing JavaScript errors with JSHint
The JavaScript Coding Standards have been updated, so it’s time to move on to tackling our JSHint errors!
JSHint is a tool to check for errors in JavaScript code. As was discussed last week, we’re kicking off a small effort to work through our core JavaScript files. To get through the errors revealed by JSHint as quickly as possible, we’re following the model established by the Inline Docs team and posting a list of files with issues so that people can “claim” the files they’d like to fix!
At the bottom is a list of every file in core that is displaying JSHint errors. Files with a checkmark have been patched and should now be passing lint. Files marked with (@username #xxxxx) are already claimed, and being worked on.
Please read and understand the process we’ll be following to address these issues! Many thanks to @azaozz, @nacin and @jorbin for helping identify the safest way to approach fixing these errors, and to @rzen for posting the Inline Docs article on which we based this guide.
How to contribute:
- Leave a comment on this post with the file* you’re about to edit (check the list first to make sure it hasn’t already been claimed).
- Update your local WordPress SVN to the latest version of WordPress trunk (currently 3.8-alpha).
- Create a new ticket on Trac for the file.
- Format the title as “jshint shouldn’t throw errors – path/to/file.js”.
- Assign the ticket to the “Build Tools” component.
- Make sure your email is stored in Trac’s preferences
If you are logged in, you can click this link to automatically open a ticket with the right settings.
- Edit the file, and make a patch. Please make sure you create the patch from the root directory of your WordPress SVN checkout. If you are working on a large file, consider making multiple patches for each type of change.
- Upload your patch to the Trac ticket you created, and add the keyword “has-patch”.
*Note: We strongly encourage you to work on one file at a time. These shouldn’t take very long, but if you call a bunch at once and get tied up, we won’t be able to get through these as quickly as possible. To quote @rzen from the inline docs effort, “your edits should be made and patched swiftly so that they aren’t invalidated by (or don’t invalidate) another patch.”
Keeping Discussions Focused:
Any discussion about the specifics of a patch itself should happen on Trac. Discussion about the overall effort should take place during our standing weekly meeting, on Wednesdays at 1900 UTC in #wordpress-dev*.
Files needing patches:
Checked files are now passing JSHint
See all open tickets in the Build Tools component
For tips on dealing with global variables, inlined third-party code within first-party scripts, etc, see the JSHint tips in the JavaScript Coding Standards
For the curious, this list was created with a jazzy little command @nacin came up with to pipe Grunt output through ack:
grunt jshint --force | ack '^Linting src/' | ack -o 'wp-.*.js' | sort | uniq -c | sort
What we’re NOT doing
The two JSHint options called out in the earlier post, “curly” and “eqeqeq,” would ordinarily make up the vast majority of the errors JSHint reports in our files. We’ve currently set Grunt and JSHint to ignore these two types of errors when JSHint is run against core. While these are best practices, we’ll come back to them once we address the more significant code smell issues like undefined variables.
Also note that we’re not tackling whitespace or non-JSHint-related refactoring during this effort. We’ll get there, but we have to mitigate the risk to core as much as possible so we don’t interrupt the 3.8 cycle. Keep your changes focused on passing JSHint this go-around.
K.Adam White 8:03 pm on November 13, 2013 Permalink | Log in to Reply
VERY IMPORTANT thing that I forgot: If you want to run JSHint against only the file you’ve claimed, you can target it with Grunt using the following command:
grunt jshint:core --file=filename.js
Tom McFarlin 9:27 pm on November 15, 2013 Permalink | Log in to Reply
If you’re up for modifying the Gruntfile (which is handy if you’re responsible for a number of files and so long as you don’t actually commit the changes :), you can add the following under jsint:
mytask: {
expand: true,
cwd: SOURCE_DIR,
src: [ 'wp-admin/js/dashboard.js', 'wp-admin/js/media-gallery.js', 'wp-admin/js/theme-preview.js','wp-includes/js/shortcode.js' ]
}
Then you can run:
grunt jshint:mytask
(and include--force
if needed)K.Adam White 9:32 pm on November 15, 2013 Permalink | Log in to Reply
You could also just run
grunt jshint:core --file=dashboard.js; grunt jshint:core --file=shortcode.js;
, etcetera.Tom McFarlin 8:12 pm on November 13, 2013 Permalink | Log in to Reply
I’ll claim the following (opening tickets shortly):
Tom McFarlin 9:29 pm on November 15, 2013 Permalink | Log in to Reply
These all have patches committed:
Doug Wollison 8:15 pm on November 13, 2013 Permalink | Log in to Reply
I’ll take wp-admin/js/editor.js for now.
Doug Wollison 8:41 pm on November 13, 2013 Permalink | Log in to Reply
Done: #25947
Also, any idea why I can’t actually logout of Trac? I accidently logged in under an old account, and when I log out and log back in I don’t get the popup anymore, it just shows me logs me in automatically.
Anton Timmermans 3:49 pm on November 14, 2013 Permalink | Log in to Reply
I think you are logged in on the codex, when you press login in trac it automatically logs you win with the same account as you are logged in in the codex. I think.
Andrew Nacin 5:58 am on November 15, 2013 Permalink | Log in to Reply
That’s your browser’s way of caching HTTP authentication. Fun times.
K.Adam White 8:18 pm on November 13, 2013 Permalink | Log in to Reply
I’ll finish the admin bar, then plan to take wp-includes/js/media-models.js and wp-includes/js/media-views.js
adamsilverstein 8:48 pm on November 13, 2013 Permalink | Log in to Reply
i will tackle wp-admin/js/revisions.js to start
Anton Timmermans 9:14 pm on November 13, 2013 Permalink | Log in to Reply
I’ll do wp-admin/js/nav-menu.js to start.
Zack Tollman 9:17 pm on November 13, 2013 Permalink | Log in to Reply
I’ll start with the two Twenty Fourteen JS files
Aaron Jorbin 9:21 pm on November 13, 2013 Permalink | Log in to Reply
There is a separate Grunt task for those. Grunt jshint:themes
Zack Tollman 9:22 pm on November 13, 2013 Permalink | Log in to Reply
Thanks!
Aaron Jorbin 10:13 pm on November 13, 2013 Permalink | Log in to Reply
Also, from when I’ve done some of these, @lancewillett suggested I didn’t need to do separate tickets and diffs. I also tagged them bundled theme instead of build tools to make sure the twentyfourteen team saw them.
OriginalEXE 9:43 pm on November 13, 2013 Permalink | Log in to Reply
Claiming
wp-includes/js/wp-ajax-response.js
OriginalEXE 10:10 pm on November 13, 2013 Permalink | Log in to Reply
Done: #25954 https://core.trac.wordpress.org/ticket/25954
programmin 10:10 pm on November 13, 2013 Permalink | Log in to Reply
Just curious, are you all going to combine var statements as suggested by JSLint? See http://en.wikipedia.org/wiki/JSLint#Controversy
Aaron Jorbin 10:15 pm on November 13, 2013 Permalink | Log in to Reply
You can read about our standards related to var statements at http://make.wordpress.org/core/handbook/coding-standards/javascript/#assignments-and-globals
adamsilverstein 11:07 pm on November 13, 2013 Permalink | Log in to Reply
i’ll take wp-includes/js/utils.js
adamsilverstein 2:41 am on November 14, 2013 Permalink | Log in to Reply
I’ll take wp-admin/js/edit-comments.js – 51 errors!
dziudek 8:44 am on November 14, 2013 Permalink | Log in to Reply
I’ll take wp-includes/js/heartbeat.js
dziudek 1:15 pm on November 14, 2013 Permalink | Log in to Reply
Done: http://core.trac.wordpress.org/ticket/25986
Anton Timmermans 11:00 am on November 14, 2013 Permalink | Log in to Reply
Could you update nav-menus.js?
Ticket: http://core.trac.wordpress.org/ticket/25953
Anton Timmermans 12:54 pm on November 14, 2013 Permalink | Log in to Reply
I’m doing the following files:
wp-admin/js/password-strength-meter.js
wp-admin/js/plugin-install.js
wp-admin/js/post.js
wp-admin/js/postbox.js
wp-admin/js/revisions.js
wp-admin/js/set-post-thumbnail.js
wp-admin/js/tags.js
I’m going to start right now.
Anton Timmermans 6:41 pm on November 14, 2013 Permalink | Log in to Reply
All done except wp-admin/js/revisions.js, because that was already done.
OriginalEXE 2:22 pm on November 14, 2013 Permalink | Log in to Reply
Claiming
wp-admin/js/xfn.js
OriginalEXE 2:35 pm on November 14, 2013 Permalink | Log in to Reply
Done #25997
Brandon Kraft 3:29 pm on November 14, 2013 Permalink | Log in to Reply
I’ll take wp-includes/js/wp-util.js as a test of me figuring out jshint. More to come.
Brandon Kraft 6:33 am on November 15, 2013 Permalink | Log in to Reply
Never mind. Already handled via r26207
Doug Wollison 3:40 pm on November 14, 2013 Permalink | Log in to Reply
I’ll take wp-admin/js/gallery.js while my last one get’s approved.
Doug Wollison 4:04 pm on November 14, 2013 Permalink | Log in to Reply
#25999
Doug Wollison 4:09 pm on November 14, 2013 Permalink | Log in to Reply
You know, I’ll just take these 3 while I’m at it:
wp-admin/js/image-edit.js #26000
wp-admin/js/inline-edit-post.js #26001
wp-admin/js/inline-edit-tax.js #26002
Matthew Denton 5:49 pm on November 14, 2013 Permalink | Log in to Reply
Hi, I’m going to pick up wp-includes/js/wp-auth-check.js
Have the changes made will create the trac entry later this evening with the patch.
Matthew Denton 6:32 pm on November 14, 2013 Permalink | Log in to Reply
#26011
Matthew Denton 5:55 pm on November 14, 2013 Permalink | Log in to Reply
Also going to pick up wp-includes/js/wp-lists.js
Matthew Denton 6:42 pm on November 14, 2013 Permalink | Log in to Reply
#26012
seanchayes 6:53 pm on November 14, 2013 Permalink | Log in to Reply
I’ll take wp-includes/js/mediaelement/wp-mediaelement.js
seanchayes 6:55 pm on November 14, 2013 Permalink | Log in to Reply
Oops – wrong file. Apologies.
This is the file I’d like to take:
wp-includes/js/jquery/jquery.table-hotkeys.js
seanchayes 7:20 pm on November 14, 2013 Permalink | Log in to Reply
#26015
Matthew Denton 6:57 pm on November 14, 2013 Permalink | Log in to Reply
wp-includes/js/wp-pointer.js is reviewed and diff available #26013
Matthew Denton 6:58 pm on November 14, 2013 Permalink | Log in to Reply
Picking up wp-includes/js/tinymce/mark_loaded_src.js for review
Matthew Denton 7:05 pm on November 14, 2013 Permalink | Log in to Reply
#26014
Fun side-bar activity, will pick some more up later tonight
Doug Wollison 7:25 pm on November 14, 2013 Permalink | Log in to Reply
I should be working on finishing my plugin, but…
wp-admin/js/user-profile.js #26016
wp-admin/js/user-suggest.js #26017
wp-admin/js/word-count.js #26018
seanchayes 7:26 pm on November 14, 2013 Permalink | Log in to Reply
Working on : wp-includes/js/customize-preview.js
seanchayes 7:39 pm on November 14, 2013 Permalink | Log in to Reply
#26019
seanchayes 7:40 pm on November 14, 2013 Permalink | Log in to Reply
Working on : wp-admin/js/media.js
seanchayes 7:52 pm on November 14, 2013 Permalink | Log in to Reply
#26020
seanchayes 7:53 pm on November 14, 2013 Permalink | Log in to Reply
Working on : wp-includes/js/media-editor.js
seanchayes 8:25 pm on November 14, 2013 Permalink | Log in to Reply
#26022
iblamefish 7:57 pm on November 14, 2013 Permalink | Log in to Reply
Just about to start : src/wp-admin/js/media-upload.js
iblamefish 8:14 pm on November 14, 2013 Permalink | Log in to Reply
Ticket: https://core.trac.wordpress.org/ticket/26023
seanchayes 8:26 pm on November 14, 2013 Permalink | Log in to Reply
Working on wp-includes/js/tinymce/plugins/wplink/editor_plugin_src.js
seanchayes 8:29 pm on November 14, 2013 Permalink | Log in to Reply
#26024
seanchayes 8:59 pm on November 14, 2013 Permalink | Log in to Reply
Working on : wp-includes/js/tinymce/plugins/wpview/editor_plugin_src.js
seanchayes 9:01 pm on November 14, 2013 Permalink | Log in to Reply
#26027
Doug Wollison 9:33 pm on November 14, 2013 Permalink | Log in to Reply
Let’s see if I can get these done by 5:
wp-admin/js/wp-fullscreen.js #26029
wp-admin/js/xfn.js #26030
wp-content/themes/twentyfourteen/js/functions.js #26031
wp-content/themes/twentyfourteen/js/slider.js #26032
seanchayes 10:05 pm on November 14, 2013 Permalink | Log in to Reply
Working on : wp-admin/js/link.js
seanchayes 10:09 pm on November 14, 2013 Permalink | Log in to Reply
#26034
seanchayes 10:19 pm on November 14, 2013 Permalink | Log in to Reply
Working on : wp-includes/js/autosave.js
seanchayes 10:36 pm on November 14, 2013 Permalink | Log in to Reply
#26035
Matthew Denton 2:38 am on November 15, 2013 Permalink | Log in to Reply
patch committed on wp-includes/js/comment-reply.js
#26038
Matthew Denton 2:57 am on November 15, 2013 Permalink | Log in to Reply
patch committed on wp-includes/js/customize-base.js #26039
Matthew Denton 3:07 am on November 15, 2013 Permalink | Log in to Reply
patch committed on wp-includes/js/customize-loader.js #26040
Matthew Denton 3:19 am on November 15, 2013 Permalink | Log in to Reply
working on : wp-includes/js/plupload/handlers.js #26041
Aaron Jorbin 3:55 am on November 15, 2013 Permalink | Log in to Reply
I’ve updated the list to include all closed tickets and links to tickets for all files that have them.
Great job thus far everyone!
Anton Timmermans 9:20 am on November 15, 2013 Permalink | Log in to Reply
I’ll do wp-includes/js/plupload/wp-plupload.js (18 errors) and wp-includes/js/quicktags.js (47 errors) next.
Konstantin Kovshenin 9:40 am on November 15, 2013 Permalink | Log in to Reply
Will take a stab at wp-admin/js/theme.js
Konstantin Kovshenin 10:05 am on November 15, 2013 Permalink | Log in to Reply
I don’t see any errors in theme.js, taking wp-admin/js/theme-install.js instead.
Konstantin Kovshenin 10:31 am on November 15, 2013 Permalink | Log in to Reply
#26045 for theme-install.js, btw I think most of it will be ruled out by THX if it goes multisite.
Konstantin Kovshenin 11:11 am on November 15, 2013 Permalink | Log in to Reply
wp-includes/js/quicktags.js in #26046
Zack Tollman 1:53 pm on November 16, 2013 Permalink | Log in to Reply
Looks like @dougwollison beat me to the Twenty Fourteen files. I did confirm that they pass the linting though. The other 4 remaining tickets all have patches as well.