aboutsummaryrefslogtreecommitdiff
path: root/node_modules/resolve-dependencies/lib/gather-deps.js
blob: cc45f2a487439560040d83b8c5812749ce2c0e24 (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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.gatherDependencies = void 0;
const meriyah_1 = require("meriyah");
function isNodeAString(node) {
    return Boolean(node && (node.type === 'Literal' || node.type === 'StringLiteral'));
}
function isRequire(node) {
    return node.callee.type === 'Identifier' && node.callee.name === 'require';
}
function isImport(node) {
    return node.callee.type === 'Import';
}
function walk(node, visit) {
    if (!node || typeof node.type !== 'string' || node._visited) {
        return;
    }
    visit(node);
    node._visited = true;
    for (const childNode in node) {
        const child = node[childNode];
        if (Array.isArray(child)) {
            for (let i = 0; i < child.length; i++) {
                walk(child[i], visit);
            }
        }
        else {
            walk(child, visit);
        }
    }
}
function gatherDependencies(code, isModule) {
    const result = {
        variable: false,
        deps: {},
    };
    if (!code) {
        return result;
    }
    const visit = (node) => {
        if (node.type === 'CallExpression' &&
            (isRequire(node) || isImport(node) || node.type === 'ImportExpression')) {
            const request = node.arguments[0];
            if (isNodeAString(request)) {
                result.deps[request.value] = null;
            }
            else {
                result.variable = true;
            }
        }
        if (node.type === 'ImportDeclaration' && isNodeAString(node.source)) {
            result.deps[node.source.value] = null;
        }
    }, options = {
        globalReturn: true,
        next: true,
        module: isModule,
        specDeviation: true,
    };
    walk(meriyah_1.parse(code, options), visit);
    return result;
}
exports.gatherDependencies = gatherDependencies;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ2F0aGVyLWRlcHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvZ2F0aGVyLWRlcHMudHMiXSwibmFtZXMiOltdLC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