1 | "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
2 |
|
3 | var _keywords = require('../parser/tokenizer/keywords');
|
4 | var _types = require('../parser/tokenizer/types');
|
5 |
|
6 | var _elideImportEquals = require('../util/elideImportEquals'); var _elideImportEquals2 = _interopRequireDefault(_elideImportEquals);
|
7 |
|
8 |
|
9 |
|
10 | var _getDeclarationInfo = require('../util/getDeclarationInfo'); var _getDeclarationInfo2 = _interopRequireDefault(_getDeclarationInfo);
|
11 | var _getNonTypeIdentifiers = require('../util/getNonTypeIdentifiers');
|
12 | var _shouldElideDefaultExport = require('../util/shouldElideDefaultExport'); var _shouldElideDefaultExport2 = _interopRequireDefault(_shouldElideDefaultExport);
|
13 |
|
14 | var _Transformer = require('./Transformer'); var _Transformer2 = _interopRequireDefault(_Transformer);
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | class ESMImportTransformer extends _Transformer2.default {
|
21 |
|
22 |
|
23 |
|
24 | constructor(
|
25 | tokens,
|
26 | nameManager,
|
27 | reactHotLoaderTransformer,
|
28 | isTypeScriptTransformEnabled,
|
29 | options,
|
30 | ) {
|
31 | super();this.tokens = tokens;this.nameManager = nameManager;this.reactHotLoaderTransformer = reactHotLoaderTransformer;this.isTypeScriptTransformEnabled = isTypeScriptTransformEnabled;;
|
32 | this.nonTypeIdentifiers = isTypeScriptTransformEnabled
|
33 | ? _getNonTypeIdentifiers.getNonTypeIdentifiers.call(void 0, tokens, options)
|
34 | : new Set();
|
35 | this.declarationInfo = isTypeScriptTransformEnabled
|
36 | ? _getDeclarationInfo2.default.call(void 0, tokens)
|
37 | : _getDeclarationInfo.EMPTY_DECLARATION_INFO;
|
38 | }
|
39 |
|
40 | process() {
|
41 |
|
42 | if (this.tokens.matches3(_types.TokenType._import, _types.TokenType.name, _types.TokenType.eq)) {
|
43 | return this.processImportEquals();
|
44 | }
|
45 | if (this.tokens.matches2(_types.TokenType._export, _types.TokenType.eq)) {
|
46 | this.tokens.replaceToken("module.exports");
|
47 | return true;
|
48 | }
|
49 | if (this.tokens.matches1(_types.TokenType._import)) {
|
50 | return this.processImport();
|
51 | }
|
52 | if (this.tokens.matches2(_types.TokenType._export, _types.TokenType._default)) {
|
53 | return this.processExportDefault();
|
54 | }
|
55 | if (this.tokens.matches2(_types.TokenType._export, _types.TokenType.braceL)) {
|
56 | return this.processNamedExports();
|
57 | }
|
58 | if (
|
59 | this.tokens.matches3(_types.TokenType._export, _types.TokenType.name, _types.TokenType.braceL) &&
|
60 | this.tokens.matchesContextualAtIndex(this.tokens.currentIndex() + 1, _keywords.ContextualKeyword._type)
|
61 | ) {
|
62 |
|
63 | this.tokens.removeInitialToken();
|
64 | while (!this.tokens.matches1(_types.TokenType.braceR)) {
|
65 | this.tokens.removeToken();
|
66 | }
|
67 | this.tokens.removeToken();
|
68 |
|
69 |
|
70 | if (
|
71 | this.tokens.matchesContextual(_keywords.ContextualKeyword._from) &&
|
72 | this.tokens.matches1AtIndex(this.tokens.currentIndex() + 1, _types.TokenType.string)
|
73 | ) {
|
74 | this.tokens.removeToken();
|
75 | this.tokens.removeToken();
|
76 | }
|
77 | return true;
|
78 | }
|
79 | return false;
|
80 | }
|
81 |
|
82 | processImportEquals() {
|
83 | const importName = this.tokens.identifierNameAtIndex(this.tokens.currentIndex() + 1);
|
84 | if (this.isTypeName(importName)) {
|
85 |
|
86 | _elideImportEquals2.default.call(void 0, this.tokens);
|
87 | } else {
|
88 |
|
89 | this.tokens.replaceToken("const");
|
90 | }
|
91 | return true;
|
92 | }
|
93 |
|
94 | processImport() {
|
95 | if (this.tokens.matches2(_types.TokenType._import, _types.TokenType.parenL)) {
|
96 |
|
97 | return false;
|
98 | }
|
99 |
|
100 | const snapshot = this.tokens.snapshot();
|
101 | const allImportsRemoved = this.removeImportTypeBindings();
|
102 | if (allImportsRemoved) {
|
103 | this.tokens.restoreToSnapshot(snapshot);
|
104 | while (!this.tokens.matches1(_types.TokenType.string)) {
|
105 | this.tokens.removeToken();
|
106 | }
|
107 | this.tokens.removeToken();
|
108 | if (this.tokens.matches1(_types.TokenType.semi)) {
|
109 | this.tokens.removeToken();
|
110 | }
|
111 | }
|
112 | return true;
|
113 | }
|
114 |
|
115 | |
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | removeImportTypeBindings() {
|
122 | this.tokens.copyExpectedToken(_types.TokenType._import);
|
123 | if (
|
124 | this.tokens.matchesContextual(_keywords.ContextualKeyword._type) &&
|
125 | !this.tokens.matches1AtIndex(this.tokens.currentIndex() + 1, _types.TokenType.comma) &&
|
126 | !this.tokens.matchesContextualAtIndex(this.tokens.currentIndex() + 1, _keywords.ContextualKeyword._from)
|
127 | ) {
|
128 |
|
129 | return true;
|
130 | }
|
131 |
|
132 | if (this.tokens.matches1(_types.TokenType.string)) {
|
133 |
|
134 | this.tokens.copyToken();
|
135 | return false;
|
136 | }
|
137 |
|
138 | let foundNonTypeImport = false;
|
139 |
|
140 | if (this.tokens.matches1(_types.TokenType.name)) {
|
141 | if (this.isTypeName(this.tokens.identifierName())) {
|
142 | this.tokens.removeToken();
|
143 | if (this.tokens.matches1(_types.TokenType.comma)) {
|
144 | this.tokens.removeToken();
|
145 | }
|
146 | } else {
|
147 | foundNonTypeImport = true;
|
148 | this.tokens.copyToken();
|
149 | if (this.tokens.matches1(_types.TokenType.comma)) {
|
150 | this.tokens.copyToken();
|
151 | }
|
152 | }
|
153 | }
|
154 |
|
155 | if (this.tokens.matches1(_types.TokenType.star)) {
|
156 | if (this.isTypeName(this.tokens.identifierNameAtIndex(this.tokens.currentIndex() + 2))) {
|
157 | this.tokens.removeToken();
|
158 | this.tokens.removeToken();
|
159 | this.tokens.removeToken();
|
160 | } else {
|
161 | foundNonTypeImport = true;
|
162 | this.tokens.copyExpectedToken(_types.TokenType.star);
|
163 | this.tokens.copyExpectedToken(_types.TokenType.name);
|
164 | this.tokens.copyExpectedToken(_types.TokenType.name);
|
165 | }
|
166 | } else if (this.tokens.matches1(_types.TokenType.braceL)) {
|
167 | this.tokens.copyToken();
|
168 | while (!this.tokens.matches1(_types.TokenType.braceR)) {
|
169 | if (
|
170 | this.tokens.matches3(_types.TokenType.name, _types.TokenType.name, _types.TokenType.comma) ||
|
171 | this.tokens.matches3(_types.TokenType.name, _types.TokenType.name, _types.TokenType.braceR)
|
172 | ) {
|
173 |
|
174 | this.tokens.removeToken();
|
175 | this.tokens.removeToken();
|
176 | if (this.tokens.matches1(_types.TokenType.comma)) {
|
177 | this.tokens.removeToken();
|
178 | }
|
179 | } else if (
|
180 | this.tokens.matches5(_types.TokenType.name, _types.TokenType.name, _types.TokenType.name, _types.TokenType.name, _types.TokenType.comma) ||
|
181 | this.tokens.matches5(_types.TokenType.name, _types.TokenType.name, _types.TokenType.name, _types.TokenType.name, _types.TokenType.braceR)
|
182 | ) {
|
183 |
|
184 | this.tokens.removeToken();
|
185 | this.tokens.removeToken();
|
186 | this.tokens.removeToken();
|
187 | this.tokens.removeToken();
|
188 | if (this.tokens.matches1(_types.TokenType.comma)) {
|
189 | this.tokens.removeToken();
|
190 | }
|
191 | } else if (
|
192 | this.tokens.matches2(_types.TokenType.name, _types.TokenType.comma) ||
|
193 | this.tokens.matches2(_types.TokenType.name, _types.TokenType.braceR)
|
194 | ) {
|
195 |
|
196 | if (this.isTypeName(this.tokens.identifierName())) {
|
197 | this.tokens.removeToken();
|
198 | if (this.tokens.matches1(_types.TokenType.comma)) {
|
199 | this.tokens.removeToken();
|
200 | }
|
201 | } else {
|
202 | foundNonTypeImport = true;
|
203 | this.tokens.copyToken();
|
204 | if (this.tokens.matches1(_types.TokenType.comma)) {
|
205 | this.tokens.copyToken();
|
206 | }
|
207 | }
|
208 | } else if (
|
209 | this.tokens.matches4(_types.TokenType.name, _types.TokenType.name, _types.TokenType.name, _types.TokenType.comma) ||
|
210 | this.tokens.matches4(_types.TokenType.name, _types.TokenType.name, _types.TokenType.name, _types.TokenType.braceR)
|
211 | ) {
|
212 |
|
213 | if (this.isTypeName(this.tokens.identifierNameAtIndex(this.tokens.currentIndex() + 2))) {
|
214 | this.tokens.removeToken();
|
215 | this.tokens.removeToken();
|
216 | this.tokens.removeToken();
|
217 | if (this.tokens.matches1(_types.TokenType.comma)) {
|
218 | this.tokens.removeToken();
|
219 | }
|
220 | } else {
|
221 | foundNonTypeImport = true;
|
222 | this.tokens.copyToken();
|
223 | this.tokens.copyToken();
|
224 | this.tokens.copyToken();
|
225 | if (this.tokens.matches1(_types.TokenType.comma)) {
|
226 | this.tokens.copyToken();
|
227 | }
|
228 | }
|
229 | } else {
|
230 | throw new Error("Unexpected import form.");
|
231 | }
|
232 | }
|
233 | this.tokens.copyExpectedToken(_types.TokenType.braceR);
|
234 | }
|
235 |
|
236 | return !foundNonTypeImport;
|
237 | }
|
238 |
|
239 | isTypeName(name) {
|
240 | return this.isTypeScriptTransformEnabled && !this.nonTypeIdentifiers.has(name);
|
241 | }
|
242 |
|
243 | processExportDefault() {
|
244 | if (
|
245 | _shouldElideDefaultExport2.default.call(void 0, this.isTypeScriptTransformEnabled, this.tokens, this.declarationInfo)
|
246 | ) {
|
247 |
|
248 |
|
249 |
|
250 | this.tokens.removeInitialToken();
|
251 | this.tokens.removeToken();
|
252 | this.tokens.removeToken();
|
253 | return true;
|
254 | }
|
255 |
|
256 | const alreadyHasName =
|
257 | this.tokens.matches4(_types.TokenType._export, _types.TokenType._default, _types.TokenType._function, _types.TokenType.name) ||
|
258 |
|
259 | this.tokens.matches5(_types.TokenType._export, _types.TokenType._default, _types.TokenType.name, _types.TokenType._function, _types.TokenType.name) ||
|
260 | this.tokens.matches4(_types.TokenType._export, _types.TokenType._default, _types.TokenType._class, _types.TokenType.name) ||
|
261 | this.tokens.matches5(_types.TokenType._export, _types.TokenType._default, _types.TokenType._abstract, _types.TokenType._class, _types.TokenType.name);
|
262 |
|
263 | if (!alreadyHasName && this.reactHotLoaderTransformer) {
|
264 |
|
265 |
|
266 | const defaultVarName = this.nameManager.claimFreeName("_default");
|
267 | this.tokens.replaceToken(`let ${defaultVarName}; export`);
|
268 | this.tokens.copyToken();
|
269 | this.tokens.appendCode(` ${defaultVarName} =`);
|
270 | this.reactHotLoaderTransformer.setExtractedDefaultExportName(defaultVarName);
|
271 | return true;
|
272 | }
|
273 | return false;
|
274 | }
|
275 |
|
276 | |
277 |
|
278 |
|
279 |
|
280 | processNamedExports() {
|
281 | if (!this.isTypeScriptTransformEnabled) {
|
282 | return false;
|
283 | }
|
284 | this.tokens.copyExpectedToken(_types.TokenType._export);
|
285 | this.tokens.copyExpectedToken(_types.TokenType.braceL);
|
286 |
|
287 | while (!this.tokens.matches1(_types.TokenType.braceR)) {
|
288 | if (!this.tokens.matches1(_types.TokenType.name)) {
|
289 | throw new Error("Expected identifier at the start of named export.");
|
290 | }
|
291 | if (this.shouldElideExportedName(this.tokens.identifierName())) {
|
292 | while (
|
293 | !this.tokens.matches1(_types.TokenType.comma) &&
|
294 | !this.tokens.matches1(_types.TokenType.braceR) &&
|
295 | !this.tokens.isAtEnd()
|
296 | ) {
|
297 | this.tokens.removeToken();
|
298 | }
|
299 | if (this.tokens.matches1(_types.TokenType.comma)) {
|
300 | this.tokens.removeToken();
|
301 | }
|
302 | } else {
|
303 | while (
|
304 | !this.tokens.matches1(_types.TokenType.comma) &&
|
305 | !this.tokens.matches1(_types.TokenType.braceR) &&
|
306 | !this.tokens.isAtEnd()
|
307 | ) {
|
308 | this.tokens.copyToken();
|
309 | }
|
310 | if (this.tokens.matches1(_types.TokenType.comma)) {
|
311 | this.tokens.copyToken();
|
312 | }
|
313 | }
|
314 | }
|
315 | this.tokens.copyExpectedToken(_types.TokenType.braceR);
|
316 | return true;
|
317 | }
|
318 |
|
319 | |
320 |
|
321 |
|
322 |
|
323 |
|
324 | shouldElideExportedName(name) {
|
325 | return (
|
326 | this.isTypeScriptTransformEnabled &&
|
327 | this.declarationInfo.typeDeclarations.has(name) &&
|
328 | !this.declarationInfo.valueDeclarations.has(name)
|
329 | );
|
330 | }
|
331 | } exports.default = ESMImportTransformer;
|