Skip to content

Commit

Permalink
Release 3.2.7; Fix comments not exported from unexported namespace (#25)
Browse files Browse the repository at this point in the history
* Fix comments not exported from unexported namespace

* Bump
  • Loading branch information
shrinktofit committed May 5, 2023
1 parent 02c8d94 commit 3ad1e8b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 43 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tfig",
"version": "3.2.6-alpha.3",
"version": "3.2.7",
"description": "Yet another tool to generate .d.ts bundle.",
"main": "build/gift.js",
"types": "build/gift.d.ts",
Expand Down
74 changes: 33 additions & 41 deletions source/recast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,14 @@ export function recastTopLevelModule({
} else if (ts.isTypeAliasDeclaration(statement)) {
return !statement.name ? null : recastTypeAliasDeclaration(statement, statement.name.text, false);
} else if (ts.isVariableStatement(statement)) {
return nodeFactor.createVariableStatement(
return copyComments(statement, nodeFactor.createVariableStatement(
recastDeclarationModifiers(statement, false),
nodeFactor.createVariableDeclarationList(
statement.declarationList.declarations.map((declaration) =>
recastVariableDeclaration(declaration, declaration.name.getText(), false)),
statement.declarationList.flags,
),
);
));
} else if (ts.isImportDeclaration(statement)) {
return recastImportDeclaration(statement);
} else {
Expand All @@ -253,11 +253,28 @@ export function recastTopLevelModule({
}

function recastDeclaration(declaration: ts.Declaration, newName: string, forceExport: boolean): ts.Statement | null {
const result = recastDeclarationNoComment(declaration, newName, forceExport);
if (result) {
copyComments(declaration, result);
if (ts.isClassDeclaration(declaration)) {
return recastClassDeclaration(declaration, newName, forceExport);
} else if (ts.isFunctionDeclaration(declaration)) {
return recastFunctionDeclaration(declaration, newName, forceExport);
} else if (ts.isInterfaceDeclaration(declaration)) {
return recastInterfaceDeclaration(declaration, newName, forceExport);
} else if (ts.isEnumDeclaration(declaration)) {
return recastEnumDeclaration(declaration, newName, forceExport);
} else if (ts.isTypeAliasDeclaration(declaration)) {
return recastTypeAliasDeclaration(declaration, newName, forceExport);
} else if (ts.isVariableDeclaration(declaration)) {
return copyComments(declaration, nodeFactor.createVariableStatement(
recastDeclarationModifiers(declaration, forceExport),
nodeFactor.createVariableDeclarationList(
[recastVariableDeclaration(declaration, newName, forceExport)],
declaration.parent.flags,
),
));
} else if (ts.isModuleDeclaration(declaration)) {
// return recastModuleDeclaration(declaration, newName);
}
return result;
return null;
}

function copyComments<T extends ts.Node>(src: ts.Node, dst: T): T {
Expand Down Expand Up @@ -292,31 +309,6 @@ export function recastTopLevelModule({
return dst;
}

function recastDeclarationNoComment(declaration: ts.Declaration, newName: string, forceExport: boolean): ts.Statement | null {
if (ts.isClassDeclaration(declaration)) {
return recastClassDeclaration(declaration, newName, forceExport);
} else if (ts.isFunctionDeclaration(declaration)) {
return recastFunctionDeclaration(declaration, newName, forceExport);
} else if (ts.isInterfaceDeclaration(declaration)) {
return recastInterfaceDeclaration(declaration, newName, forceExport);
} else if (ts.isEnumDeclaration(declaration)) {
return recastEnumDeclaration(declaration, newName, forceExport);
} else if (ts.isTypeAliasDeclaration(declaration)) {
return recastTypeAliasDeclaration(declaration, newName, forceExport);
} else if (ts.isVariableDeclaration(declaration)) {
return nodeFactor.createVariableStatement(
recastDeclarationModifiers(declaration, forceExport),
nodeFactor.createVariableDeclarationList(
[recastVariableDeclaration(declaration, newName, forceExport)],
declaration.parent.flags,
),
);
} else if (ts.isModuleDeclaration(declaration)) {
// return recastModuleDeclaration(declaration, newName);
}
return null;
}

function recastSourceFileDeclarationAsNamespaceDeclaration(sourceFile: ts.SourceFile, newName: string) {
const newBody = nodeFactor.createModuleBlock(recastStatements(sourceFile.statements));
return nodeFactor.createModuleDeclaration(
Expand Down Expand Up @@ -351,7 +343,7 @@ export function recastTopLevelModule({
}

function recastFunctionDeclaration(functionDeclaration: ts.FunctionDeclaration, newName: string, forceExport: boolean) {
return nodeFactor.createFunctionDeclaration(
return copyComments(functionDeclaration, nodeFactor.createFunctionDeclaration(
undefined,
recastModifiers(functionDeclaration.modifiers),
functionDeclaration.asteriskToken,
Expand All @@ -360,7 +352,7 @@ export function recastTopLevelModule({
recastParameterArray(functionDeclaration.parameters), // parameters
recastTypeNode(functionDeclaration.type),
undefined,
);
));
}

function recastVariableDeclaration(variableDeclaration: ts.VariableDeclaration, newName: string, forceExport: boolean) {
Expand Down Expand Up @@ -588,14 +580,14 @@ export function recastTopLevelModule({
console.warn(`Don't know how to handle element ${element.name?.getText()} of class ${newName}`);
}
}
return nodeFactor.createClassDeclaration(
return copyComments(classDeclaration, nodeFactor.createClassDeclaration(
undefined,
recastDeclarationModifiers(classDeclaration, forceExport),
newName,
recastTypeParameterArray(classDeclaration.typeParameters),
recastHeritageClauses(classDeclaration.heritageClauses),
classElements,
);
));
}

function isPrivateMember(classElement: ts.ClassElement) {
Expand All @@ -607,14 +599,14 @@ export function recastTopLevelModule({

function recastInterfaceDeclaration(
interfaceDeclaration: ts.InterfaceDeclaration, newName: string, forceExport: boolean) {
return nodeFactor.createInterfaceDeclaration(
return copyComments(interfaceDeclaration, nodeFactor.createInterfaceDeclaration(
undefined,
recastDeclarationModifiers(interfaceDeclaration, forceExport),
newName,
recastTypeParameterArray(interfaceDeclaration.typeParameters),
recastHeritageClauses(interfaceDeclaration.heritageClauses),
recastTypeElements(interfaceDeclaration.members),
);
));
}

function recastTypeElement(typeElement: ts.TypeElement) {
Expand Down Expand Up @@ -677,7 +669,7 @@ export function recastTopLevelModule({
}

function recastEnumDeclaration(enumDeclaration: ts.EnumDeclaration, newName: string, forceExport: boolean) {
return nodeFactor.createEnumDeclaration(
return copyComments(enumDeclaration, nodeFactor.createEnumDeclaration(
undefined,
recastDeclarationModifiers(enumDeclaration, forceExport),
newName,
Expand All @@ -687,18 +679,18 @@ export function recastTopLevelModule({
recastExpression(enumerator.initializer),
));
}),
);
));
}

function recastTypeAliasDeclaration(
typeAliasDeclaration: ts.TypeAliasDeclaration, newName: string, forceExport: boolean) {
return nodeFactor.createTypeAliasDeclaration(
return copyComments(typeAliasDeclaration, nodeFactor.createTypeAliasDeclaration(
undefined,
recastDeclarationModifiers(typeAliasDeclaration, forceExport),
newName,
recastTypeParameterArray(typeAliasDeclaration.typeParameters),
recastTypeNode(typeAliasDeclaration.type)!,
);
));
}

function recastModifiers(modifiers: ts.NodeArray<ts.Modifier>): ts.Modifier[];
Expand Down
26 changes: 26 additions & 0 deletions test/comments/__snapshots__/comments.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ exports[`Type alias 1`] = `
* Namespace2.
*/
export namespace Ns { }
/**
* Var referencing unexported namespace.
*/
export const varReferencingUnexportedNamespace: Function & typeof __private._foo__UnexportedNamespace;
export namespace __private {
export namespace _foo__UnexportedNamespace {
/**
* Unexported namespace member function.
*/
export const memberVar: number;
/**
* Unexported namespace member function.
*/
export function memberFunction(): void;
/**
* Unexported namespace member class.
*/
export class MemberClass {
}
/**
* Unexported namespace member interface.
*/
export interface MemberInterface {
}
}
}
export {};
}
"
Expand Down
30 changes: 30 additions & 0 deletions test/comments/input.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,36 @@ declare module "foo" {
*/
export namespace Ns {
}

/**
* Var referencing unexported namespace.
*/
export const varReferencingUnexportedNamespace: Function & typeof UnexportedNamespace;

/**
* Unexported namespace.
*/
namespace UnexportedNamespace {
/**
* Unexported namespace member function.
*/
export const memberVar: number;

/**
* Unexported namespace member function.
*/
export function memberFunction(): void;

/**
* Unexported namespace member class.
*/
export class MemberClass { }

/**
* Unexported namespace member interface.
*/
export interface MemberInterface { }
}
}

declare module "bar" {
Expand Down

0 comments on commit 3ad1e8b

Please sign in to comment.