aboutsummaryrefslogtreecommitdiff
path: root/test/mozilla-ast.js
blob: c10c679a8ea5699666fcd8a5795a0af623ba85e0 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Testing UglifyJS <-> SpiderMonkey AST conversion
"use strict";

var acorn = require("acorn");
var ufuzz = require("./ufuzz");
var UglifyJS = require("..");

function beautify(ast) {
    var beautified = UglifyJS.minify(ast, {
        compress: false,
        mangle: false,
        output: {
            beautify: true,
            braces: true,
        },
    });
    if (beautified.error) return beautified;
    return UglifyJS.minify(beautified.code, {
        compress: false,
        mangle: false,
        output: {
            ast: true,
        },
    });
}

function validate(ast) {
    try {
        ast.walk(new UglifyJS.TreeWalker(function(node) {
            node.validate();
        }));
    } catch (e) {
        return { error: e };
    }
    return UglifyJS.minify(ast, {
        compress: false,
        mangle: false,
        output: {
            ast: true,
        },
        validate: true,
    });
}

function patch_import(code) {
    return code.replace(/\bimport\s*\{\s*\}\s*from\s*(['"])/g, "import$1")
        .replace(/\b(import\b.*?)\s*,\s*\{\s*\}\s*(from\s*['"])/g, "$1 $2");
}

function equals(input, transformed) {
    if (input.code === transformed.code) return true;
    return patch_import(input.code) === patch_import(transformed.code);
}

function test(input, to_moz, description, skip_on_error, beautified) {
    try {
        var ast = UglifyJS.AST_Node.from_mozilla_ast(to_moz(input));
    } catch (e) {
        if (skip_on_error) return true;
        console.log("//=============================================================");
        console.log("//", description, "failed... round", round);
        console.log(e);
        console.log("// original code");
        if (beautified === true) console.log("// (beautified)");
        console.log(input.code);
        return false;
    }
    var transformed = validate(ast);
    if (transformed.error || !equals(input, transformed)) {
        if (!beautified) {
            beautified = beautify(input.ast);
            if (!beautified.error) {
                beautified.raw = beautified.code;
                if (!test(beautified, to_moz, description, skip_on_error, true)) return false;
            }
        }
        console.log("//=============================================================");
        console.log("// !!!!!! Failed... round", round);
        console.log("// original code");
        if (beautified.error) {
            console.log("// !!! beautify failed !!!");
            console.log(beautified.error.stack);
        } else if (beautified === true) {
            console.log("// (beautified)");
        }
        console.log(input.raw);
        console.log();
        console.log();
        console.log("//-------------------------------------------------------------");
        console.log("//", description);
        if (transformed.error) {
            console.log(transformed.error.stack);
        } else {
            beautified = beautify(transformed.ast);
            if (beautified.error) {
                console.log("// !!! beautify failed !!!");
                console.log(beautified.error.stack);
                console.log(transformed.code);
            } else {
                console.log("// (beautified)");
                console.log(beautified.code);
            }
        }
        console.log("!!!!!! Failed... round", round);
        return false;
    }
    return true;
}

var num_iterations = ufuzz.num_iterations;
var minify_options = require("./ufuzz/options.json").map(JSON.stringify);
minify_options.unshift(null);
for (var round = 1; round <= num_iterations; round++) {
    process.stdout.write(round + " of " + num_iterations + "\r");
    var code = ufuzz.createTopLevelCode();
    minify_options.forEach(function(options) {
        var ok = true;
        var input = UglifyJS.minify(options ? UglifyJS.minify(code, JSON.parse(options)).code : code, {
            compress: false,
            mangle: false,
            output: {
                ast: true,
            },
        });
        input.raw = options ? input.code : code;
        if (input.error) {
            ok = false;
            console.log("//=============================================================");
            console.log("// minify() failed... round", round);
            console.log(input.error);
            console.log("// original code");
            console.log(code);
        }
        if (ok) ok = test(input, function(input) {
            return input.ast.to_mozilla_ast();
        }, "AST_Node.to_mozilla_ast()");
        if (ok) ok = test(input, function(input) {
            return acorn.parse(input.raw, {
                ecmaVersion: "latest",
                locations: true,
                sourceType: "module",
            });
        }, "acorn.parse()", !ufuzz.verbose);
        if (!ok) process.exit(1);
    });
}
console.log();