JSHint Tips
Updated pdexter 2024-01-30
Background
JSHint is a third party Javascript syntax checker. For details see https://jshint.com/docs/ .
JSHint has been introduced into Ruleset and Ruleset Include Editor templates as of Feb 2022. It has been configured to work as well as possible with the way Formbird script is written.
This was introduced to replace the old native syntax checking that came with a simple "eval" call on the script. While this worked, it only provided line numbers of errors in Firefox, leaving it nearly useless for other browsers.
Bypassing JSHint
While all efforts have been made to configure JSHint to only highlight real problem lines in Formbird script, there are times when it throws a "false" error (ie, a code discrepancy that we in fact live with).
To bypass on single line
To avoid an error on a single line, follow the code with the following
// jshint ignore:line
Example
var x = swal; // jshint ignore : line
To bypass a block of script
Define a block to bypass by /* jshint ignore:start */
and /* jshint ignore:end */
Example
// Code here will be checked with JSHint.
var x = 'hello world';
/* jshint ignore:start */
// Code here will be ignored by JSHint.
var y = 'nah nah can\'t catch me';
/* jshint ignore:end */
var z = 'hello again world';
Placing the start tag-comment /* jshint ignore:start */
at the beginning of a script, without setting an end tag-comment, will bypass checking for the whole script HIGHLY NOT RECOMMENDED.
Gotchas & how to avoid
Global objects "not defined"
Often using a global object or property will raise an error, eg
var x = location.href
// error: location not defined.
Generally in the client javascript context, a global object is a child of the global object "window", so the above could be replaced with
var x = window.location.href
In the server javascript context, global objects are childs of "global", eg var x = global.widgetService
; (server side global use unverified -- pdexter 2022-05-23).
Another solution is to declare the global variable/constant near the top of your script, eg
/* global MySuperObject */
Unreachable code after return
Often we use a return;
statement in a block of script in order to bypass the rest of the block. JSHint will complain about this.
Solution
a) Remove or comment the code following return
.
b) If you want to keep the following script for reference or later restoration, change the return
line to
if (true) return;