Skip to content

Commit

Permalink
[Refactor] ExportMap: add exports Map
Browse files Browse the repository at this point in the history
  • Loading branch information
whitneyit committed May 2, 2024
1 parent c0ac54b commit e1bd0ba
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/exportMap/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export default class ExportMapBuilder {
&& exportMap.namespace.size > 0 // anything is exported
&& !exportMap.namespace.has('default') // and default isn't added already
) {
exportMap.exports.set('default', {});
exportMap.namespace.set('default', {}); // add default export
}

Expand Down
1 change: 1 addition & 0 deletions src/exportMap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class ExportMap {
* @type {Map<string, () => ExportMap>}
*/
this.imports = new Map();
this.exports = new Map();
this.errors = [];
/**
* type {'ambiguous' | 'Module' | 'Script'}
Expand Down
3 changes: 3 additions & 0 deletions src/exportMap/specifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ export default function processSpecifier(specifier, astNode, exportMap, namespac
local = 'default';
break;
case 'ExportNamespaceSpecifier':
exportMap.exports.set(specifier.exported.name, astNode);
exportMap.namespace.set(specifier.exported.name, Object.defineProperty(exportMeta, 'namespace', {
get() { return namespace.resolveImport(nsource); },
}));
return;
case 'ExportAllDeclaration':
exportMap.exports.set(specifier.exported.name || specifier.exported.value, astNode);
exportMap.namespace.set(specifier.exported.name || specifier.exported.value, namespace.add(exportMeta, specifier.source.value));
return;
case 'ExportSpecifier':
if (!astNode.source) {
exportMap.exports.set(specifier.exported.name || specifier.exported.value, astNode);
exportMap.namespace.set(specifier.exported.name || specifier.exported.value, namespace.add(exportMeta, specifier.local));
return;
}
Expand Down
25 changes: 16 additions & 9 deletions src/exportMap/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default class ImportExportVisitorBuilder {
if (astNode.declaration.type === 'Identifier') {
this.namespace.add(exportMeta, astNode.declaration);
}
this.exportMap.exports.set('default', astNode);
this.exportMap.namespace.set('default', exportMeta);
},
ExportAllDeclaration() {
Expand Down Expand Up @@ -86,13 +87,17 @@ export default class ImportExportVisitorBuilder {
case 'TSInterfaceDeclaration':
case 'TSAbstractClassDeclaration':
case 'TSModuleDeclaration':
this.exportMap.exports.set(astNode.declaration.id.name, astNode);
this.exportMap.namespace.set(astNode.declaration.id.name, captureDoc(this.source, this.docStyleParsers, astNode));
break;
case 'VariableDeclaration':
astNode.declaration.declarations.forEach((d) => {
recursivePatternCapture(
d.id,
(id) => this.exportMap.namespace.set(id.name, captureDoc(this.source, this.docStyleParsers, d, astNode)),
(id) => {
this.exportMap.exports.set(id.name, astNode);
this.exportMap.namespace.set(id.name, captureDoc(this.source, this.docStyleParsers, d, astNode));
},
);
});
break;
Expand Down Expand Up @@ -126,18 +131,21 @@ export default class ImportExportVisitorBuilder {
));
if (exportedDecls.length === 0) {
// Export is not referencing any local declaration, must be re-exporting
this.exportMap.exports.set('default', astNode);
this.exportMap.namespace.set('default', captureDoc(this.source, this.docStyleParsers, astNode));
return;
}
if (
this.isEsModuleInteropTrue // esModuleInterop is on in tsconfig
&& !this.exportMap.namespace.has('default') // and default isn't added already
) {
this.exportMap.exports.set('default', {}); // add default export
this.exportMap.namespace.set('default', {}); // add default export
}
exportedDecls.forEach((decl) => {
if (decl.type === 'TSModuleDeclaration') {
if (decl.body && decl.body.type === 'TSModuleDeclaration') {
this.exportMap.exports.set(decl.body.id.name, astNode);
this.exportMap.namespace.set(decl.body.id.name, captureDoc(this.source, this.docStyleParsers, decl.body));
} else if (decl.body && decl.body.body) {
decl.body.body.forEach((moduleBlockNode) => {
Expand All @@ -150,20 +158,19 @@ export default class ImportExportVisitorBuilder {
if (!namespaceDecl) {
// TypeScript can check this for us; we needn't
} else if (namespaceDecl.type === 'VariableDeclaration') {
namespaceDecl.declarations.forEach((d) => recursivePatternCapture(d.id, (id) => this.exportMap.namespace.set(
id.name,
captureDoc(this.source, this.docStyleParsers, decl, namespaceDecl, moduleBlockNode),
)),
);
namespaceDecl.declarations.forEach((d) => recursivePatternCapture(d.id, (id) => {
this.exportMap.exports.set(id.name, astNode);
this.exportMap.namespace.set(id.name, captureDoc(this.source, this.docStyleParsers, decl, namespaceDecl, moduleBlockNode));
}));
} else {
this.exportMap.namespace.set(
namespaceDecl.id.name,
captureDoc(this.source, this.docStyleParsers, moduleBlockNode));
this.exportMap.exports.set(namespaceDecl.id.name, astNode);
this.exportMap.namespace.set(namespaceDecl.id.name, captureDoc(this.source, this.docStyleParsers, moduleBlockNode));
}
});
}
} else {
// Export as default
this.exportMap.exports.set('default', {});
this.exportMap.namespace.set('default', captureDoc(this.source, this.docStyleParsers, decl));
}
});
Expand Down

0 comments on commit e1bd0ba

Please sign in to comment.