Debian Codegraph @

packages/babel-generator/src/generators/modules.ts

raw symbols SCIP
1 import type Printer from "../printer";
2 import {
3 isClassDeclaration,
4 isExportDefaultSpecifier,
5 isExportNamespaceSpecifier,
6 isImportDefaultSpecifier,
7 isImportNamespaceSpecifier,
8 isStatement,
9 } from "@babel/types";
10 import type * as t from "@babel/types";
11
12 export function ImportSpecifier(this: Printer, node: t.ImportSpecifier) {
13 if (node.importKind === "type" || node.importKind === "typeof") {
14 this.word(node.importKind);
15 this.space();
16 }
17
18 this.print(node.imported, node);
19 // @ts-expect-error todo(flow-ts) maybe check node type instead of relying on name to be undefined on t.StringLiteral
20 if (node.local && node.local.name !== node.imported.name) {
21 this.space();
22 this.word("as");
23 this.space();
24 this.print(node.local, node);
25 }
26 }
27
28 export function ImportDefaultSpecifier(
29 this: Printer,
30 node: t.ImportDefaultSpecifier,
31 ) {
32 this.print(node.local, node);
33 }
34
35 export function ExportDefaultSpecifier(
36 this: Printer,
37 node: t.ExportDefaultSpecifier,
38 ) {
39 this.print(node.exported, node);
40 }
41
42 export function ExportSpecifier(this: Printer, node: t.ExportSpecifier) {
43 if (node.exportKind === "type") {
44 this.word("type");
45 this.space();
46 }
47
48 this.print(node.local, node);
49 // @ts-expect-error todo(flow-ts) maybe check node type instead of relying on name to be undefined on t.StringLiteral
50 if (node.exported && node.local.name !== node.exported.name) {
51 this.space();
52 this.word("as");
53 this.space();
54 this.print(node.exported, node);
55 }
56 }
57
58 export function ExportNamespaceSpecifier(
59 this: Printer,
60 node: t.ExportNamespaceSpecifier,
61 ) {
62 this.token("*");
63 this.space();
64 this.word("as");
65 this.space();
66 this.print(node.exported, node);
67 }
68
69 export function _printAssertions(
70 this: Printer,
71 node: Extract<t.Node, { assertions?: t.ImportAttribute[] }>,
72 ) {
73 this.word("assert");
74 this.space();
75 this.token("{");
76 this.space();
77 this.printList(node.assertions, node);
78 this.space();
79 this.token("}");
80 }
81
82 export function ExportAllDeclaration(
83 this: Printer,
84 node: t.ExportAllDeclaration | t.DeclareExportAllDeclaration,
85 ) {
86 this.word("export");
87 this.space();
88 if (node.exportKind === "type") {
89 this.word("type");
90 this.space();
91 }
92 this.token("*");
93 this.space();
94 this.word("from");
95 this.space();
96 // @ts-expect-error Fixme: assertions is not defined in DeclareExportAllDeclaration
97 if (node.assertions?.length) {
98 this.print(node.source, node, true);
99 this.space();
100 // @ts-expect-error Fixme: assertions is not defined in DeclareExportAllDeclaration
101 this._printAssertions(node);
102 } else {
103 this.print(node.source, node);
104 }
105
106 this.semicolon();
107 }
108
109 export function ExportNamedDeclaration(
110 this: Printer,
111 node: t.ExportNamedDeclaration,
112 ) {
113 if (!process.env.BABEL_8_BREAKING) {
114 if (
115 this.format.decoratorsBeforeExport &&
116 isClassDeclaration(node.declaration)
117 ) {
118 this.printJoin(node.declaration.decorators, node);
119 }
120 }
121
122 this.word("export");
123 this.space();
124 if (node.declaration) {
125 const declar = node.declaration;
126 this.print(declar, node);
127 if (!isStatement(declar)) this.semicolon();
128 } else {
129 if (node.exportKind === "type") {
130 this.word("type");
131 this.space();
132 }
133
134 const specifiers = node.specifiers.slice(0);
135
136 // print "special" specifiers first
137 let hasSpecial = false;
138 for (;;) {
139 const first = specifiers[0];
140 if (
141 isExportDefaultSpecifier(first) ||
142 isExportNamespaceSpecifier(first)
143 ) {
144 hasSpecial = true;
145 this.print(specifiers.shift(), node);
146 if (specifiers.length) {
147 this.token(",");
148 this.space();
149 }
150 } else {
151 break;
152 }
153 }
154
155 if (specifiers.length || (!specifiers.length && !hasSpecial)) {
156 this.token("{");
157 if (specifiers.length) {
158 this.space();
159 this.printList(specifiers, node);
160 this.space();
161 }
162 this.token("}");
163 }
164
165 if (node.source) {
166 this.space();
167 this.word("from");
168 this.space();
169 if (node.assertions?.length) {
170 this.print(node.source, node, true);
171 this.space();
172 this._printAssertions(node);
173 } else {
174 this.print(node.source, node);
175 }
176 }
177
178 this.semicolon();
179 }
180 }
181
182 export function ExportDefaultDeclaration(
183 this: Printer,
184 node: t.ExportDefaultDeclaration,
185 ) {
186 if (!process.env.BABEL_8_BREAKING) {
187 if (
188 this.format.decoratorsBeforeExport &&
189 isClassDeclaration(node.declaration)
190 ) {
191 this.printJoin(node.declaration.decorators, node);
192 }
193 }
194
195 this.word("export");
196 this.noIndentInnerCommentsHere();
197 this.space();
198 this.word("default");
199 this.space();
200 const declar = node.declaration;
201 this.print(declar, node);
202 if (!isStatement(declar)) this.semicolon();
203 }
204
205 export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) {
206 this.word("import");
207 this.space();
208
209 const isTypeKind = node.importKind === "type" || node.importKind === "typeof";
210 if (isTypeKind) {
211 this.noIndentInnerCommentsHere();
212 this.word(node.importKind);
213 this.space();
214 } else if (node.module) {
215 this.noIndentInnerCommentsHere();
216 this.word("module");
217 this.space();
218 }
219
220 const specifiers = node.specifiers.slice(0);
221 const hasSpecifiers = !!specifiers.length;
222 // print "special" specifiers first. The loop condition is constant,
223 // but there is a "break" in the body.
224 while (hasSpecifiers) {
225 const first = specifiers[0];
226 if (isImportDefaultSpecifier(first) || isImportNamespaceSpecifier(first)) {
227 this.print(specifiers.shift(), node);
228 if (specifiers.length) {
229 this.token(",");
230 this.space();
231 }
232 } else {
233 break;
234 }
235 }
236
237 if (specifiers.length) {
238 this.token("{");
239 this.space();
240 this.printList(specifiers, node);
241 this.space();
242 this.token("}");
243 } else if (isTypeKind && !hasSpecifiers) {
244 this.token("{");
245 this.token("}");
246 }
247
248 if (hasSpecifiers || isTypeKind) {
249 this.space();
250 this.word("from");
251 this.space();
252 }
253
254 if (node.assertions?.length) {
255 this.print(node.source, node, true);
256 this.space();
257 this._printAssertions(node);
258 } else {
259 this.print(node.source, node);
260 }
261 if (!process.env.BABEL_8_BREAKING) {
262 // @ts-ignore(Babel 7 vs Babel 8) Babel 7 supports module attributes
263 if (node.attributes?.length) {
264 this.space();
265 this.word("with");
266 this.space();
267 // @ts-ignore(Babel 7 vs Babel 8) Babel 7 supports module attributes
268 this.printList(node.attributes, node);
269 }
270 }
271
272 this.semicolon();
273 }
274
275 export function ImportAttribute(this: Printer, node: t.ImportAttribute) {
276 this.print(node.key);
277 this.token(":");
278 this.space();
279 this.print(node.value);
280 }
281
282 export function ImportNamespaceSpecifier(
283 this: Printer,
284 node: t.ImportNamespaceSpecifier,
285 ) {
286 this.token("*");
287 this.space();
288 this.word("as");
289 this.space();
290 this.print(node.local, node);
291 }