jashkenas/coffee-script — Issue #1849: 1.1.3: generating empty var statement
Author: lookfirst
Created: 2011-11-10 01:35:03+00:00
State: open

json2Form = (@data, @form) ->
flattened = flattenData(@data)
for flat in flattened
@key = flat.key
@value = flat.value
@flds = $('[name^="' + @key + '"]', @form)
for fld in @flds
@jfld = $(fld) # convert to jquery
switch fld.type
when "textarea", "hidden", "text"
@jfld.val(@value).change()
when "select-one", "select-multiple"
$('select[name="' + fld.name + '"] option', @form).each (i, e) =>
e = $(e)
if e.val() == "#{@value}"
e.attr('selected', true)
else
e.removeAttr('selected')
e.change()
when "checkbox", "radio"
if @jfld.val() == "#{@value}"
@jfld.attr('checked', true)
else
@jfld.removeAttr('checked')
@jfld.change()

produces the code below. notice the empty "var ;" line.

This worked fine with 1.1.2.

json2Form = function(data, form) {
var flat, flattened, fld, _i, _len, _results;
this.data = data;
this.form = form;
flattened = flattenData(this.data);
_results = [];
for (_i = 0, _len = flattened.length; _i < _len; _i++) {
flat = flattened[_i];
this.key = flat.key;
this.value = flat.value;
this.flds = $('[name^="' + this.key + '"]', this.form);
_results.push((function() {
var _j, _len2, _ref, _results2;
var ;
_ref = this.flds;
_results2 = [];
for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) {
fld = _ref[_j];
this.jfld = $(fld);
switch (fld.type) {
case "textarea":
case "hidden":
case "text":
_results2.push(this.jfld.val(this.value).change());
break;
case "select-one":
case "select-multiple":
_results2.push($('select[name="' + fld.name + '"] option', this.form).each(function(i, e) {
e = $(e);
if (e.val() === ("" + _this.value)) {
e.attr('selected', true);
} else {
e.removeAttr('selected');
}
return e.change();
}));
break;
case "checkbox":
case "radio":
if (this.jfld.val() === ("" + this.value)) {
this.jfld.attr('checked', true);
} else {
this.jfld.removeAttr('checked');
}
_results2.push(this.jfld.change());
break;
default:
_results2.push(void 0);
}
}
return _results2;
}).call(this));
}
return _results;
};

I've cut the above down to this (invalid) code which is what generates the empty var ;...

json2Form = (@data) ->
for flat in flattened
@key = flat.key
for fld in @flds
switch fld.type
when ""
$().each () =>
e = boo

and the generated code....

json2Form = function(data) {
var flat, fld, _i, _len, _results;
this.data = data;
_results = [];
for (_i = 0, _len = flattened.length; _i < _len; _i++) {
flat = flattened[_i];
this.key = flat.key;
_results.push((function() {
var _j, _len2, _ref, _results2;
var ;
_ref = this.flds;
_results2 = [];
for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) {
fld = _ref[_j];
switch (fld.type) {
case "":
_results2.push($().each(function() {
var e;
return e = boo;
}));
break;
default:
_results2.push(void 0);
}
}
return _results2;
}).call(this));
}
return _results;
};

Note: this could be related to my other issue #1841 since it seems to be a case of nested for loops.

This is a pretty serious issue for me since my code is now completely broken.