Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/api-extractor/src/analyzer/AstSymbolTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ export class AstSymbolTable {
case ts.SyntaxKind.ExpressionWithTypeArguments: // special case for e.g. the "extends" keyword
case ts.SyntaxKind.ComputedPropertyName: // used for EcmaScript "symbols", e.g. "[toPrimitive]".
case ts.SyntaxKind.TypeQuery: // represents for "typeof X" as a type
case ts.SyntaxKind.ExportSpecifier: // NamedExports inside namespace, e.g. "export { A as B };", "export { A }"
{
// Sometimes the type reference will involve multiple identifiers, e.g. "a.b.C".
// In this case, we only need to worry about importing the first identifier,
Expand Down
23 changes: 23 additions & 0 deletions apps/api-extractor/src/generators/ApiReportGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,20 @@ export class ApiReportGenerator {
case ts.SyntaxKind.ExportKeyword:
case ts.SyntaxKind.DefaultKeyword:
case ts.SyntaxKind.DeclareKeyword:
if (astDeclaration.parent) {
// Keep it as is for nested declarations.
break;
}
if (
ts.isModuleDeclaration(astDeclaration.declaration) &&
(TypeScriptHelpers.findFirstParent(span.node, ts.SyntaxKind.ExportDeclaration) ||
TypeScriptHelpers.findFirstParent(span.node, ts.SyntaxKind.VariableStatement))
) {
// Keep it as is for nested declarations.
// (special cases inside namespace. e.g. "export {};", "export const a: number;")
break;
}

// Delete any explicit "export" or "declare" keywords -- we will re-add them below
span.modification.skipAll();
break;
Expand All @@ -314,6 +328,11 @@ export class ApiReportGenerator {
case ts.SyntaxKind.ModuleKeyword:
case ts.SyntaxKind.TypeKeyword:
case ts.SyntaxKind.FunctionKeyword:
if (astDeclaration.parent) {
// Keep it as is for nested declarations
break;
}

// Replace the stuff we possibly deleted above
let replacedModifiers: string = '';

Expand Down Expand Up @@ -373,6 +392,10 @@ export class ApiReportGenerator {
}
break;

case ts.SyntaxKind.ExportSpecifier:
DtsEmitHelpers.modifyExportSpecifierSpan(collector, span);
break;

case ts.SyntaxKind.Identifier:
const referencedEntity: CollectorEntity | undefined = collector.tryGetEntityForNode(
span.node as ts.Identifier
Expand Down
24 changes: 24 additions & 0 deletions apps/api-extractor/src/generators/DtsEmitHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ export class DtsEmitHelpers {
}
}

public static modifyExportSpecifierSpan(collector: Collector, span: Span): void {
// For "export { A }" (inside namespace), we need to emit as "export { AcutalName as A }"
const node: ts.ExportSpecifier = span.node as ts.ExportSpecifier;
if (!node.propertyName) {
if (!ts.isIdentifier(node.name)) {
throw new Error(
`Export specifier name should be an identifier, but got ${ts.SyntaxKind[node.name.kind]}.\n` +
SourceFileLocationFormatter.formatDeclaration(node)
);
}

const referencedEntity: CollectorEntity | undefined = collector.tryGetEntityForNode(node.name);
if (!referencedEntity?.nameForEmit) {
throw new InternalError('referencedEntry or referencedEntry.nameForEmit is undefined');
}

const exportName: string = node.name.getText().trim();
if (referencedEntity.nameForEmit !== exportName) {
span.modification.skipAll();
span.modification.prefix = `${referencedEntity.nameForEmit} as ${span.getText()}`;
}
}
}

public static modifyImportTypeSpan(
collector: Collector,
span: Span,
Expand Down
27 changes: 24 additions & 3 deletions apps/api-extractor/src/generators/DtsRollupGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,20 @@ export class DtsRollupGenerator {
case ts.SyntaxKind.ExportKeyword:
case ts.SyntaxKind.DefaultKeyword:
case ts.SyntaxKind.DeclareKeyword:
if (astDeclaration.parent) {
// Keep it as is for nested declarations.
break;
}
if (
ts.isModuleDeclaration(astDeclaration.declaration) &&
(TypeScriptHelpers.findFirstParent(span.node, ts.SyntaxKind.ExportDeclaration) ||
TypeScriptHelpers.findFirstParent(span.node, ts.SyntaxKind.VariableStatement))
) {
// Keep it as is for nested declarations.
// (special cases inside namespace. e.g. "export {};", "export const a: number;")
break;
}

// Delete any explicit "export" or "declare" keywords -- we will re-add them below
span.modification.skipAll();
break;
Expand All @@ -282,13 +296,16 @@ export class DtsRollupGenerator {
case ts.SyntaxKind.ModuleKeyword:
case ts.SyntaxKind.TypeKeyword:
case ts.SyntaxKind.FunctionKeyword:
if (astDeclaration.parent) {
// Keep it as is for nested declarations
break;
}

// Replace the stuff we possibly deleted above
let replacedModifiers: string = '';

// Add a declare statement for root declarations (but not for nested declarations)
if (!astDeclaration.parent) {
replacedModifiers += 'declare ';
}
replacedModifiers += 'declare ';

if (entity.shouldInlineExport) {
replacedModifiers = 'export ' + replacedModifiers;
Expand Down Expand Up @@ -349,6 +366,10 @@ export class DtsRollupGenerator {
}
break;

case ts.SyntaxKind.ExportSpecifier:
DtsEmitHelpers.modifyExportSpecifierSpan(collector, span);
break;

case ts.SyntaxKind.Identifier:
{
const referencedEntity: CollectorEntity | undefined = collector.tryGetEntityForNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export type IInterfaceLikeTypeAlias = {
// @public (undocumented)
export namespace Namespace1 {
// (undocumented)
export class Class3 {
class Class3 {
someMethod(x: boolean | string): void;
someOverload(x: boolean): void;
someOverload(x: string): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export declare type IInterfaceLikeTypeAlias = {
/** @public */
export declare namespace Namespace1 {
/** @public */
export class Class3 {
class Class3 {
/** Some prop. */
someProp: number;
/** Some method. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export enum DocEnumNamespaceMerge {

// @public
export namespace DocEnumNamespaceMerge {
export function exampleFunction(): void;
function exampleFunction(): void;
}

// @public
Expand Down Expand Up @@ -178,8 +178,8 @@ export interface IDocInterface7 {

// @public
export namespace OuterNamespace {
export namespace InnerNamespace {
export function nestedFunction(x: number): number;
namespace InnerNamespace {
function nestedFunction(x: number): number;
}
let nestedVariable: boolean;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ export declare interface Lib1Interface {

/** @public */
export declare namespace Lib1Namespace {
export namespace Inner {
export class X {
namespace Inner {
class X {
}
}
export class Y {
class Y {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ export interface Lib1Interface {
// @public (undocumented)
export namespace Lib1Namespace {
// (undocumented)
export namespace Inner {
namespace Inner {
// (undocumented)
export class X {
class X {
}
}
// (undocumented)
export class Y {
class Y {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ export namespace n1 {
// (undocumented)
export namespace n2 {
// (undocumented)
export class SomeClass3 {
class SomeClass3 {
}
}
// (undocumented)
export class SomeClass1 {
class SomeClass1 {
}
// (undocumented)
export class SomeClass2 extends SomeClass1 {
}
{};
export {};
}

// @public (undocumented)
export namespace n1 {
// (undocumented)
export class SomeClass4 {
class SomeClass4 {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ export declare interface IInterface {

/** @public */
export declare namespace n1 {
export class SomeClass1 {
class SomeClass1 {
}
export class SomeClass2 extends SomeClass1 {
}
export namespace n2 {
export class SomeClass3 {
class SomeClass3 {
}
}
{};
export {};
}

/** @public */
export declare namespace n1 {
export class SomeClass4 {
class SomeClass4 {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function failWithMissingReference(): void;
// @public (undocumented)
export namespace MyNamespace {
// (undocumented)
export class MyClass {
class MyClass {
// @beta
myMethod(x: number): number;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export declare function failWithMissingReference(): void;
* @public
*/
export declare namespace MyNamespace {
export class MyClass {
class MyClass {
/**
* Summary for myMethod
* @remarks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// @public (undocumented)
export namespace A {
// (undocumented)
export class B {
class B {
// (undocumented)
myMethod(): void;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @public */
export declare namespace A {
export class B {
class B {
myMethod(): void;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ export function someFunction7(): AnotherDuplicateName_2;
// @public (undocumented)
export namespace SomeNamespace1 {
// (undocumented)
export class ForgottenExport3 {
class ForgottenExport3 {
}
// (undocumented)
export function someFunction3(): ForgottenExport3;
{};
export {};
}

// (No @packageDocumentation comment for this package)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ export declare function someFunction7(): AnotherDuplicateName_2;

/** @public */
export declare namespace SomeNamespace1 {
export class ForgottenExport3 {
class ForgottenExport3 {
}
export function someFunction3(): ForgottenExport3;
{};
export {};
}

export { }
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ export interface MergedInterfaces {
// @public (undocumented)
export namespace MergedNamespaces {
// (undocumented)
export class SomeClass {
class SomeClass {
}
}

// @public (undocumented)
export namespace MergedNamespaces {
// (undocumented)
export class AnotherClass {
class AnotherClass {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ export declare interface MergedInterfaces {

/** @public */
export declare namespace MergedNamespaces {
export class SomeClass {
class SomeClass {
}
}

/** @public */
export declare namespace MergedNamespaces {
export class AnotherClass {
class AnotherClass {
}
}

Expand Down
Loading
Loading