Ticket #2876 (new defect)

Opened 19 months ago

Last modified 3 months ago

ShrinkSafe v0.1.0 will break javascript with multiple var inside if-else

Reported by: guest Owned by: alex
Priority: normal Milestone: future
Component: ShrinkSafe Version:
Severity: normal Keywords: multiple variable definitions
Cc:

Description (last modified by dylan) (diff)

shrinksafe (the online version as of april 28,2007 v0.1.0) will break a script if it's got multiple variable definitions for the same variable within an if-else:

function test(bVariable) {
	if (bVariable) {
		var aVariable='ok';
	}else{
		var aVariable='not ok';
	}
	alert(aVariable);
}

in the example above, shrinksafe will not recognize that 'aVariable' is the same variable and will assign two different variable names:

function test(_1){if(_1){var _2="ok";}else{var _3="not ok";}alert(_3);}

Change History

Changed 18 months ago by sjmiles

  • priority changed from high to normal
  • owner changed from anonymous to alex
  • component changed from General to BuildTools

Fwiw, using using multiple var for one variable name inside a single function is not necessary and generally considered bad form.

I'll let Alex decide if this is 'wontfix' or 'fix', but you can solve your immediate problem by simply removing the extra 'var'. Javascript detects var declarations exclusive of any execution path.

I.e. if (false){var x=3;} will declare 'x' (but does not assign 3 to it).

Changed 14 months ago by peller

  • keywords shrinksafe removed
  • component changed from BuildTools to ShrinkSafe

Changed 12 months ago by dylan

  • milestone set to 1.1

This bug gets brought up pretty regularly on the mailing lists and forums. Here's the latest feedback:

I'm not sure where I'm supposed to report bugs re: dojo compressor, so I'll post this here. I've found either a bug in dojo compressor, or a very unfortunate feature.

Given the following: function test() {

if( true ) {

var foo = 1;

} else {

var foo = 2;

}

}

dojo compressor produces: function test(){ if(true){ var _1=1; }else{ var _2=2; } }

Thus, it has completely renamed the variable foo, so that the conditional is no longer setting the same variable. Since javascript does not have block-level scoping determined by curly braces the way perl does, this behavior will very easily break someone's code.

I suppose you can make the argument that I should be declaring variables at the top of my script, or not using the var keyword in the way in which i am, but nonetheless, the compressor took working javascript code and broke it. As a result of this behavior, I opted to use YUI Compressor on the same code and it successfully compressed it without breaking it.

I would love to see you take this advice to heart and make dojo compressor more closely align itself with how javascript scoping actually works, rather than align it with "best practices" or whatever the rationale for the current behavior is.

Thanks, Dan

Changed 9 months ago by dylan

  • milestone changed from 1.1 to 1.2

moving shrinksafe bugs to 1.2

Changed 3 months ago by dylan

  • description modified (diff)
  • milestone changed from 1.2 to future

moving shrinksafe bugs to future... help wanted.

Note: See TracTickets for help on using tickets.