UNPKG

5.22 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.ImportSpecifier = ImportSpecifier;
7exports.ImportDefaultSpecifier = ImportDefaultSpecifier;
8exports.ExportDefaultSpecifier = ExportDefaultSpecifier;
9exports.ExportSpecifier = ExportSpecifier;
10exports.ExportNamespaceSpecifier = ExportNamespaceSpecifier;
11exports.ExportAllDeclaration = ExportAllDeclaration;
12exports.ExportNamedDeclaration = ExportNamedDeclaration;
13exports.ExportDefaultDeclaration = ExportDefaultDeclaration;
14exports.ImportDeclaration = ImportDeclaration;
15exports.ImportAttribute = ImportAttribute;
16exports.ImportNamespaceSpecifier = ImportNamespaceSpecifier;
17
18var t = require("@babel/types");
19
20const {
21 isClassDeclaration,
22 isExportDefaultSpecifier,
23 isExportNamespaceSpecifier,
24 isImportDefaultSpecifier,
25 isImportNamespaceSpecifier,
26 isStatement
27} = t;
28
29function ImportSpecifier(node) {
30 if (node.importKind === "type" || node.importKind === "typeof") {
31 this.word(node.importKind);
32 this.space();
33 }
34
35 this.print(node.imported, node);
36
37 if (node.local && node.local.name !== node.imported.name) {
38 this.space();
39 this.word("as");
40 this.space();
41 this.print(node.local, node);
42 }
43}
44
45function ImportDefaultSpecifier(node) {
46 this.print(node.local, node);
47}
48
49function ExportDefaultSpecifier(node) {
50 this.print(node.exported, node);
51}
52
53function ExportSpecifier(node) {
54 this.print(node.local, node);
55
56 if (node.exported && node.local.name !== node.exported.name) {
57 this.space();
58 this.word("as");
59 this.space();
60 this.print(node.exported, node);
61 }
62}
63
64function ExportNamespaceSpecifier(node) {
65 this.token("*");
66 this.space();
67 this.word("as");
68 this.space();
69 this.print(node.exported, node);
70}
71
72function ExportAllDeclaration(node) {
73 this.word("export");
74 this.space();
75
76 if (node.exportKind === "type") {
77 this.word("type");
78 this.space();
79 }
80
81 this.token("*");
82 this.space();
83 this.word("from");
84 this.space();
85 this.print(node.source, node);
86 this.printAssertions(node);
87 this.semicolon();
88}
89
90function ExportNamedDeclaration(node) {
91 if (this.format.decoratorsBeforeExport && isClassDeclaration(node.declaration)) {
92 this.printJoin(node.declaration.decorators, node);
93 }
94
95 this.word("export");
96 this.space();
97 ExportDeclaration.apply(this, arguments);
98}
99
100function ExportDefaultDeclaration(node) {
101 if (this.format.decoratorsBeforeExport && isClassDeclaration(node.declaration)) {
102 this.printJoin(node.declaration.decorators, node);
103 }
104
105 this.word("export");
106 this.space();
107 this.word("default");
108 this.space();
109 ExportDeclaration.apply(this, arguments);
110}
111
112function ExportDeclaration(node) {
113 if (node.declaration) {
114 const declar = node.declaration;
115 this.print(declar, node);
116 if (!isStatement(declar)) this.semicolon();
117 } else {
118 if (node.exportKind === "type") {
119 this.word("type");
120 this.space();
121 }
122
123 const specifiers = node.specifiers.slice(0);
124 let hasSpecial = false;
125
126 for (;;) {
127 const first = specifiers[0];
128
129 if (isExportDefaultSpecifier(first) || isExportNamespaceSpecifier(first)) {
130 hasSpecial = true;
131 this.print(specifiers.shift(), node);
132
133 if (specifiers.length) {
134 this.token(",");
135 this.space();
136 }
137 } else {
138 break;
139 }
140 }
141
142 if (specifiers.length || !specifiers.length && !hasSpecial) {
143 this.token("{");
144
145 if (specifiers.length) {
146 this.space();
147 this.printList(specifiers, node);
148 this.space();
149 }
150
151 this.token("}");
152 }
153
154 if (node.source) {
155 this.space();
156 this.word("from");
157 this.space();
158 this.print(node.source, node);
159 this.printAssertions(node);
160 }
161
162 this.semicolon();
163 }
164}
165
166function ImportDeclaration(node) {
167 this.word("import");
168 this.space();
169
170 if (node.importKind === "type" || node.importKind === "typeof") {
171 this.word(node.importKind);
172 this.space();
173 }
174
175 const specifiers = node.specifiers.slice(0);
176
177 if (specifiers != null && specifiers.length) {
178 for (;;) {
179 const first = specifiers[0];
180
181 if (isImportDefaultSpecifier(first) || isImportNamespaceSpecifier(first)) {
182 this.print(specifiers.shift(), node);
183
184 if (specifiers.length) {
185 this.token(",");
186 this.space();
187 }
188 } else {
189 break;
190 }
191 }
192
193 if (specifiers.length) {
194 this.token("{");
195 this.space();
196 this.printList(specifiers, node);
197 this.space();
198 this.token("}");
199 }
200
201 this.space();
202 this.word("from");
203 this.space();
204 }
205
206 this.print(node.source, node);
207 this.printAssertions(node);
208 {
209 var _node$attributes;
210
211 if ((_node$attributes = node.attributes) != null && _node$attributes.length) {
212 this.space();
213 this.word("with");
214 this.space();
215 this.printList(node.attributes, node);
216 }
217 }
218 this.semicolon();
219}
220
221function ImportAttribute(node) {
222 this.print(node.key);
223 this.token(":");
224 this.space();
225 this.print(node.value);
226}
227
228function ImportNamespaceSpecifier(node) {
229 this.token("*");
230 this.space();
231 this.word("as");
232 this.space();
233 this.print(node.local, node);
234}
\No newline at end of file