Skip to content

Commit

Permalink
fix(bug): adds support for missing node types
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed Feb 25, 2019
1 parent 608d38d commit b5de5ee
Show file tree
Hide file tree
Showing 55 changed files with 26 additions and 1,096 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"@types/node": "^11.9.5",
"@types/resolve": "0.0.8",
"@wessberg/browserslist-generator": "1.0.11",
"@wessberg/stringutil": "^1.0.18",
"browserslist": "^4.4.1",
"find-up": "^3.0.0",
"magic-string": "^0.25.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@ export interface UpdateExportsVisitorOptions<T extends Node> extends VisitorOpti
getExportedSpecifiersFromModule(moduleName: string): Set<string>;
parsedExportedSymbols: Set<string>;
exportedSpecifiersFromModule: Set<string>;

isIdentifierFree(identifier: string): boolean;
rootLevelIdentifiersForModule: Set<string>;
generateUniqueVariableName(candidate: string): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ import {setExtension} from "../../../../util/path/path-util";
export function updateExports({usedExports, ...rest}: IDeclarationBundlerOptions): TransformerFactory<SourceFile> {
const parsedExportedSymbolsMap: Map<string, Set<string>> = new Map();
const exportedSpecifiersFromModuleMap: Map<string, Set<string>> = new Map();
const rootLevelIdentifiersForModuleMap: Map<string, Set<string>> = new Map();
const generatedVariableNamesForChunkMap: Map<string, Set<string>> = new Map();

return context => {
return sourceFile => {
Expand All @@ -49,14 +47,7 @@ export function updateExports({usedExports, ...rest}: IDeclarationBundlerOptions
const chunkFilename = getChunkFilename(sourceFile.fileName, rest.supportedExtensions, rest.chunkToOriginalFileMap);

let parsedExportedSymbols = parsedExportedSymbolsMap.get(sourceFile.fileName);
let rootLevelIdentifiersForModule = rootLevelIdentifiersForModuleMap.get(sourceFile.fileName);
let exportedSpecifiersFromModule = exportedSpecifiersFromModuleMap.get(sourceFile.fileName);
let generatedVariableNamesForChunk = generatedVariableNamesForChunkMap.get(chunkFilename);

if (rootLevelIdentifiersForModule == null) {
rootLevelIdentifiersForModule = new Set();
rootLevelIdentifiersForModuleMap.set(sourceFile.fileName, rootLevelIdentifiersForModule);
}

if (parsedExportedSymbols == null) {
parsedExportedSymbols = new Set();
Expand All @@ -68,11 +59,6 @@ export function updateExports({usedExports, ...rest}: IDeclarationBundlerOptions
exportedSpecifiersFromModuleMap.set(sourceFile.fileName, exportedSpecifiersFromModule);
}

if (generatedVariableNamesForChunk == null) {
generatedVariableNamesForChunk = new Set();
generatedVariableNamesForChunkMap.set(chunkFilename, generatedVariableNamesForChunk);
}

// Prepare some VisitorOptions
const visitorOptions = {
usedExports,
Expand All @@ -81,17 +67,6 @@ export function updateExports({usedExports, ...rest}: IDeclarationBundlerOptions
...rest,
continuation: <U extends Node>(node: U) => visitEachChild(node, visitor, context),

isIdentifierFree(identifier: string): boolean {
for (const module of rest.localModuleNames) {
// Skip the current module
if (module === sourceFile.fileName) continue;

const identifiersForModule = rootLevelIdentifiersForModuleMap.get(module);
if (identifiersForModule != null && identifiersForModule.has(identifier)) return false;
}
return true;
},

getParsedExportedSymbolsForModule(moduleName: string): Set<string> {
let matched: Set<string> | undefined;
for (const extension of ["", ...rest.supportedExtensions]) {
Expand Down Expand Up @@ -119,22 +94,7 @@ export function updateExports({usedExports, ...rest}: IDeclarationBundlerOptions
return matched;
},
parsedExportedSymbols,
rootLevelIdentifiersForModule,
exportedSpecifiersFromModule,
generateUniqueVariableName(candidate: string): string {
const suffix = "_$";
let counter = 0;

while (true) {
let currentCandidate = candidate + suffix + counter;
if (generatedVariableNamesForChunk!.has(currentCandidate)) {
counter++;
} else {
generatedVariableNamesForChunk!.add(currentCandidate);
return currentCandidate;
}
}
}
exportedSpecifiersFromModule
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ export function visitClassDeclaration({
isEntry,
exportedSpecifiersFromModule,
parsedExportedSymbols,
identifiersForDefaultExportsForModules,
rootLevelIdentifiersForModule
identifiersForDefaultExportsForModules
}: UpdateExportsVisitorOptions<ClassDeclaration>): ClassDeclaration | undefined {
if (node.name != null) {
rootLevelIdentifiersForModule.add(node.name.text);
}

// If the node has no export modifier, leave it as it is
if (!hasExportModifier(node)) return continuation(node);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ export function visitEnumDeclaration({
isEntry,
exportedSpecifiersFromModule,
parsedExportedSymbols,
identifiersForDefaultExportsForModules,
rootLevelIdentifiersForModule
identifiersForDefaultExportsForModules
}: UpdateExportsVisitorOptions<EnumDeclaration>): EnumDeclaration | undefined {
rootLevelIdentifiersForModule.add(node.name.text);

// If the node has no export modifier, leave it as it is
if (!hasExportModifier(node)) return continuation(node);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ export function visitFunctionDeclaration({
isEntry,
exportedSpecifiersFromModule,
parsedExportedSymbols,
identifiersForDefaultExportsForModules,
rootLevelIdentifiersForModule
identifiersForDefaultExportsForModules
}: UpdateExportsVisitorOptions<FunctionDeclaration>): FunctionDeclaration | undefined {
if (node.name != null) {
rootLevelIdentifiersForModule.add(node.name.text);
}

// If the node has no export modifier, leave it as it is
if (!hasExportModifier(node)) return continuation(node);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ export function visitInterfaceDeclaration({
isEntry,
exportedSpecifiersFromModule,
parsedExportedSymbols,
identifiersForDefaultExportsForModules,
rootLevelIdentifiersForModule
identifiersForDefaultExportsForModules
}: UpdateExportsVisitorOptions<InterfaceDeclaration>): InterfaceDeclaration | undefined {
rootLevelIdentifiersForModule.add(node.name.text);

// If the node has no export modifier, leave it as it is
if (!hasExportModifier(node)) return continuation(node);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ export function visitModuleDeclaration({
isEntry,
exportedSpecifiersFromModule,
parsedExportedSymbols,
identifiersForDefaultExportsForModules,
rootLevelIdentifiersForModule
identifiersForDefaultExportsForModules
}: UpdateExportsVisitorOptions<ModuleDeclaration>): ModuleDeclaration | undefined {
rootLevelIdentifiersForModule.add(node.name.text);

// If the node has no export modifier, leave it as it is
if (!hasExportModifier(node)) return continuation(node);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ export function visitTypeAliasDeclaration({
isEntry,
exportedSpecifiersFromModule,
parsedExportedSymbols,
identifiersForDefaultExportsForModules,
rootLevelIdentifiersForModule
identifiersForDefaultExportsForModules
}: UpdateExportsVisitorOptions<TypeAliasDeclaration>): TypeAliasDeclaration | undefined {
rootLevelIdentifiersForModule.add(node.name.text);

// If the node has no export modifier, leave it as it is
if (!hasExportModifier(node)) return continuation(node);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import {createVariableDeclaration, isIdentifier, updateVariableDeclarationList,
import {UpdateExportsVisitorOptions} from "../update-exports-visitor-options";
import {hasDefaultExportModifier, removeExportModifier} from "../../util/modifier/modifier-util";
import {getIdentifiersForBindingName} from "../../util/binding-name/get-identifiers-for-binding-name";
import {pascalCase} from "@wessberg/stringutil";
import {basename} from "path";
import {stripExtension} from "../../../../../util/path/path-util";

/**
* Visits the given VariableStatement.
Expand All @@ -15,18 +18,14 @@ export function visitVariableStatement({
isEntry,
identifiersForDefaultExportsForModules,
parsedExportedSymbols,
exportedSpecifiersFromModule,
generateUniqueVariableName,
rootLevelIdentifiersForModule
exportedSpecifiersFromModule
}: UpdateExportsVisitorOptions<VariableStatement>): VariableStatement | undefined {
for (const declaration of node.declarationList.declarations) {
// Add all of the named bindings to the exported symbols
for (const identifier of getIdentifiersForBindingName(declaration.name)) {
if (isEntry && !hasDefaultExportModifier(node.modifiers)) {
if (isEntry && !hasDefaultExportModifier(node.modifiers)) {
for (const declaration of node.declarationList.declarations) {
// Add all of the named bindings to the exported symbols
for (const identifier of getIdentifiersForBindingName(declaration.name)) {
exportedSpecifiersFromModule.add(identifier);
}

rootLevelIdentifiersForModule.add(identifier);
}
}

Expand All @@ -42,7 +41,7 @@ export function visitVariableStatement({
// If the name of the declaration is '_default', it is an assignment to a const before exporting it as default
if (isIdentifier(declaration.name) && declaration.name.text === "_default") {
// Give it a unique variable name and bind it to a new variable
const name = generateUniqueVariableName(declaration.name.text);
const name = `default${pascalCase(stripExtension(basename(sourceFile.fileName)))}Export`;
identifiersForDefaultExportsForModules.set(sourceFile.fileName, name);

return continuation(
Expand Down
Loading

0 comments on commit b5de5ee

Please sign in to comment.