forked from babel/babel
/
file.js
312 lines (263 loc) · 8.23 KB
/
file.js
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// @flow
import * as helpers from "@babel/helpers";
import { NodePath, Scope, type HubInterface } from "@babel/traverse";
import { codeFrameColumns } from "@babel/code-frame";
import traverse from "@babel/traverse";
import * as t from "@babel/types";
import semver from "semver";
import type { NormalizedFile } from "../normalize-file";
const errorVisitor = {
enter(path, state) {
const loc = path.node.loc;
if (loc) {
state.loc = loc;
path.stop();
}
},
};
export type NodeLocation = {
loc?: {
end?: { line: number, column: number },
start: { line: number, column: number },
},
_loc?: {
end?: { line: number, column: number },
start: { line: number, column: number },
},
};
export default class File {
_map: Map<any, any> = new Map();
opts: Object;
declarations: Object = {};
path: NodePath = null;
ast: Object = {};
scope: Scope;
metadata: {} = {};
code: string = "";
inputMap: Object | null = null;
hub: HubInterface = {
// keep it for the usage in babel-core, ex: path.hub.file.opts.filename
file: this,
getCode: () => this.code,
getScope: () => this.scope,
addHelper: this.addHelper.bind(this),
buildError: this.buildCodeFrameError.bind(this),
};
constructor(options: {}, { code, ast, inputMap }: NormalizedFile) {
this.opts = options;
this.code = code;
this.ast = ast;
this.inputMap = inputMap;
this.path = NodePath.get({
hub: this.hub,
parentPath: null,
parent: this.ast,
container: this.ast,
key: "program",
}).setContext();
this.scope = this.path.scope;
}
/**
* Provide backward-compatible access to the interpreter directive handling
* in Babel 6.x. If you are writing a plugin for Babel 7.x, it would be
* best to use 'program.interpreter' directly.
*/
get shebang(): string {
const { interpreter } = this.path.node;
return interpreter ? interpreter.value : "";
}
set shebang(value: string): void {
if (value) {
this.path.get("interpreter").replaceWith(t.interpreterDirective(value));
} else {
this.path.get("interpreter").remove();
}
}
set(key: mixed, val: mixed) {
if (key === "helpersNamespace") {
throw new Error(
"Babel 7.0.0-beta.56 has dropped support for the 'helpersNamespace' utility." +
"If you are using @babel/plugin-external-helpers you will need to use a newer " +
"version than the one you currently have installed. " +
"If you have your own implementation, you'll want to explore using 'helperGenerator' " +
"alongside 'file.availableHelper()'.",
);
}
this._map.set(key, val);
}
get(key: mixed): any {
return this._map.get(key);
}
has(key: mixed): boolean {
return this._map.has(key);
}
getModuleName(): ?string {
const {
filename,
filenameRelative = filename,
moduleId,
moduleIds = !!moduleId,
getModuleId,
sourceRoot: sourceRootTmp,
moduleRoot = sourceRootTmp,
sourceRoot = moduleRoot,
} = this.opts;
if (!moduleIds) return null;
// moduleId is n/a if a `getModuleId()` is provided
if (moduleId != null && !getModuleId) {
return moduleId;
}
let moduleName = moduleRoot != null ? moduleRoot + "/" : "";
if (filenameRelative) {
const sourceRootReplacer =
sourceRoot != null ? new RegExp("^" + sourceRoot + "/?") : "";
moduleName += filenameRelative
// remove sourceRoot from filename
.replace(sourceRootReplacer, "")
// remove extension
.replace(/\.(\w*?)$/, "");
}
// normalize path separators
moduleName = moduleName.replace(/\\/g, "/");
if (getModuleId) {
// If return is falsy, assume they want us to use our generated default name
return getModuleId(moduleName) || moduleName;
} else {
return moduleName;
}
}
addImport() {
throw new Error(
"This API has been removed. If you're looking for this " +
"functionality in Babel 7, you should import the " +
"'@babel/helper-module-imports' module and use the functions exposed " +
" from that module, such as 'addNamed' or 'addDefault'.",
);
}
/**
* Check if a given helper is available in @babel/core's helper list.
*
* This _also_ allows you to pass a Babel version specifically. If the
* helper exists, but was not available for the full given range, it will be
* considered unavailable.
*/
availableHelper(name: string, versionRange: ?string): boolean {
let minVersion;
try {
minVersion = helpers.minVersion(name);
} catch (err) {
if (err.code !== "BABEL_HELPER_UNKNOWN") throw err;
return false;
}
if (typeof versionRange !== "string") return true;
// semver.intersects() has some surprising behavior with comparing ranges
// with preprelease versions. We add '^' to ensure that we are always
// comparing ranges with ranges, which sidesteps this logic.
// For example:
//
// semver.intersects(`<7.0.1`, "7.0.0-beta.0") // false - surprising
// semver.intersects(`<7.0.1`, "^7.0.0-beta.0") // true - expected
//
// This is because the first falls back to
//
// semver.satisfies("7.0.0-beta.0", `<7.0.1`) // false - surprising
//
// and this fails because a prerelease version can only satisfy a range
// if it is a prerelease within the same major/minor/patch range.
//
// Note: If this is found to have issues, please also revisit the logic in
// transform-runtime's definitions.js file.
if (semver.valid(versionRange)) versionRange = `^${versionRange}`;
return (
!semver.intersects(`<${minVersion}`, versionRange) &&
!semver.intersects(`>=8.0.0`, versionRange)
);
}
addHelper(name: string): Object {
const declar = this.declarations[name];
if (declar) return t.cloneNode(declar);
const generator = this.get("helperGenerator");
if (generator) {
const res = generator(name);
if (res) return res;
}
// make sure that the helper exists
helpers.ensure(name);
const uid = (this.declarations[name] = this.scope.generateUidIdentifier(
name,
));
const dependencies = {};
for (const dep of helpers.getDependencies(name)) {
dependencies[dep] = this.addHelper(dep);
}
const { nodes, globals } = helpers.get(
name,
dep => dependencies[dep],
uid,
Object.keys(this.scope.getAllBindings()),
);
globals.forEach(name => {
if (this.path.scope.hasBinding(name, true /* noGlobals */)) {
this.path.scope.rename(name);
}
});
nodes.forEach(node => {
node._compact = true;
});
this.path.unshiftContainer("body", nodes);
// TODO: NodePath#unshiftContainer should automatically register new
// bindings.
this.path.get("body").forEach(path => {
if (nodes.indexOf(path.node) === -1) return;
if (path.isVariableDeclaration()) this.scope.registerDeclaration(path);
});
return uid;
}
addTemplateObject() {
throw new Error(
"This function has been moved into the template literal transform itself.",
);
}
buildCodeFrameError(
node: ?NodeLocation,
msg: string,
Error: typeof Error = SyntaxError,
): Error {
let loc = node && (node.loc || node._loc);
msg = `${this.opts.filename}: ${msg}`;
if (!loc && node) {
const state = {
loc: null,
};
traverse(node, errorVisitor, this.scope, state);
loc = state.loc;
let txt =
"This is an error on an internal node. Probably an internal error.";
if (loc) txt += " Location has been estimated.";
msg += ` (${txt})`;
}
if (loc) {
const { highlightCode = true } = this.opts;
msg +=
"\n" +
codeFrameColumns(
this.code,
{
start: {
line: loc.start.line,
column: loc.start.column + 1,
},
end:
loc.end && loc.start.line === loc.end.line
? {
line: loc.end.line,
column: loc.end.column + 1,
}
: undefined,
},
{ highlightCode },
);
}
return new Error(msg);
}
}