Uglify无法为嵌套的if-else工作

时间:2016-06-23 08:44:41

标签: javascript gruntjs uglifyjs grunt-contrib-uglify

我正在使用https://maps.gstatic.com/mapfiles/transparent.png来验证我的代码,虽然我正在使用grunt-uglify来做同样但同样的错误,它可以很好地缩小代码但是当我评估这段代码时它会返回错误expected :。因为它不适用于嵌套的if-else案例。

示例代码。

$._ext_ILST = {
    changeColorLabel: function() {
        spots = app.documents[0].spots;
        var colorLabelArray = new Array();
        for (var i = 0; i < spots.length; i++) {
            try {
                if (spots[i].toString() !== "[Spot]") {
                    if (spots[i].name.indexOf('$') == 0) {
                        colorLabelArray.push(spots[i].name.substring(1, spots[i].name.length));
                    }
                }
            } catch (e) {}
        }

        var colorInfo = getColorInfromation();
        var obj = {}
        if (colorInfo.length > 0)
            colorLabelArray = [];
        for (var i = 0; i < colorInfo.length; i++) {
            colorLabelArray.push(colorInfo[i].colorName);
            if ((colorInfo[i].hasOwnProperty('colorType') && colorInfo[i].colorType != "teamColorVariable")) {
                if (colorInfo[i].isGarmentColor) {
                    obj[colorInfo[i].colorName] = "G"
                } else {
                    obj[colorInfo[i].colorName] = "D"
                }
            } else if (!colorInfo[i].hasOwnProperty('colorType')) {
                if (colorInfo[i].isGarmentColor) {
                    obj[colorInfo[i].colorName] = "G"
                } else {
                    obj[colorInfo[i].colorName] = "D"
                }
            }
        }
        var mainColorObj = {
            colorLabelArray: colorLabelArray,
            colorMapArray: obj
        }
        return JSON.stringify(mainColorObj);
    }
}

缩小产量 -

$._ext_ILST = {
    changeColorLabel: function() {
        spots = app.documents[0].spots;
        for (var a = new Array, b = 0; b < spots.length; b++) try {
            "[Spot]" !== spots[b].toString() && 0 == spots[b].name.indexOf("$") && a.push(spots[b].name.substring(1, spots[b].name.length))
        } catch (c) {}
        var d = getColorInfromation(),
            e = {};
        d.length > 0 && (a = []);
        for (var b = 0; b < d.length; b++) a.push(d[b].colorName), d[b].hasOwnProperty("colorType") && "teamColorVariable" != d[b].colorType ? d[b].isGarmentColor ? e[d[b].colorName] = "G" : e[d[b].colorName] = "D" : d[b].hasOwnProperty("colorType") || (d[b].isGarmentColor ? e[d[b].colorName] = "G" : e[d[b].colorName] = "D");
        var f = {
            colorLabelArray: a,
            colorMapArray: e
        };
        return JSON.stringify(f)
    }
};

任何帮助将不胜感激。

谢谢, Upendra sengar

1 个答案:

答案 0 :(得分:0)

你应该lint your JS first

这是您的代码的后lint(并略微修改)版本:

/* globals app, $, getColorInfromation */

(function(app, $, JSON, undefined) {
    $._ext_ILST = {
        changeColorLabel: function() {
            var spots = app.documents[0].spots;
            var colorLabelArray = [];
            var i;
            for (i = 0; i < spots.length; i++) {
                try {
                    if (spots[i].toString() !== "[Spot]") {
                        if (spots[i].name.indexOf('$') === 0) {
                            colorLabelArray.push(spots[i].name.substring(1, spots[i].name.length));
                        }
                    }
                } catch (e) {}
            }

            var colorInfo = getColorInfromation();
            var obj = {};
            if (colorInfo.length > 0)
                colorLabelArray = [];
            for (i = 0; i < colorInfo.length; i++) {
                colorLabelArray.push(colorInfo[i].colorName);
                if ((colorInfo[i].hasOwnProperty('colorType') && colorInfo[i].colorType != "teamColorVariable")) {
                    if (colorInfo[i].isGarmentColor) {
                        obj[colorInfo[i].colorName] = "G";
                    } else {
                        obj[colorInfo[i].colorName] = "D";
                    }
                } else if (!colorInfo[i].hasOwnProperty('colorType')) {
                    if (colorInfo[i].isGarmentColor) {
                        obj[colorInfo[i].colorName] = "G";
                    } else {
                        obj[colorInfo[i].colorName] = "D";
                    }
                }
            }
            var mainColorObj = {
                colorLabelArray: colorLabelArray,
                colorMapArray: obj
            };
            return JSON.stringify(mainColorObj);
        }
    };
}(app, $, JSON));

请注意,我wrapped your code in an IIFE可以为您提供更好的丑化效果。另请注意,我只是对代码进行了专题扫描......我认为你可以采取更多措施使其更好(例如,if语句括号,我个人喜欢明确hoist all my vars等。 )。

接下来,我快速搜索online minification/uglification tool并复制/粘贴代码的linted版本;这是结果:

!function(o,r,e,a){r._ext_ILST={changeColorLabel:function(){var r,a=o.documents[0].spots,n=[];for(r=0;r<a.length;r++)try{"[Spot]"!==a[r].toString()&&0===a[r].name.indexOf("$")&&n.push(a[r].name.substring(1,a[r].name.length))}catch(t){}var l=getColorInfromation(),c={};for(l.length>0&&(n=[]),r=0;r<l.length;r++)n.push(l[r].colorName),l[r].hasOwnProperty("colorType")&&"teamColorVariable"!=l[r].colorType?l[r].isGarmentColor?c[l[r].colorName]="G":c[l[r].colorName]="D":l[r].hasOwnProperty("colorType")||(l[r].isGarmentColor?c[l[r].colorName]="G":c[l[r].colorName]="D");var m={colorLabelArray:n,colorMapArray:c};return e.stringify(m)}}}(app,$,JSON);

不确定这是否是您正在寻找的,和/或代码是否仍在运作,但希望这会让您朝着正确的方向前进?