Enumerate regular expressions via UglifyJS

前端 未结 2 1253
余生分开走
余生分开走 2021-01-05 07:56

I have some JavaScript code, from which I need to find start+end indexes of every literal regular expression.

How can such information be extracted from UglifyJS?

相关标签:
2条回答
  • 2021-01-05 08:35

    Since nobody has answered yet, I have managed to come up with a head-on solution that works, though perhaps not the best one.

    function enumRegEx(parsed) {
        var result = [];
    
        function loop(obj) {
    
            if (obj && typeof obj === 'object') {
                if (obj.used) {
                    return;
                } else {
                    obj.used = true;
                }
                if (obj instanceof Array) {
                    obj.forEach(function (d) {
                        loop(d);
                    });
                } else {
                    if (obj instanceof uglify.AST_Node) {
                        for (var v in obj) {
                            loop(obj[v]);
                        }
                    } else {
                        if (obj instanceof uglify.AST_Token) {
                            if (obj.type === 'regexp') {
                                result.push({
                                    startIdx: obj.col,
                                    endIdx: obj.endcol
                                });
                            }
                        }
                    }
                }
            }
        }
    
        loop(parsed);
        return result;
    }
    

    The things I don't like about such approach:

    • I'm using it against a huge, 30,000 lines JavaScript file, which gets parsed by UglifyJS in 240ms, and then my algorithm takes another 430ms just to enumerate regular expressions. This seems quite inefficient.

    • I have to modify the original objects with property used because the parsed structure uses mutual references, which otherwise results in infinite loops and running out of call stack. Although I'm not worried about that very much, since I'm not using the parsed data for anything else.

    If you know a better approach - please, throw it in! At this point I'm mostly interested in improving the performance of my enumeration, which is currently quite slow, compared to the actual parsing that is.

    0 讨论(0)
  • 2021-01-05 08:39

    I got this ultimately useful link to the UglifyJS author's blog post, which pointed me in the right direction. Based on that blog I was able to modify my enumeration code to the following:

    function enumRegEx(parsed) {
        var result = [];
        parsed.walk(new uglify.TreeWalker(function (obj) {
            if (obj instanceof uglify.AST_RegExp) {
                result.push({
                    startIdx: obj.end.col,
                    endIdx: obj.end.endcol
                });
            }
        }));
        return result;
    }
    

    Not only this thing is shorter and works the same, but its processing speed is almost instant, within 10ms, which puts the previous result (430ms) to shame.

    Now that is the result I was looking for! :)

    UPDATE: In the end though, I found out that for this particular task esprima is a much better choice. It is much faster and has full ES6 support, unlike UglifyJS.

    The very same task done via esprima, thanks to the excellent support from Ariya Hidayat:

    function parseRegEx(originalCode) {
        var result = [];
        esprima.tokenize(originalCode, {loc: true, range: true}, function (obj) {
            if (obj.type === 'RegularExpression') {
                result.push({
                    startIdx: obj.range[0],
                    endIdx: obj.range[1]
                });
            }
        });
        return result;
    }
    

    As you can see, with esprima you do not even need to parse the code, you pass in the original code instead, which esprima will only tokenize, which is way faster.

    0 讨论(0)
提交回复
热议问题