保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
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
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Tobias Koppers @sokra
*/
"use strict";
 
const RequireEnsureDependenciesBlock = require("./RequireEnsureDependenciesBlock");
const RequireEnsureItemDependency = require("./RequireEnsureItemDependency");
const getFunctionExpression = require("./getFunctionExpression");
 
module.exports = class RequireEnsureDependenciesBlockParserPlugin {
    apply(parser) {
        parser.hooks.call
            .for("require.ensure")
            .tap("RequireEnsureDependenciesBlockParserPlugin", expr => {
                let chunkName = null;
                let chunkNameRange = null;
                let errorExpressionArg = null;
                let errorExpression = null;
                switch (expr.arguments.length) {
                    case 4: {
                        const chunkNameExpr = parser.evaluateExpression(expr.arguments[3]);
                        if (!chunkNameExpr.isString()) return;
                        chunkNameRange = chunkNameExpr.range;
                        chunkName = chunkNameExpr.string;
                    }
                    // falls through
                    case 3: {
                        errorExpressionArg = expr.arguments[2];
                        errorExpression = getFunctionExpression(errorExpressionArg);
 
                        if (!errorExpression && !chunkName) {
                            const chunkNameExpr = parser.evaluateExpression(
                                expr.arguments[2]
                            );
                            if (!chunkNameExpr.isString()) return;
                            chunkNameRange = chunkNameExpr.range;
                            chunkName = chunkNameExpr.string;
                        }
                    }
                    // falls through
                    case 2: {
                        const dependenciesExpr = parser.evaluateExpression(
                            expr.arguments[0]
                        );
                        const dependenciesItems = dependenciesExpr.isArray()
                            ? dependenciesExpr.items
                            : [dependenciesExpr];
                        const successExpressionArg = expr.arguments[1];
                        const successExpression = getFunctionExpression(
                            successExpressionArg
                        );
 
                        if (successExpression) {
                            parser.walkExpressions(successExpression.expressions);
                        }
                        if (errorExpression) {
                            parser.walkExpressions(errorExpression.expressions);
                        }
 
                        const dep = new RequireEnsureDependenciesBlock(
                            expr,
                            successExpression ? successExpression.fn : successExpressionArg,
                            errorExpression ? errorExpression.fn : errorExpressionArg,
                            chunkName,
                            chunkNameRange,
                            parser.state.module,
                            expr.loc
                        );
                        const old = parser.state.current;
                        parser.state.current = dep;
                        try {
                            let failed = false;
                            parser.inScope([], () => {
                                for (const ee of dependenciesItems) {
                                    if (ee.isString()) {
                                        const edep = new RequireEnsureItemDependency(ee.string);
                                        edep.loc = dep.loc;
                                        dep.addDependency(edep);
                                    } else {
                                        failed = true;
                                    }
                                }
                            });
                            if (failed) {
                                return;
                            }
                            if (successExpression) {
                                if (successExpression.fn.body.type === "BlockStatement") {
                                    parser.walkStatement(successExpression.fn.body);
                                } else {
                                    parser.walkExpression(successExpression.fn.body);
                                }
                            }
                            old.addBlock(dep);
                        } finally {
                            parser.state.current = old;
                        }
                        if (!successExpression) {
                            parser.walkExpression(successExpressionArg);
                        }
                        if (errorExpression) {
                            if (errorExpression.fn.body.type === "BlockStatement") {
                                parser.walkStatement(errorExpression.fn.body);
                            } else {
                                parser.walkExpression(errorExpression.fn.body);
                            }
                        } else if (errorExpressionArg) {
                            parser.walkExpression(errorExpressionArg);
                        }
                        return true;
                    }
                }
            });
    }
};