UNPKG

5.33 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6var _assert = _interopRequireDefault(require("assert"));
7
8var t = _interopRequireWildcard(require("babel-types"));
9
10function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
11
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
14var ImportBuilder = function () {
15 function ImportBuilder(importedSource, scope, file) {
16 Object.defineProperty(this, "_statements", {
17 configurable: true,
18 enumerable: true,
19 writable: true,
20 value: []
21 });
22 Object.defineProperty(this, "_resultName", {
23 configurable: true,
24 enumerable: true,
25 writable: true,
26 value: null
27 });
28 Object.defineProperty(this, "_scope", {
29 configurable: true,
30 enumerable: true,
31 writable: true,
32 value: null
33 });
34 Object.defineProperty(this, "_file", {
35 configurable: true,
36 enumerable: true,
37 writable: true,
38 value: null
39 });
40 this._scope = scope;
41 this._file = file;
42 this._importedSource = importedSource;
43 }
44
45 var _proto = ImportBuilder.prototype;
46
47 _proto.done = function done() {
48 return {
49 statements: this._statements,
50 resultName: this._resultName
51 };
52 };
53
54 _proto.import = function _import() {
55 this._statements.push(t.importDeclaration([], t.stringLiteral(this._importedSource)));
56
57 return this;
58 };
59
60 _proto.require = function require() {
61 this._statements.push(t.expressionStatement(t.callExpression(t.identifier("require"), [t.stringLiteral(this._importedSource)])));
62
63 return this;
64 };
65
66 _proto.namespace = function namespace(name) {
67 name = this._scope.generateUidIdentifier(name);
68 var statement = this._statements[this._statements.length - 1];
69 (0, _assert.default)(statement.type === "ImportDeclaration");
70 (0, _assert.default)(statement.specifiers.length === 0);
71 statement.specifiers = [t.importNamespaceSpecifier(name)];
72 this._resultName = t.clone(name);
73 return this;
74 };
75
76 _proto.default = function _default(name) {
77 name = this._scope.generateUidIdentifier(name);
78 var statement = this._statements[this._statements.length - 1];
79 (0, _assert.default)(statement.type === "ImportDeclaration");
80 (0, _assert.default)(statement.specifiers.length === 0);
81 statement.specifiers = [t.importDefaultSpecifier(name)];
82 this._resultName = t.clone(name);
83 return this;
84 };
85
86 _proto.named = function named(name, importName) {
87 if (importName === "default") return this.default(name);
88 name = this._scope.generateUidIdentifier(name);
89 var statement = this._statements[this._statements.length - 1];
90 (0, _assert.default)(statement.type === "ImportDeclaration");
91 (0, _assert.default)(statement.specifiers.length === 0);
92 statement.specifiers = [t.importSpecifier(name, t.identifier(importName))];
93 this._resultName = t.clone(name);
94 return this;
95 };
96
97 _proto.var = function _var(name) {
98 name = this._scope.generateUidIdentifier(name);
99 var statement = this._statements[this._statements.length - 1];
100
101 if (statement.type !== "ExpressionStatement") {
102 (0, _assert.default)(this._resultName);
103 statement = t.expressionStatement(this._resultName);
104
105 this._statements.push(statement);
106 }
107
108 this._statements[this._statements.length - 1] = t.variableDeclaration("var", [t.variableDeclarator(name, statement.expression)]);
109 this._resultName = t.clone(name);
110 return this;
111 };
112
113 _proto.defaultInterop = function defaultInterop() {
114 return this._interop(this._file.addHelper("interopRequireDefault"));
115 };
116
117 _proto.wildcardInterop = function wildcardInterop() {
118 return this._interop(this._file.addHelper("interopRequireWildcard"));
119 };
120
121 _proto._interop = function _interop(callee) {
122 var statement = this._statements[this._statements.length - 1];
123
124 if (statement.type === "ExpressionStatement") {
125 statement.expression = t.callExpression(callee, [statement.expression]);
126 } else if (statement.type === "VariableDeclaration") {
127 (0, _assert.default)(statement.declarations.length === 1);
128 statement.declarations[0].init = t.callExpression(callee, [statement.declarations[0].init]);
129 } else {
130 _assert.default.fail("Unexpected type.");
131 }
132
133 return this;
134 };
135
136 _proto.prop = function prop(name) {
137 var statement = this._statements[this._statements.length - 1];
138
139 if (statement.type === "ExpressionStatement") {
140 statement.expression = t.memberExpression(statement.expression, t.identifier(name));
141 } else if (statement.type === "VariableDeclaration") {
142 (0, _assert.default)(statement.declarations.length === 1);
143 statement.declarations[0].init = t.memberExpression(statement.declarations[0].init, t.identifier(name));
144 } else {
145 _assert.default.fail("Unexpected type:" + statement.type);
146 }
147
148 return this;
149 };
150
151 _proto.read = function read(name) {
152 this._resultName = t.memberExpression(this._resultName, t.identifier(name));
153 };
154
155 return ImportBuilder;
156}();
157
158exports.default = ImportBuilder;
\No newline at end of file