Sam
Sam

Reputation: 15508

ShrinkSafe minify tool complains: debugging Javascript code via JSLint: style or real errors?

When I tried to minify my Javascript file via ShrinkSafe, an awesome minification tool, it output an empty file which means that my Javascript file apparently contains an error, though all works perfect in five browsers. So I tried JSLint, another awesome website and so found hundreds of style errors that I corrected most of them, but these I don't know to fix. Which are these are style hints and which could be the potential error left over?

Much appreciated for your style or error correcting hints to these issues.

JS LINT said that these are

# Errors
    
Problem at line 38 character 55: **Missing radix parameter.**

    if (settings.crossLinking && location.hash && parseInt(location.hash.slice(...

Problem at line 39 character 32: **Missing radix parameter**.

    var currentPanel = parseInt(location.hash.slice(1));

Problem at line 46 character 30: 'currentPanel' **is already defined**.

    var currentPanel = settings.firstPanelToLoad;

Problem at line 47 character 24: 'offset' **is already defined**.

    var offset = - (panelWidth*(currentPanel - 1));

Problem at line 53 character 30: 'currentPanel' **is already defined**.

    var currentPanel = 1;

Problem at line 108 character 33: **Missing radix parameter**.

    if (sliderCount === parseInt($(this).attr("rel").slice(12))) {

Problem at line 111 character 35: **Missing radix parameter**.

    targetPanel = parseInt($(this).attr("href").slice(1));

Problem at line 125 character 55: **Missing radix parameter**.

    if (settings.crossLinking && location.hash && parseInt(location.hash.slice(...

Problem at line 137 character 45: 'currentPanel' **used out of scope**.

    panelHeight = $('.panel:eq(' + (currentPanel - 1) + ')', slider).height();

Problem at line 150 character 73: **Expected** ';' **and instead saw** 'slider'.

    panelHeight = $('.panel:eq(' + x + ')', slider).height()

Problem at line 161 character 32: 'offset' **is already defined**.

    var offset = - (panelWidth*currentPanel);

Problem at line 168 character 69: 'offset' **used out of scope**.

    $('.panel-container', slider).animate({ marginLeft: offset }, settings.sl...

Upvotes: 0

Views: 713

Answers (2)

Amit Behere
Amit Behere

Reputation: 82

Missing radix parameter: The function parseInt takes 2 parameters. The 2nd is a radix, it is optional but JSLint throws an error. Pass in 2nd parameter as 10 (base 10) to get rid of this error. http://www.w3schools.com/jsref/jsref_parseInt.asp

var xyz is already defined: You need to defined a variable using var just once, for future assignments to the same variable (say currentPanel), omit the var

Make sure you have semi-colon at the end of each line (where it is required). A missing ; in my experience is the number one cause of why code breaks once it is minified (since all the line breaks are removed)

xyz out of scope: Means that variable has not been defined at that point in the code. Figuring this out will require a more thorough understanding of the code.

Upvotes: 2

Stephen Chung
Stephen Chung

Reputation: 14605

Missing radix -- definitely should fix; parseInt should never be used without a radix parameter, as it is too easy to introduce a bug (string starting with "0" being interpreted as octal).

Already defined -- sometimes it can be a bug because you are overwriting an earlier value (which you may need). Typical with temp variables, but safe if you know what you are doing and you really want to redefine a variable.

Used out of scope -- check if this is really what you want; it is a warning only.

Expected ';' etc. -- You are probably missing an ending ";" at the end of a statement. JavaScript will auto-insert it at the end of a line, but it is never a good idea to depend on this behavior, as it will silently insert ";" sometimes in the wrong place and makes you code impossible to debug.

Nothing I see looks like a definite syntax error. Shrinksafe should minify it fine. I suggest you check your Shrinksafe parameters to see if you are missing anything.

Also consider the Closure Compiler (Simple mode) which yields even tighter compression than Shrinksafe. The Closure Compiler is like JSLint + Shrinksafe wrapped up in one. Link to on-line web service: http://closure-compiler.appspot.com/home

Upvotes: 3

Related Questions