1 | // NOTE: This file must be compatible with old Node.js versions, since it runs
|
---|
2 | // during testing.
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * @typedef {Object} HelperMetadata
|
---|
6 | * @property {string[]} globals
|
---|
7 | * @property {{ [name: string]: string[] }} locals
|
---|
8 | * @property {{ [name: string]: string[] }} dependencies
|
---|
9 | * @property {string[]} exportBindingAssignments
|
---|
10 | * @property {string} exportName
|
---|
11 | */
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Given a file AST for a given helper, get a bunch of metadata about it so that Babel can quickly render
|
---|
15 | * the helper is whatever context it is needed in.
|
---|
16 | *
|
---|
17 | * @param {typeof import("@babel/core")} babel
|
---|
18 | *
|
---|
19 | * @returns {HelperMetadata}
|
---|
20 | */
|
---|
21 | export function getHelperMetadata(babel, code, helperName) {
|
---|
22 | const globals = new Set();
|
---|
23 | // Maps imported identifier name -> helper name
|
---|
24 | const dependenciesBindings = new Map();
|
---|
25 |
|
---|
26 | let exportName;
|
---|
27 | const exportBindingAssignments = [];
|
---|
28 | // helper name -> reference paths
|
---|
29 | const dependencies = new Map();
|
---|
30 | // local variable name -> reference paths
|
---|
31 | const locals = new Map();
|
---|
32 |
|
---|
33 | const spansToRemove = [];
|
---|
34 |
|
---|
35 | const validateDefaultExport = decl => {
|
---|
36 | if (exportName) {
|
---|
37 | throw new Error(
|
---|
38 | `Helpers can have only one default export (in ${helperName})`
|
---|
39 | );
|
---|
40 | }
|
---|
41 |
|
---|
42 | if (!decl.isFunctionDeclaration() || !decl.node.id) {
|
---|
43 | throw new Error(
|
---|
44 | `Helpers can only export named function declarations (in ${helperName})`
|
---|
45 | );
|
---|
46 | }
|
---|
47 | };
|
---|
48 |
|
---|
49 | /** @type {import("@babel/traverse").Visitor} */
|
---|
50 | const dependencyVisitor = {
|
---|
51 | Program(path) {
|
---|
52 | for (const child of path.get("body")) {
|
---|
53 | if (child.isImportDeclaration()) {
|
---|
54 | if (
|
---|
55 | child.get("specifiers").length !== 1 ||
|
---|
56 | !child.get("specifiers.0").isImportDefaultSpecifier()
|
---|
57 | ) {
|
---|
58 | throw new Error(
|
---|
59 | `Helpers can only import a default value (in ${helperName})`
|
---|
60 | );
|
---|
61 | }
|
---|
62 | dependenciesBindings.set(
|
---|
63 | child.node.specifiers[0].local.name,
|
---|
64 | child.node.source.value
|
---|
65 | );
|
---|
66 | dependencies.set(child.node.source.value, []);
|
---|
67 | spansToRemove.push([child.node.start, child.node.end]);
|
---|
68 | child.remove();
|
---|
69 | }
|
---|
70 | }
|
---|
71 | for (const child of path.get("body")) {
|
---|
72 | if (child.isExportDefaultDeclaration()) {
|
---|
73 | const decl = child.get("declaration");
|
---|
74 | validateDefaultExport(decl);
|
---|
75 |
|
---|
76 | exportName = decl.node.id.name;
|
---|
77 | spansToRemove.push([child.node.start, decl.node.start]);
|
---|
78 | child.replaceWith(decl.node);
|
---|
79 | } else if (
|
---|
80 | child.isExportNamedDeclaration() &&
|
---|
81 | child.node.specifiers.length === 1 &&
|
---|
82 | child.get("specifiers.0.exported").isIdentifier({ name: "default" })
|
---|
83 | ) {
|
---|
84 | const { name } = child.node.specifiers[0].local;
|
---|
85 |
|
---|
86 | validateDefaultExport(child.scope.getBinding(name).path);
|
---|
87 |
|
---|
88 | exportName = name;
|
---|
89 | spansToRemove.push([child.node.start, child.node.end]);
|
---|
90 | child.remove();
|
---|
91 | } else if (
|
---|
92 | process.env.IS_BABEL_OLD_E2E &&
|
---|
93 | child.isExportNamedDeclaration() &&
|
---|
94 | child.node.specifiers.length === 0
|
---|
95 | ) {
|
---|
96 | spansToRemove.push([child.node.start, child.node.end]);
|
---|
97 | child.remove();
|
---|
98 | } else if (
|
---|
99 | child.isExportAllDeclaration() ||
|
---|
100 | child.isExportNamedDeclaration()
|
---|
101 | ) {
|
---|
102 | throw new Error(`Helpers can only export default (in ${helperName})`);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | path.scope.crawl();
|
---|
107 |
|
---|
108 | const bindings = path.scope.getAllBindings();
|
---|
109 | Object.keys(bindings).forEach(name => {
|
---|
110 | if (dependencies.has(name)) return;
|
---|
111 |
|
---|
112 | const binding = bindings[name];
|
---|
113 |
|
---|
114 | const references = [
|
---|
115 | ...binding.path.getBindingIdentifierPaths(true)[name].map(makePath),
|
---|
116 | ...binding.referencePaths.map(makePath),
|
---|
117 | ];
|
---|
118 | for (const violation of binding.constantViolations) {
|
---|
119 | violation.getBindingIdentifierPaths(true)[name].forEach(path => {
|
---|
120 | references.push(makePath(path));
|
---|
121 | });
|
---|
122 | }
|
---|
123 |
|
---|
124 | locals.set(name, references);
|
---|
125 | });
|
---|
126 | },
|
---|
127 | ReferencedIdentifier(child) {
|
---|
128 | const name = child.node.name;
|
---|
129 | const binding = child.scope.getBinding(name);
|
---|
130 | if (!binding) {
|
---|
131 | if (dependenciesBindings.has(name)) {
|
---|
132 | dependencies
|
---|
133 | .get(dependenciesBindings.get(name))
|
---|
134 | .push(makePath(child));
|
---|
135 | } else if (name !== "arguments" || child.scope.path.isProgram()) {
|
---|
136 | globals.add(name);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | },
|
---|
140 | AssignmentExpression(child) {
|
---|
141 | const left = child.get("left");
|
---|
142 |
|
---|
143 | if (!(exportName in left.getBindingIdentifiers())) return;
|
---|
144 |
|
---|
145 | if (!left.isIdentifier()) {
|
---|
146 | throw new Error(
|
---|
147 | `Only simple assignments to exports are allowed in helpers (in ${helperName})`
|
---|
148 | );
|
---|
149 | }
|
---|
150 |
|
---|
151 | const binding = child.scope.getBinding(exportName);
|
---|
152 |
|
---|
153 | if (binding && binding.scope.path.isProgram()) {
|
---|
154 | exportBindingAssignments.push(makePath(child));
|
---|
155 | }
|
---|
156 | },
|
---|
157 | };
|
---|
158 |
|
---|
159 | babel.transformSync(code, {
|
---|
160 | configFile: false,
|
---|
161 | babelrc: false,
|
---|
162 | plugins: [() => ({ visitor: dependencyVisitor })],
|
---|
163 | });
|
---|
164 |
|
---|
165 | if (!exportName) throw new Error("Helpers must have a named default export.");
|
---|
166 |
|
---|
167 | // Process these in reverse so that mutating the references does not invalidate any later paths in
|
---|
168 | // the list.
|
---|
169 | exportBindingAssignments.reverse();
|
---|
170 |
|
---|
171 | spansToRemove.sort(([start1], [start2]) => start2 - start1);
|
---|
172 | for (const [start, end] of spansToRemove) {
|
---|
173 | code = code.slice(0, start) + code.slice(end);
|
---|
174 | }
|
---|
175 |
|
---|
176 | return [
|
---|
177 | code,
|
---|
178 | {
|
---|
179 | globals: Array.from(globals),
|
---|
180 | locals: Object.fromEntries(locals),
|
---|
181 | dependencies: Object.fromEntries(dependencies),
|
---|
182 | exportBindingAssignments,
|
---|
183 | exportName,
|
---|
184 | },
|
---|
185 | ];
|
---|
186 | }
|
---|
187 |
|
---|
188 | function makePath(path) {
|
---|
189 | const parts = [];
|
---|
190 |
|
---|
191 | for (; path.parentPath; path = path.parentPath) {
|
---|
192 | parts.push(path.key);
|
---|
193 | if (path.inList) parts.push(path.listKey);
|
---|
194 | }
|
---|
195 |
|
---|
196 | return parts.reverse().join(".");
|
---|
197 | }
|
---|
198 |
|
---|
199 | export function stringifyMetadata(metadata) {
|
---|
200 | return `\
|
---|
201 | {
|
---|
202 | globals: ${JSON.stringify(metadata.globals)},
|
---|
203 | locals: ${JSON.stringify(metadata.locals)},
|
---|
204 | exportBindingAssignments: ${JSON.stringify(metadata.exportBindingAssignments)},
|
---|
205 | exportName: ${JSON.stringify(metadata.exportName)},
|
---|
206 | dependencies: ${JSON.stringify(metadata.dependencies)},
|
---|
207 | }
|
---|
208 | `;
|
---|
209 | }
|
---|