When java yuicompressor return error, ruby-yuicompressor just return empty file #18

Closed
evtuhovich opened this Issue May 12, 2011 · 2 comments

3 participants

@evtuhovich

It tooks me 2 hours to understand, why one of my js assets is empty after running jammit.

When some js file in asset incorrect (in term of java-yuicompressor), the result asset is empty file.

It be better, if some clear error message displayed instead of such behavior.

$(function () {

    if ( $.r('offer_form').length === 0 ) {
        return;
    }

    var newAddress = $('[role="new_address"]'),
        citySelect = $('[role="city_select"]', newAddress),
        metroList = $.parseJSON( citySelect.attr('data-metro') ),
        metroSelect = $('[role="metro_select"]', newAddress),
        addressInput = $('[role="street_input"]', newAddress),
        addressAndPhoneInputs = $('[role="street_input"], [role="phone_input"]', newAddress),
        submitNewAddress = $('[role="submit"]', newAddress),
        addressesList = $('[role="addresses_list"]'),
        addressesListUl = $('ul', addressesList),
        addressTemplate = $('[role="template"]', addressesList).html(),
        addressesField = $('[role="addresses_field"]', addressesList),
        currentAddressId = null,
        map = new YMaps.Map( $('[role="map"]', newAddress) ),
        mapIsLocked = false,
        timer, placemark, longitude, latitude;

    function addNewAddress (address) {
        if ( addressesList.is('.hide') ) {
            addressesList.removeClass('hide');
        }
        var newAddressLi = $(Handlebars.compile(addressTemplate)({
            city: getCityName(),
            metro_station: $('option[value="' + address.metro_station_id + '"]', metroSelect).text(),
            address: address.address,
            phone: address.phone
        }));

        newAddressLi.appendTo(addressesListUl).data( 'address_json', address );

        updateAddressesField();
    }

    function removeAddress (addressObj) {
        addressObj.fadeOut(200, function () {
            $(this).remove();
            updateAddressesField();

            if ( $('li', addressesListUl).length === 0 ) {
                addressesList.addClass('hide');
            }
        });
    }

    function editAddress (addressObj) {
        var address = addressObj.data('address_json');
        currentAddressId = address.id;
        mapIsLocked = true;
        citySelect.val(address.city_id).change();
        setTimeout(function () {
            metroSelect.val(address.metro_station_id);
        }, 0);
        addressInput.val(address.address);
        $('[role="phone_input"]', newAddress).val(address.phone);
        updateMap(address.latitude, address.longitude);
        removeAddress(addressObj);
        addressAndPhoneInputs.keyup();
    }

    function getCityName () {
        return(
            $('option[value="' + citySelect.val() + '"]', citySelect).text()
        );
    }

    function loadMetroStations () {
        var metroStations = metroList[ citySelect.val() ];

        if (typeof metroStations !== 'undefined') {

          metroSelect.html(
              metroStations.map(function (value) {
                  return(
                      '' +
                          value.metro_station.name +
                      ''
                  );
              }).join()
          ).removeAttr('disabled');

        } else {
            metroSelect.html('').attr('disabled', 'disabled');
        }
    }

    function updateAddressesField () {

        addressesField.val(
            JSON.stringify(
                $('li', addressesListUl).map(function () {
                    return(
                        $(this).data('address_json')
                    );
                }).toArray()
            )
        );
    }

    citySelect.change(function () {
        loadMetroStations();
        if (!mapIsLocked) {
            updateMap();
        }

    }).change();

    submitNewAddress.click(function (e) {

        if ( !$(this).is('.disabled') ) {

          function addressVal(role) {
            return( $('[role="' + role + '"]', newAddress).val() );
          }

          addNewAddress({
              id: currentAddressId,
              city_id: addressVal('city_select'),
              metro_station_id: addressVal('metro_select'),
              address: addressVal('street_input'),
              phone: addressVal('phone_input'),
              longitude: longitude,
              latitude: latitude
          });

          addressAndPhoneInputs.val('');

          mapIsLocked = false;
          updateMap();
          currentAddressId = null
        }

        e.preventDefault();
    });

    addressesListUl.delegate('[role="edit"]', 'click', function (e) {
        editAddress( $(this).parents('li:first') );
        e.preventDefault();
    });

    addressesListUl.delegate('[role="delete"]', 'click', function (e) {
        removeAddress( $(this).parents('li:first') );
        addressAndPhoneInputs.keyup();
        e.preventDefault();
    });

    timer = addressInput.delayedKeyup(function () {
        if (!mapIsLocked) {
            updateMap();
        }
    }, 800);

    $('[role="show_address"]', newAddress).click(function (e) {
        mapIsLocked = false;
        timer.now();
        e.preventDefault();
    });

    addressAndPhoneInputs.keyup(function () {
        if ( addressAndPhoneInputs.is('[value=""]') ) {
            submitNewAddress.addClass('disabled');
        } else {
            submitNewAddress.removeClass('disabled');
        }
    });

    $('li', addressesListUl).each(function () {
        $(this).data('address_json', JSON.parse( $(this).attr('data-address') ).place);
    });

    updateAddressesField();

    function updateMap (lat, long) {

        if (typeof lat !== 'undefined') {

            if (typeof placemark !== 'undefined') {
                map.removeOverlay(placemark);
            }

            placemark = new YMaps.Placemark( new YMaps.GeoPoint(long, lat), {draggable: true} );
            map.addOverlay(placemark);

            map.setCenter(new YMaps.GeoPoint(long, lat), 15);

            latitude = lat;
            longitude = long;

            // TODO: Refactor me!
            YMaps.Events.observe(placemark, placemark.Events.DragStart, function (obj) {
                mapIsLocked = true;
            });

            YMaps.Events.observe(placemark, placemark.Events.DragEnd, function (obj) {
                var newCoords = obj.getGeoPoint();
                longitude = newCoords.getLng();
                latitude = newCoords.getLat();
            });

        } else {

            var searchFor = getCityName(),
                isAddress = false;

            if ( addressInput.val() !== '' ) {
                searchFor += ', ' + addressInput.val();
                isAddress = true;
            }

            var geocoder = new YMaps.Geocoder(searchFor);

            YMaps.Events.observe(geocoder, geocoder.Events.Load, function (geocoder) {
                if ( geocoder.length() ) {

                    if (typeof placemark !== 'undefined') {
                        map.removeOverlay(placemark);
                    }

                    var point = this.get(0);

                    if (isAddress) {
                        placemark = new YMaps.Placemark( point.getGeoPoint(), { draggable: true });
                        map.addOverlay(placemark);

                        longitude = point.getCoordPoint().getLng();
                        latitude = point.getCoordPoint().getLat();

                        YMaps.Events.observe(placemark, placemark.Events.DragStart, function (obj) {
                            mapIsLocked = true;
                        });

                        YMaps.Events.observe(placemark, placemark.Events.DragEnd, function (obj) {
                            var newCoords = obj.getGeoPoint();
                            longitude = newCoords.getLng();
                            latitude = newCoords.getLat();
                        });
                    }

                    map.setBounds( point.getBounds() );
                }
            });
        }
    }

    map.addControl(
            new YMaps.TypeControl([YMaps.MapType.MAP,
                                   YMaps.MapType.SATELLITE,
                                   YMaps.MapType.HYBRID], [1, 2]));
    map.addControl( new YMaps.Zoom() );
});
@jhilden

I had a very similar problem after accidentally leaving a debugger; statement in my JS. All I saw was

gems/yui-compressor-0.9.6/lib/yui/compressor.rb:87:in `block in compress': compression failed (YUI::Compressor::RuntimeError)

I was able to figure it out by pasteing my JS here http://www.refresh-sf.com/yui/ which gave me this better error message:

[ERROR] x:xx:identifier is a reserved word

It would be very nice to pass the error messages along and to display them when using the gem. Thanks.

@stevecrozz
Collaborator

I've recently improved error reporting. When I try with your example file above I now get this:

irb(main):014:0> YUI::JavaScriptCompressor.new.compress(File.read("out.js"))

[ERROR] 177:34:missing formal parameter

[ERROR] 177:34:missing } after function body

[ERROR] 179:41:missing ; before statement

[ERROR] 181:51:missing ; before statement

[ERROR] 182:21:syntax error

[ERROR] 183:13:syntax error

[ERROR] 185:24:syntax error

[ERROR] 188:50:identifier is a reserved word

[ERROR] 191:29:identifier is a reserved word

[ERROR] 204:9:syntax error

[ERROR] 246:9:syntax error

[ERROR] 254:1:syntax error

[ERROR] 1:0:Compilation produced 12 syntax errors.
org.mozilla.javascript.EvaluatorException: Compilation produced 12 syntax errors.
    at com.yahoo.platform.yui.compressor.YUICompressor$1.runtimeError(YUICompressor.java:154)
    at org.mozilla.javascript.Parser.parse(Parser.java:392)
    at org.mozilla.javascript.Parser.parse(Parser.java:337)
    at com.yahoo.platform.yui.compressor.JavaScriptCompressor.parse(JavaScriptCompressor.java:312)
    at com.yahoo.platform.yui.compressor.JavaScriptCompressor.<init>(JavaScriptCompressor.java:533)
    at com.yahoo.platform.yui.compressor.YUICompressor.main(YUICompressor.java:131)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.yahoo.platform.yui.compressor.Bootstrap.main(Bootstrap.java:21)
YUI::Compressor::RuntimeError: Command 'java -jar /home/stevecrozz/Private/Projects/ruby-yui-compressor/lib/yui/../yuicompressor-2.4.7.jar --type js --charset utf-8 --nomunge /tmp/yui_compress20130714-28197-x1b082' returned non-zero exit status
    from /home/stevecrozz/Private/Projects/ruby-yui-compressor/lib/yui/compressor.rb:106:in `block in compress'
    from /home/stevecrozz/Private/Projects/ruby-yui-compressor/lib/yui/compressor.rb:141:in `streamify'
    from /home/stevecrozz/Private/Projects/ruby-yui-compressor/lib/yui/compressor.rb:86:in `compress'
    from (irb):14
    from /home/stevecrozz/.rbenv/versions/1.9.3-p194/bin/irb:12:in `<main>'
@stevecrozz stevecrozz closed this Jul 15, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment