Skip to content

Commit

Permalink
fix(bug): refined handling of default exports of unnamed classes
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed Apr 17, 2019
1 parent 702e108 commit 0782f57
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ export function traceIdentifiersForImportDeclaration({
}: TraceIdentifiersVisitorOptions<ImportDeclaration>): void {
if (node.importClause != null) {
if (node.importClause.name != null) {
if (!isIdentifierFree(node.importClause.name.text)) {
updateIdentifierName(node.importClause.name.text, generateUniqueVariableName(node.importClause.name.text));
} else {
addIdentifier(node.importClause.name.text);
}
addIdentifier(node.importClause.name.text);
}

if (node.importClause.namedBindings != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {ClassDeclaration, updateClassDeclaration} from "typescript";
import {ClassDeclaration, updateClassDeclaration, createIdentifier, Identifier} from "typescript";
import {UpdateExportsVisitorOptions} from "../update-exports-visitor-options";
import {hasDefaultExportModifier, hasExportModifier, removeExportModifier} from "../../util/modifier/modifier-util";
import {pascalCase} from "@wessberg/stringutil";
import {stripExtension} from "../../../../../util/path/path-util";
import {basename} from "path";

/**
* Visits the given ClassDeclaration.
Expand All @@ -19,22 +22,28 @@ export function visitClassDeclaration({
// If the node has no export modifier, leave it as it is
if (!hasExportModifier(node)) return continuation(node);

let name: Identifier | undefined = node.name;

// If the node is located in the entry file, leave it as it is - completely
if (isEntry) {
if (!hasDefaultExportModifier(node.modifiers)) {
exportedSpecifiersFromModule.add(node.name!.text);
if (!hasDefaultExportModifier(node.modifiers) && name != null) {
exportedSpecifiersFromModule.add(name.text);
}
return continuation(node);
}

// If the node has a default export, mark it as the identifier for the default export of that module
if (hasDefaultExportModifier(node.modifiers)) {
identifiersForDefaultExportsForModules.set(sourceFile.fileName, [node.name!.text, node]);
} else {
if (name == null) {
name = createIdentifier(`Default${pascalCase(stripExtension(basename(sourceFile.fileName)))}Export`);
}
// Compute a name for it. It must have one
identifiersForDefaultExportsForModules.set(sourceFile.fileName, [name.text, node]);
} else if (name != null) {
// Add the node name to the exported symbols
parsedExportedSymbols.set(node.name!.text, node);
parsedExportedSymbols.set(name.text, node);
}

// Update the node and remove the export modifiers from it
return continuation(updateClassDeclaration(node, node.decorators, removeExportModifier(node.modifiers), node.name, node.typeParameters, node.heritageClauses, node.members));
return continuation(updateClassDeclaration(node, node.decorators, removeExportModifier(node.modifiers), name, node.typeParameters, node.heritageClauses, node.members));
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ export function visitExportDeclaration({
if (node.exportClause != null) {
for (const element of node.exportClause.elements) {
const ref = element.propertyName != null ? element.propertyName : element.name;

const declaration = getAliasedDeclaration(ref, typeChecker);
if (declaration != null) {
parsedExportedSymbols.set(ref.text, (declaration as unknown) as DeclarationStatement);
element.name.text === "default"
? identifiersForDefaultExportsForModules.set(sourceFile.fileName, [ref.text, declaration])
: parsedExportedSymbols.set(ref.text, (declaration as unknown) as DeclarationStatement);
}
}
}
Expand Down

0 comments on commit 0782f57

Please sign in to comment.