Skip to content

Commit d0231c0

Browse files
whitneyitljharb
authored andcommitted
[Refactor] ExportMap: add exports Map
1 parent 806e3c2 commit d0231c0

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

src/exportMap/builder.js

+1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ export default class ExportMapBuilder {
195195
&& exportMap.namespace.size > 0 // anything is exported
196196
&& !exportMap.namespace.has('default') // and default isn't added already
197197
) {
198+
exportMap.exports.set('default', {});
198199
exportMap.namespace.set('default', {}); // add default export
199200
}
200201

src/exportMap/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default class ExportMap {
1414
* @type {Map<string, () => ExportMap>}
1515
*/
1616
this.imports = new Map();
17+
this.exports = new Map();
1718
this.errors = [];
1819
/**
1920
* type {'ambiguous' | 'Module' | 'Script'}

src/exportMap/specifier.js

+3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ export default function processSpecifier(specifier, astNode, exportMap, namespac
99
local = 'default';
1010
break;
1111
case 'ExportNamespaceSpecifier':
12+
exportMap.exports.set(specifier.exported.name, astNode);
1213
exportMap.namespace.set(specifier.exported.name, Object.defineProperty(exportMeta, 'namespace', {
1314
get() { return namespace.resolveImport(nsource); },
1415
}));
1516
return;
1617
case 'ExportAllDeclaration':
18+
exportMap.exports.set(specifier.exported.name || specifier.exported.value, astNode);
1719
exportMap.namespace.set(specifier.exported.name || specifier.exported.value, namespace.add(exportMeta, specifier.source.value));
1820
return;
1921
case 'ExportSpecifier':
2022
if (!astNode.source) {
23+
exportMap.exports.set(specifier.exported.name || specifier.exported.value, astNode);
2124
exportMap.namespace.set(specifier.exported.name || specifier.exported.value, namespace.add(exportMeta, specifier.local));
2225
return;
2326
}

src/exportMap/visitor.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export default class ImportExportVisitorBuilder {
5353
if (astNode.declaration.type === 'Identifier') {
5454
this.namespace.add(exportMeta, astNode.declaration);
5555
}
56+
this.exportMap.exports.set('default', astNode);
5657
this.exportMap.namespace.set('default', exportMeta);
5758
},
5859
ExportAllDeclaration() {
@@ -86,13 +87,17 @@ export default class ImportExportVisitorBuilder {
8687
case 'TSInterfaceDeclaration':
8788
case 'TSAbstractClassDeclaration':
8889
case 'TSModuleDeclaration':
90+
this.exportMap.exports.set(astNode.declaration.id.name, astNode);
8991
this.exportMap.namespace.set(astNode.declaration.id.name, captureDoc(this.source, this.docStyleParsers, astNode));
9092
break;
9193
case 'VariableDeclaration':
9294
astNode.declaration.declarations.forEach((d) => {
9395
recursivePatternCapture(
9496
d.id,
95-
(id) => this.exportMap.namespace.set(id.name, captureDoc(this.source, this.docStyleParsers, d, astNode)),
97+
(id) => {
98+
this.exportMap.exports.set(id.name, astNode);
99+
this.exportMap.namespace.set(id.name, captureDoc(this.source, this.docStyleParsers, d, astNode));
100+
},
96101
);
97102
});
98103
break;
@@ -126,18 +131,21 @@ export default class ImportExportVisitorBuilder {
126131
));
127132
if (exportedDecls.length === 0) {
128133
// Export is not referencing any local declaration, must be re-exporting
134+
this.exportMap.exports.set('default', astNode);
129135
this.exportMap.namespace.set('default', captureDoc(this.source, this.docStyleParsers, astNode));
130136
return;
131137
}
132138
if (
133139
this.isEsModuleInteropTrue // esModuleInterop is on in tsconfig
134140
&& !this.exportMap.namespace.has('default') // and default isn't added already
135141
) {
142+
this.exportMap.exports.set('default', {}); // add default export
136143
this.exportMap.namespace.set('default', {}); // add default export
137144
}
138145
exportedDecls.forEach((decl) => {
139146
if (decl.type === 'TSModuleDeclaration') {
140147
if (decl.body && decl.body.type === 'TSModuleDeclaration') {
148+
this.exportMap.exports.set(decl.body.id.name, astNode);
141149
this.exportMap.namespace.set(decl.body.id.name, captureDoc(this.source, this.docStyleParsers, decl.body));
142150
} else if (decl.body && decl.body.body) {
143151
decl.body.body.forEach((moduleBlockNode) => {
@@ -150,20 +158,19 @@ export default class ImportExportVisitorBuilder {
150158
if (!namespaceDecl) {
151159
// TypeScript can check this for us; we needn't
152160
} else if (namespaceDecl.type === 'VariableDeclaration') {
153-
namespaceDecl.declarations.forEach((d) => recursivePatternCapture(d.id, (id) => this.exportMap.namespace.set(
154-
id.name,
155-
captureDoc(this.source, this.docStyleParsers, decl, namespaceDecl, moduleBlockNode),
156-
)),
157-
);
161+
namespaceDecl.declarations.forEach((d) => recursivePatternCapture(d.id, (id) => {
162+
this.exportMap.exports.set(id.name, astNode);
163+
this.exportMap.namespace.set(id.name, captureDoc(this.source, this.docStyleParsers, decl, namespaceDecl, moduleBlockNode));
164+
}));
158165
} else {
159-
this.exportMap.namespace.set(
160-
namespaceDecl.id.name,
161-
captureDoc(this.source, this.docStyleParsers, moduleBlockNode));
166+
this.exportMap.exports.set(namespaceDecl.id.name, astNode);
167+
this.exportMap.namespace.set(namespaceDecl.id.name, captureDoc(this.source, this.docStyleParsers, moduleBlockNode));
162168
}
163169
});
164170
}
165171
} else {
166172
// Export as default
173+
this.exportMap.exports.set('default', {});
167174
this.exportMap.namespace.set('default', captureDoc(this.source, this.docStyleParsers, decl));
168175
}
169176
});

0 commit comments

Comments
 (0)