Recent Changes - Search:

edit SideBar

JSLint

See Also

Overview

http://www.jslint.com/ is a static analyzer for .js files.

JSLint is very picky, but it is worth understanding why it complains about various issues.

Sample Run

See node-jslint below for how to install

bash-3.2$ vendors/node-jslint/bin/jslint.js ptolemy/actor/lib/jjs/basicFunctions.js

ptolemy/actor/lib/jjs/basicFunctions.js
 #1 Unexpected dangling '_' in '_debug'.
    var _debug = false; // Line 47, Pos 5
 #2 Missing 'use strict' statement.
    var MessageHandler = Java.type('ptolemy.util.MessageHandler'); // Line 53, Pos 5
 #3 Missing 'use strict' statement.
    actor.clearTimeout(handle); // Line 62, Pos 5
 #4 Missing 'use strict' statement.
    actor.clearTimeout(handle); // Line 70, Pos 5
 #5 Missing 'use strict' statement.
    throw message; // Line 78, Pos 5
 #6 Missing 'use strict' statement.
    if (_debug) { // Line 94, Pos 5
 #7 Unexpected dangling '_' in '_debug'.
    if (_debug) { // Line 94, Pos 9
 #8 Expected exactly one space between 'function' and '('.
    console.log("httpRequest(" + (function(obj) { // Line 95, Pos 47
 #9 'result' was used before it was defined.
    result = []; // Line 96, Pos 13
#10 'p' was used before it was defined.
    for (p in obj) { // Line 97, Pos 18
#11 Cannot read property 'kind' of undefined
     // Line 97, Pos 20
bash-3.2$

JSLint Warnings.

Running code through JSLint is rather depressing.

https://jslinterrors.com/ helps explain warnings.

Common Changes for JSLint

We are using JSLint because it is a common tool that other people will use on our code.

  • If you place this code below the main comment, then you will avoid many JSLint warnings.
"use strict";
/*global addInputHandler, getParameter, input, output, parameter, removeInputHandler, send */
  • Use function ( not function(
  • Add trailing semicolons (Eclipse warns about this).
  • Fix the indentation to match JSLint's desires.

JSLint and Eclipse complain about "for each"

JSLint fails to handle for each:

#11 Expected '(' and instead saw 'each'.
for each (sample in data) { // Line 48, Pos 9

The Problems tab in the JavaScript Perspective of Eclipse Luna also complains:

Description Resource Path Location Type
Syntax error on token "each", delete this token AudioCapture.js /ptII/org/terraswarm/accessor/accessors/web line 48 JavaScript Problem

https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions suggests using "for of"

However, using "for of" in localFunctions.js fails in Ptolemy II:

ptolemy.kernel.util.IllegalActionException: Failed to load localFunctions.js
  in .JavaScriptRecordToken.JavaScript
Because:
<eval>:437:12 Expected ( but found of
        for of (label in value.labelSet()) {
            ^ in <eval> at line number 437 at column number 12
        at ptolemy.actor.lib.jjs.JavaScript._createEngineAndEvaluateSetup(JavaScript.java:1711)

node-jslint

It is possible to use npm install node-jslint to install node-jslint, but I could not get the command line version working, so I downloaded the git repo instead.

  1. Install:
    cd $PTII/vendors
    git clone https://github.com/reid/node-jslint.git
    cd node-jslint
    make
  2. Run JSLint on a file:
    bash-3.2$ vendors/node-jslint/bin/jslint.js ptolemy/actor/lib/jjs/basicFunctions.js

    ptolemy/actor/lib/jjs/basicFunctions.js
     #1 Unexpected dangling '_' in '_debug'.
        var _debug = false; // Line 8, Pos 5
     #2 Missing 'use strict' statement.
        var MessageHandler = Java.type('ptolemy.util.MessageHandler'); // Line 13, Pos 5
     #3 'Java' was used before it was defined.
        var MessageHandler = Java.type('ptolemy.util.MessageHandler'); // Line 13, Pos 26

    ...

node-jslint Problems

Cannot find module 'readable-stream'

bash-3.2$ vendors/node-jslint/bin/jslint.js ptolemy/actor/lib/jjs/basicFunctions.js
module.js:338
    throw err;
          ^
Error: Cannot find module 'readable-stream'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/cxh/ptII/vendors/node-jslint/lib/stream.js:1:80)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
bash-3.2$

Solution: Run make

Jenkins

Jenkins Undefined Errors

"You'll probably find that if you use library "foo" you get a lot of "undefined foo" errors at first. Just put a globals statement at the top of your JavaScript file so jslint knows that "foo" is a global:"

/*globals foo, bar*/
var baz = function() {
    foo.doSomethingWith(bar);
};

"Please note: you must NOT have a space between "*" and "globals", otherwise it won't work"

Another possibility is to invoke JSLint with -Dpredef=foo,bar,baz

The Jenkins configuration states: "This would be like having /*global foo,bar,baz*/ at the top of every JavaScript file JSLint runs on."

So, we use -Dpredef=Java,actor in the Jenkins JSLint configuration.

Jenkins Problems

Jenkins output is not very descriptive

The Jenkins output does not include as much detail as running from the command line. The Jenkins output includes:

  No description available. Please upgrade to latest checkstyle version.

  • Back to JS
Edit - History - Print - Recent Changes - Search
Page last modified on October 30, 2015, at 11:30 pm