Skip to content

Commit

Permalink
extract method
Browse files Browse the repository at this point in the history
  • Loading branch information
soryy708 committed Mar 27, 2024
1 parent 6a59ce0 commit 586a696
Showing 1 changed file with 72 additions and 73 deletions.
145 changes: 72 additions & 73 deletions src/exportMap/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,81 +39,17 @@ export class ImportExportVisitorBuilder {
this.ast = ast;
this.isEsModuleInteropTrue = isEsModuleInteropTrue;
this.thunkFor = thunkFor;
}

build(astNode) {
const docstyle = this.context.settings && this.context.settings['import/docstyle'] || ['jsdoc'];
const docStyleParsers = {};
this.docStyleParsers = {};
docstyle.forEach((style) => {
docStyleParsers[style] = availableDocStyleParsers[style];
this.docStyleParsers[style] = availableDocStyleParsers[style];
});
}

// This doesn't declare anything, but changes what's being exported.
function typeScriptExport() {
const exportedName = astNode.type === 'TSNamespaceExportDeclaration'
? (astNode.id || astNode.name).name
: astNode.expression && astNode.expression.name || astNode.expression.id && astNode.expression.id.name || null;
const declTypes = [
'VariableDeclaration',
'ClassDeclaration',
'TSDeclareFunction',
'TSEnumDeclaration',
'TSTypeAliasDeclaration',
'TSInterfaceDeclaration',
'TSAbstractClassDeclaration',
'TSModuleDeclaration',
];
const exportedDecls = this.ast.body.filter(({ type, id, declarations }) => includes(declTypes, type) && (
id && id.name === exportedName || declarations && declarations.find((d) => d.id.name === exportedName)
));
if (exportedDecls.length === 0) {
// Export is not referencing any local declaration, must be re-exporting
this.exportMap.namespace.set('default', captureDoc(this.source, docStyleParsers, astNode));
return;
}
if (
this.isEsModuleInteropTrue // esModuleInterop is on in tsconfig
&& !this.exportMap.namespace.has('default') // and default isn't added already
) {
this.exportMap.namespace.set('default', {}); // add default export
}
exportedDecls.forEach((decl) => {
if (decl.type === 'TSModuleDeclaration') {
if (decl.body && decl.body.type === 'TSModuleDeclaration') {
this.exportMap.namespace.set(decl.body.id.name, captureDoc(this.source, docStyleParsers, decl.body));
} else if (decl.body && decl.body.body) {
decl.body.body.forEach((moduleBlockNode) => {
// Export-assignment exports all members in the namespace,
// explicitly exported or not.
const namespaceDecl = moduleBlockNode.type === 'ExportNamedDeclaration'
? moduleBlockNode.declaration
: moduleBlockNode;

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, docStyleParsers, decl, namespaceDecl, moduleBlockNode),
)),
);
} else {
this.exportMap.namespace.set(
namespaceDecl.id.name,
captureDoc(this.source, docStyleParsers, moduleBlockNode));
}
});
}
} else {
// Export as default
this.exportMap.namespace.set('default', captureDoc(this.source, docStyleParsers, decl));
}
});
}

build(astNode) {
return {

Check warning on line 50 in src/exportMap/visitor.js

View check run for this annotation

Codecov / codecov/patch

src/exportMap/visitor.js#L50

Added line #L50 was not covered by tests
ExportDefaultDeclaration() {
const exportMeta = captureDoc(this.source, docStyleParsers, astNode);
const exportMeta = captureDoc(this.source, this.docStyleParsers, astNode);
if (astNode.declaration.type === 'Identifier') {
this.namespace.add(exportMeta, astNode.declaration);
}
Expand Down Expand Up @@ -150,13 +86,13 @@ export class ImportExportVisitorBuilder {
case 'TSInterfaceDeclaration':
case 'TSAbstractClassDeclaration':
case 'TSModuleDeclaration':
this.exportMap.namespace.set(astNode.declaration.id.name, captureDoc(this.source, docStyleParsers, astNode));
this.exportMap.namespace.set(astNode.declaration.id.name, captureDoc(this.source, this.docStyleParsers, astNode));
break;
case 'VariableDeclaration':
astNode.declaration.declarations.forEach((d) => {
recursivePatternCapture(

Check warning on line 93 in src/exportMap/visitor.js

View check run for this annotation

Codecov / codecov/patch

src/exportMap/visitor.js#L93

Added line #L93 was not covered by tests
d.id,
(id) => this.exportMap.namespace.set(id.name, captureDoc(this.source, docStyleParsers, d, astNode)),
(id) => this.exportMap.namespace.set(id.name, captureDoc(this.source, this.docStyleParsers, d, astNode)),
);
});
break;
Expand All @@ -165,8 +101,71 @@ export class ImportExportVisitorBuilder {
}
astNode.specifiers.forEach((s) => processSpecifier(s, astNode, this.exportMap, this.namespace));
},
TSExportAssignment: typeScriptExport,
...this.isEsModuleInteropTrue && { TSNamespaceExportDeclaration: typeScriptExport },
TSExportAssignment: () => this.typeScriptExport(astNode),
...this.isEsModuleInteropTrue && { TSNamespaceExportDeclaration: () => this.typeScriptExport(astNode) },
};
}

// This doesn't declare anything, but changes what's being exported.
typeScriptExport(astNode) {

Check warning on line 110 in src/exportMap/visitor.js

View check run for this annotation

Codecov / codecov/patch

src/exportMap/visitor.js#L110

Added line #L110 was not covered by tests
const exportedName = astNode.type === 'TSNamespaceExportDeclaration'
? (astNode.id || astNode.name).name
: astNode.expression && astNode.expression.name || astNode.expression.id && astNode.expression.id.name || null;
const declTypes = [

Check warning on line 114 in src/exportMap/visitor.js

View check run for this annotation

Codecov / codecov/patch

src/exportMap/visitor.js#L114

Added line #L114 was not covered by tests
'VariableDeclaration',
'ClassDeclaration',
'TSDeclareFunction',
'TSEnumDeclaration',
'TSTypeAliasDeclaration',
'TSInterfaceDeclaration',
'TSAbstractClassDeclaration',
'TSModuleDeclaration',
];
const exportedDecls = this.ast.body.filter(({ type, id, declarations }) => includes(declTypes, type) && (
id && id.name === exportedName || declarations && declarations.find((d) => d.id.name === exportedName)
));
if (exportedDecls.length === 0) {
// Export is not referencing any local declaration, must be re-exporting
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

Check warning on line 134 in src/exportMap/visitor.js

View check run for this annotation

Codecov / codecov/patch

src/exportMap/visitor.js#L134

Added line #L134 was not covered by tests
) {
this.exportMap.namespace.set('default', {}); // add default export
}
exportedDecls.forEach((decl) => {
if (decl.type === 'TSModuleDeclaration') {
if (decl.body && decl.body.type === 'TSModuleDeclaration') {
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) => {
// Export-assignment exports all members in the namespace,
// explicitly exported or not.
const namespaceDecl = moduleBlockNode.type === 'ExportNamedDeclaration'
? moduleBlockNode.declaration
: moduleBlockNode;

if (!namespaceDecl) {
// TypeScript can check this for us; we needn't
} else if (namespaceDecl.type === 'VariableDeclaration') {

Check warning on line 152 in src/exportMap/visitor.js

View check run for this annotation

Codecov / codecov/patch

src/exportMap/visitor.js#L152

Added line #L152 was not covered by tests
namespaceDecl.declarations.forEach((d) => recursivePatternCapture(d.id, (id) => 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));
}
});
}
} else {
// Export as default
this.exportMap.namespace.set('default', captureDoc(this.source, this.docStyleParsers, decl));
}
});
}
}

0 comments on commit 586a696

Please sign in to comment.