File size: 3,437 Bytes
7d73cf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStaticJSONValue = exports.isUndefinedIdentifier = exports.isNumberIdentifier = exports.isExpression = void 0;
function isExpression(node) {
    if (node.type === "JSONIdentifier" || node.type === "JSONLiteral") {
        const parent = node.parent;
        if (parent.type === "JSONProperty" && parent.key === node) {
            return false;
        }
        return true;
    }
    if (node.type === "JSONObjectExpression" ||
        node.type === "JSONArrayExpression" ||
        node.type === "JSONUnaryExpression" ||
        node.type === "JSONTemplateLiteral") {
        return true;
    }
    return false;
}
exports.isExpression = isExpression;
function isNumberIdentifier(node) {
    return (isExpression(node) && (node.name === "Infinity" || node.name === "NaN"));
}
exports.isNumberIdentifier = isNumberIdentifier;
function isUndefinedIdentifier(node) {
    return isExpression(node) && node.name === "undefined";
}
exports.isUndefinedIdentifier = isUndefinedIdentifier;
const resolver = {
    Program(node) {
        if (node.body.length !== 1 ||
            node.body[0].type !== "JSONExpressionStatement") {
            throw new Error("Illegal argument");
        }
        return getStaticJSONValue(node.body[0]);
    },
    JSONExpressionStatement(node) {
        return getStaticJSONValue(node.expression);
    },
    JSONObjectExpression(node) {
        const object = {};
        for (const prop of node.properties) {
            Object.assign(object, getStaticJSONValue(prop));
        }
        return object;
    },
    JSONProperty(node) {
        const keyName = node.key.type === "JSONLiteral"
            ? `${node.key.value}`
            : node.key.name;
        return {
            [keyName]: getStaticJSONValue(node.value),
        };
    },
    JSONArrayExpression(node) {
        const array = [];
        for (let index = 0; index < node.elements.length; index++) {
            const element = node.elements[index];
            if (element) {
                array[index] = getStaticJSONValue(element);
            }
        }
        return array;
    },
    JSONLiteral(node) {
        if (node.regex) {
            try {
                return new RegExp(node.regex.pattern, node.regex.flags);
            }
            catch (_a) {
                return `/${node.regex.pattern}/${node.regex.flags}`;
            }
        }
        if (node.bigint != null) {
            try {
                return BigInt(node.bigint);
            }
            catch (_b) {
                return `${node.bigint}`;
            }
        }
        return node.value;
    },
    JSONUnaryExpression(node) {
        const value = getStaticJSONValue(node.argument);
        return node.operator === "-" ? -value : value;
    },
    JSONIdentifier(node) {
        if (node.name === "Infinity") {
            return Infinity;
        }
        if (node.name === "NaN") {
            return NaN;
        }
        if (node.name === "undefined") {
            return undefined;
        }
        throw new Error("Illegal argument");
    },
    JSONTemplateLiteral(node) {
        return getStaticJSONValue(node.quasis[0]);
    },
    JSONTemplateElement(node) {
        return node.value.cooked;
    },
};
function getStaticJSONValue(node) {
    return resolver[node.type](node);
}
exports.getStaticJSONValue = getStaticJSONValue;