Skip to content

Commit f16894d

Browse files
authored
Merge pull request microsoft#1267 from jonathandturner/default_import_esmodule
[api-extractor] Improve support for default imports
2 parents ed89660 + 4f004d5 commit f16894d

16 files changed

Lines changed: 235 additions & 102 deletions

File tree

apps/api-extractor/src/analyzer/AstImport.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import { InternalError } from '@microsoft/node-core-library';
88
* Indicates the import kind for an `AstImport`.
99
*/
1010
export enum AstImportKind {
11+
/**
12+
* An import statement such as `import X from "y";`.
13+
*/
14+
DefaultImport,
15+
1116
/**
1217
* An import statement such as `import { X } from "y";`.
1318
*/
@@ -62,6 +67,9 @@ export class AstImport {
6267
* The name depends on the type of import:
6368
*
6469
* ```ts
70+
* // For AstImportKind.DefaultImport style, exportName would be "X" in this example:
71+
* import X from "y";
72+
*
6573
* // For AstImportKind.NamedImport style, exportName would be "X" in this example:
6674
* import { X } from "y";
6775
*
@@ -110,6 +118,8 @@ export class AstImport {
110118
*/
111119
public static getKey(options: IAstImportOptions): string {
112120
switch (options.importKind) {
121+
case AstImportKind.DefaultImport:
122+
return `${options.modulePath}:${options.exportName}`;
113123
case AstImportKind.NamedImport:
114124
return `${options.modulePath}:${options.exportName}`;
115125
case AstImportKind.StarImport:

apps/api-extractor/src/analyzer/ExportAnalyzer.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,15 @@ export class ExportAnalyzer {
474474
// StringLiteral: pre=['./A']
475475
// SemicolonToken: pre=[;]
476476

477+
const importClause: ts.ImportClause = declaration as ts.ImportClause;
478+
const exportName: string = importClause.name ?
479+
importClause.name.getText().trim() : ts.InternalSymbolName.Default;
480+
477481
if (externalModulePath !== undefined) {
478482
return this._fetchAstImport(declarationSymbol, {
479-
importKind: AstImportKind.NamedImport,
483+
importKind: AstImportKind.DefaultImport,
480484
modulePath: externalModulePath,
481-
exportName: ts.InternalSymbolName.Default
485+
exportName
482486
});
483487
}
484488

apps/api-extractor/src/generators/DtsEmitHelpers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import { Collector } from '../collector/Collector';
1515
export class DtsEmitHelpers {
1616
public static emitImport(stringWriter: StringWriter, collectorEntity: CollectorEntity, astImport: AstImport): void {
1717
switch (astImport.importKind) {
18+
case AstImportKind.DefaultImport:
19+
stringWriter.writeLine(`import ${astImport.exportName} from '${astImport.modulePath}';`);
20+
break;
1821
case AstImportKind.NamedImport:
1922
if (collectorEntity.nameForEmit !== astImport.exportName) {
2023
stringWriter.write(`import { ${astImport.exportName} as ${collectorEntity.nameForEmit} }`);

build-tests/api-extractor-test-01/dist/api-extractor-test-01-beta.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
/// <reference types="jest" />
1313
/// <reference lib="es2018.intl" />
14+
import Long from 'long';
15+
import { MAX_UNSIGNED_VALUE } from 'long';
1416

1517
/**
1618
* Example of an abstract class that is directly exported.
@@ -235,6 +237,7 @@ export declare interface ISimpleInterface {
235237
}
236238

237239
declare const locallyExportedCustomSymbol: unique symbol;
240+
export { MAX_UNSIGNED_VALUE }
238241

239242
/** @public */
240243
export declare namespace NamespaceContainingVariable {
@@ -293,6 +296,11 @@ export declare class TypeReferencesInAedoc {
293296

294297
declare const unexportedCustomSymbol: unique symbol;
295298

299+
/** @public */
300+
export declare class UseLong {
301+
use_long(): Long;
302+
}
303+
296304
/* Excluded from this release type: VARIABLE */
297305

298306
/**

build-tests/api-extractor-test-01/dist/api-extractor-test-01-public.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
/// <reference types="jest" />
1313
/// <reference lib="es2018.intl" />
14+
import Long from 'long';
15+
import { MAX_UNSIGNED_VALUE } from 'long';
1416

1517
/**
1618
* Example of an abstract class that is directly exported.
@@ -228,6 +230,7 @@ export declare interface ISimpleInterface {
228230
}
229231

230232
declare const locallyExportedCustomSymbol: unique symbol;
233+
export { MAX_UNSIGNED_VALUE }
231234

232235
/** @public */
233236
export declare namespace NamespaceContainingVariable {
@@ -286,6 +289,11 @@ export declare class TypeReferencesInAedoc {
286289

287290
declare const unexportedCustomSymbol: unique symbol;
288291

292+
/** @public */
293+
export declare class UseLong {
294+
use_long(): Long;
295+
}
296+
289297
/* Excluded from this release type: VARIABLE */
290298

291299
/**

build-tests/api-extractor-test-01/dist/api-extractor-test-01.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
/// <reference types="jest" />
1313
/// <reference lib="es2018.intl" />
14+
import Long from 'long';
15+
import { MAX_UNSIGNED_VALUE } from 'long';
1416

1517
/**
1618
* Example of an abstract class that is directly exported.
@@ -255,6 +257,7 @@ export declare interface ISimpleInterface {
255257
}
256258

257259
declare const locallyExportedCustomSymbol: unique symbol;
260+
export { MAX_UNSIGNED_VALUE }
258261

259262
/** @public */
260263
export declare namespace NamespaceContainingVariable {
@@ -314,6 +317,11 @@ export declare class TypeReferencesInAedoc {
314317

315318
declare const unexportedCustomSymbol: unique symbol;
316319

320+
/** @public */
321+
export declare class UseLong {
322+
use_long(): Long;
323+
}
324+
317325
/** @alpha */
318326
export declare const VARIABLE: string;
319327

build-tests/api-extractor-test-01/etc/api-extractor-test-01.api.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
55
```ts
66

7+
import Long from 'long';
8+
import { MAX_UNSIGNED_VALUE } from 'long';
9+
710
// @public
811
export abstract class AbstractClass {
912
// (undocumented)
@@ -141,6 +144,8 @@ export interface IMergedInterfaceReferencee {
141144
export interface ISimpleInterface {
142145
}
143146

147+
export { MAX_UNSIGNED_VALUE }
148+
144149
// @public (undocumented)
145150
export namespace NamespaceContainingVariable {
146151
let // @internal (undocumented)
@@ -174,6 +179,12 @@ export class TypeReferencesInAedoc {
174179
getValue3(arg1: TypeReferencesInAedoc): TypeReferencesInAedoc;
175180
}
176181

182+
// @public (undocumented)
183+
export class UseLong {
184+
// (undocumented)
185+
use_long(): Long;
186+
}
187+
177188
// @alpha (undocumented)
178189
export const VARIABLE: string;
179190

build-tests/api-extractor-test-01/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,9 @@
1717
"@types/node": "8.5.8",
1818
"fs-extra": "~7.0.1",
1919
"typescript": "~3.1.6"
20+
},
21+
"devDependencies": {
22+
"@types/long": "4.0.0",
23+
"long": "^4.0.0"
2024
}
2125
}

build-tests/api-extractor-test-01/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ export {
9696
default as ClassExportedAsDefault
9797
} from './DefaultExportEdgeCase';
9898

99+
/**
100+
* Test that we can correctly carry default imports into the rollup .d.ts file
101+
*/
102+
import Long, { MAX_UNSIGNED_VALUE } from "long";
103+
export { MAX_UNSIGNED_VALUE };
104+
/** @public */
105+
export declare class UseLong {
106+
use_long(): Long;
107+
}
108+
99109
export { ClassWithSymbols, fullyExportedCustomSymbol } from './EcmaScriptSymbols';
100110

101111
export { ForgottenExportConsumer1 } from './ForgottenExportConsumer1';

build-tests/api-extractor-test-01/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"declarationMap": true,
99
"experimentalDecorators": true,
1010
"strictNullChecks": true,
11+
"esModuleInterop": true,
1112
"types": [
1213
"node",
1314
"jest"

0 commit comments

Comments
 (0)