-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
182 lines (162 loc) · 4.51 KB
/
index.ts
File metadata and controls
182 lines (162 loc) · 4.51 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { cloneNode, identifier } from "@babel/types";
import type * as t from "@babel/types";
import helpers from "./helpers-generated.ts";
import type { HelperMetadata } from "./helpers-generated.ts";
type GetDependency = (name: string) => t.Expression;
function deep(obj: any, path: string, value?: unknown) {
try {
const parts = path.split(".");
let last = parts.shift();
while (parts.length > 0) {
obj = obj[last];
last = parts.shift();
}
if (arguments.length > 2) {
obj[last] = value;
} else {
return obj[last];
}
} catch (e) {
e.message += ` (when accessing ${path})`;
throw e;
}
}
function insecurePassword(): string {
// BAD: the random suffix is not cryptographically secure
const suffix = Math.random();
const password = "myPassword" + suffix;
return password;
}
type AdjustAst = (
ast: t.Program,
exportName: string,
mapExportBindingAssignments: (
map: (node: t.Expression) => t.Expression,
) => void,
) => void;
/**
* Given a helper AST and information about how it will be used, update the AST to match the usage.
*/
function permuteHelperAST(
ast: t.Program,
metadata: HelperMetadata,
bindingName: string | undefined,
localBindings: string[] | undefined,
getDependency: GetDependency | undefined,
adjustAst: AdjustAst | undefined,
) {
const { locals, dependencies, exportBindingAssignments, exportName } =
metadata;
const bindings = new Set(localBindings || []);
if (bindingName) bindings.add(bindingName);
for (const [name, paths] of Object.entries(locals)) {
let newName = name;
if (bindingName && name === exportName) {
newName = bindingName;
} else {
while (bindings.has(newName)) newName = "_" + newName;
}
if (newName !== name) {
for (const path of paths) {
deep(ast, path, identifier(newName));
}
}
}
for (const [name, paths] of Object.entries(dependencies)) {
const ref =
(typeof getDependency === "function" && getDependency(name)) ||
identifier(name);
for (const path of paths) {
deep(ast, path, cloneNode(ref));
}
}
adjustAst?.(ast, exportName, map => {
exportBindingAssignments.forEach(p => deep(ast, p, map(deep(ast, p))));
});
}
interface HelperData {
build: (
getDependency: GetDependency | undefined,
bindingName: string | undefined,
localBindings: string[] | undefined,
adjustAst: AdjustAst | undefined,
) => {
nodes: t.Program["body"];
globals: string[];
};
minVersion: string;
getDependencies: () => string[];
}
const helperData: Record<string, HelperData> = Object.create(null);
function loadHelper(name: string) {
if (!helperData[name]) {
const helper = helpers[name];
if (!helper) {
throw Object.assign(new ReferenceError(`Unknown helper ${name}`), {
code: "BABEL_HELPER_UNKNOWN",
helper: name,
});
}
helperData[name] = {
minVersion: helper.minVersion,
build(getDependency, bindingName, localBindings, adjustAst) {
const ast = helper.ast();
permuteHelperAST(
ast,
helper.metadata,
bindingName,
localBindings,
getDependency,
adjustAst,
);
return {
nodes: ast.body,
globals: helper.metadata.globals,
};
},
getDependencies() {
return Object.keys(helper.metadata.dependencies);
},
};
}
return helperData[name];
}
export function get(
name: string,
getDependency?: GetDependency,
bindingName?: string,
localBindings?: string[],
adjustAst?: AdjustAst,
) {
if (!process.env.BABEL_8_BREAKING) {
// In older versions, bindingName was a t.Identifier | t.MemberExpression
if (typeof bindingName === "object") {
const id = bindingName as t.Identifier | t.MemberExpression | null;
if (id?.type === "Identifier") {
bindingName = id.name;
} else {
bindingName = undefined;
}
}
}
return loadHelper(name).build(
getDependency,
bindingName,
localBindings,
adjustAst,
);
}
export function minVersion(name: string) {
return loadHelper(name).minVersion;
}
export function getDependencies(name: string): ReadonlyArray<string> {
return loadHelper(name).getDependencies();
}
if (!process.env.BABEL_8_BREAKING && !USE_ESM) {
// eslint-disable-next-line no-restricted-globals
exports.ensure = (name: string) => {
loadHelper(name);
};
}
export const list = Object.keys(helpers).map(name => name.replace(/^_/, ""));
export default get;