forked from rollup/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast-utils.js
More file actions
132 lines (104 loc) · 3.52 KB
/
ast-utils.js
File metadata and controls
132 lines (104 loc) · 3.52 KB
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
export { default as isReference } from 'is-reference';
const operators = {
'==': (x) => equals(x.left, x.right, false),
'!=': (x) => not(operators['=='](x)),
'===': (x) => equals(x.left, x.right, true),
'!==': (x) => not(operators['==='](x)),
'!': (x) => isFalsy(x.argument),
'&&': (x) => isTruthy(x.left) && isTruthy(x.right),
'||': (x) => isTruthy(x.left) || isTruthy(x.right)
};
function not(value) {
return value === null ? value : !value;
}
function equals(a, b, strict) {
if (a.type !== b.type) return null;
// eslint-disable-next-line eqeqeq
if (a.type === 'Literal') return strict ? a.value === b.value : a.value == b.value;
return null;
}
export function isTruthy(node) {
if (!node) return false;
if (node.type === 'Literal') return !!node.value;
if (node.type === 'ParenthesizedExpression') return isTruthy(node.expression);
if (node.operator in operators) return operators[node.operator](node);
return null;
}
export function isFalsy(node) {
return not(isTruthy(node));
}
export function getKeypath(node) {
const parts = [];
while (node.type === 'MemberExpression') {
if (node.computed) return null;
parts.unshift(node.property.name);
// eslint-disable-next-line no-param-reassign
node = node.object;
}
if (node.type !== 'Identifier') return null;
const { name } = node;
parts.unshift(name);
return { name, keypath: parts.join('.') };
}
export const KEY_COMPILED_ESM = '__esModule';
export function getDefineCompiledEsmType(node) {
const definedPropertyWithExports = getDefinePropertyCallName(node, 'exports');
const definedProperty =
definedPropertyWithExports || getDefinePropertyCallName(node, 'module.exports');
if (definedProperty && definedProperty.key === KEY_COMPILED_ESM) {
return isTruthy(definedProperty.value)
? definedPropertyWithExports
? 'exports'
: 'module'
: false;
}
return false;
}
function getDefinePropertyCallName(node, targetName) {
const {
callee: { object, property }
} = node;
if (!object || object.type !== 'Identifier' || object.name !== 'Object') return;
if (!property || property.type !== 'Identifier' || property.name !== 'defineProperty') return;
if (node.arguments.length !== 3) return;
const targetNames = targetName.split('.');
const [target, key, value] = node.arguments;
if (targetNames.length === 1) {
if (target.type !== 'Identifier' || target.name !== targetNames[0]) {
return;
}
}
if (targetNames.length === 2) {
if (
target.type !== 'MemberExpression' ||
target.object.name !== targetNames[0] ||
target.property.name !== targetNames[1]
) {
return;
}
}
if (value.type !== 'ObjectExpression' || !value.properties) return;
const valueProperty = value.properties.find((p) => p.key && p.key.name === 'value');
if (!valueProperty || !valueProperty.value) return;
// eslint-disable-next-line consistent-return
return { key: key.value, value: valueProperty.value };
}
export function isShorthandProperty(parent) {
return parent && parent.type === 'Property' && parent.shorthand;
}
export function isPropertyDefinitionKey(parent, node) {
return parent && parent.type === 'PropertyDefinition' && parent.key === node;
}
export function hasDefineEsmProperty(node) {
return node.properties.some((property) => {
if (
property.type === 'Property' &&
property.key.type === 'Identifier' &&
property.key.name === '__esModule' &&
isTruthy(property.value)
) {
return true;
}
return false;
});
}