UNPKG

6.73 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8function helpers() {
9 const data = _interopRequireWildcard(require("@babel/helpers"));
10
11 helpers = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _traverse() {
19 const data = _interopRequireWildcard(require("@babel/traverse"));
20
21 _traverse = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _codeFrame() {
29 const data = require("@babel/code-frame");
30
31 _codeFrame = function () {
32 return data;
33 };
34
35 return data;
36}
37
38function t() {
39 const data = _interopRequireWildcard(require("@babel/types"));
40
41 t = function () {
42 return data;
43 };
44
45 return data;
46}
47
48function _helperModuleTransforms() {
49 const data = require("@babel/helper-module-transforms");
50
51 _helperModuleTransforms = function () {
52 return data;
53 };
54
55 return data;
56}
57
58function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
59
60function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
61
62const semver = require("semver");
63
64const errorVisitor = {
65 enter(path, state) {
66 const loc = path.node.loc;
67
68 if (loc) {
69 state.loc = loc;
70 path.stop();
71 }
72 }
73
74};
75
76class File {
77 constructor(options, {
78 code,
79 ast,
80 inputMap
81 }) {
82 this._map = new Map();
83 this.opts = void 0;
84 this.declarations = {};
85 this.path = null;
86 this.ast = {};
87 this.scope = void 0;
88 this.metadata = {};
89 this.code = "";
90 this.inputMap = null;
91 this.hub = {
92 file: this,
93 getCode: () => this.code,
94 getScope: () => this.scope,
95 addHelper: this.addHelper.bind(this),
96 buildError: this.buildCodeFrameError.bind(this)
97 };
98 this.opts = options;
99 this.code = code;
100 this.ast = ast;
101 this.inputMap = inputMap;
102 this.path = _traverse().NodePath.get({
103 hub: this.hub,
104 parentPath: null,
105 parent: this.ast,
106 container: this.ast,
107 key: "program"
108 }).setContext();
109 this.scope = this.path.scope;
110 }
111
112 get shebang() {
113 const {
114 interpreter
115 } = this.path.node;
116 return interpreter ? interpreter.value : "";
117 }
118
119 set shebang(value) {
120 if (value) {
121 this.path.get("interpreter").replaceWith(t().interpreterDirective(value));
122 } else {
123 this.path.get("interpreter").remove();
124 }
125 }
126
127 set(key, val) {
128 if (key === "helpersNamespace") {
129 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()'.");
130 }
131
132 this._map.set(key, val);
133 }
134
135 get(key) {
136 return this._map.get(key);
137 }
138
139 has(key) {
140 return this._map.has(key);
141 }
142
143 getModuleName() {
144 return (0, _helperModuleTransforms().getModuleName)(this.opts, this.opts);
145 }
146
147 addImport() {
148 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'.");
149 }
150
151 availableHelper(name, versionRange) {
152 let minVersion;
153
154 try {
155 minVersion = helpers().minVersion(name);
156 } catch (err) {
157 if (err.code !== "BABEL_HELPER_UNKNOWN") throw err;
158 return false;
159 }
160
161 if (typeof versionRange !== "string") return true;
162 if (semver.valid(versionRange)) versionRange = `^${versionRange}`;
163 return !semver.intersects(`<${minVersion}`, versionRange) && !semver.intersects(`>=8.0.0`, versionRange);
164 }
165
166 addHelper(name) {
167 const declar = this.declarations[name];
168 if (declar) return t().cloneNode(declar);
169 const generator = this.get("helperGenerator");
170
171 if (generator) {
172 const res = generator(name);
173 if (res) return res;
174 }
175
176 helpers().ensure(name, File);
177 const uid = this.declarations[name] = this.scope.generateUidIdentifier(name);
178 const dependencies = {};
179
180 for (const dep of helpers().getDependencies(name)) {
181 dependencies[dep] = this.addHelper(dep);
182 }
183
184 const {
185 nodes,
186 globals
187 } = helpers().get(name, dep => dependencies[dep], uid, Object.keys(this.scope.getAllBindings()));
188 globals.forEach(name => {
189 if (this.path.scope.hasBinding(name, true)) {
190 this.path.scope.rename(name);
191 }
192 });
193 nodes.forEach(node => {
194 node._compact = true;
195 });
196 this.path.unshiftContainer("body", nodes);
197 this.path.get("body").forEach(path => {
198 if (nodes.indexOf(path.node) === -1) return;
199 if (path.isVariableDeclaration()) this.scope.registerDeclaration(path);
200 });
201 return uid;
202 }
203
204 addTemplateObject() {
205 throw new Error("This function has been moved into the template literal transform itself.");
206 }
207
208 buildCodeFrameError(node, msg, _Error = SyntaxError) {
209 let loc = node && (node.loc || node._loc);
210
211 if (!loc && node) {
212 const state = {
213 loc: null
214 };
215 (0, _traverse().default)(node, errorVisitor, this.scope, state);
216 loc = state.loc;
217 let txt = "This is an error on an internal node. Probably an internal error.";
218 if (loc) txt += " Location has been estimated.";
219 msg += ` (${txt})`;
220 }
221
222 if (loc) {
223 const {
224 highlightCode = true
225 } = this.opts;
226 msg += "\n" + (0, _codeFrame().codeFrameColumns)(this.code, {
227 start: {
228 line: loc.start.line,
229 column: loc.start.column + 1
230 },
231 end: loc.end && loc.start.line === loc.end.line ? {
232 line: loc.end.line,
233 column: loc.end.column + 1
234 } : undefined
235 }, {
236 highlightCode
237 });
238 }
239
240 return new _Error(msg);
241 }
242
243}
244
245exports.default = File;
\No newline at end of file