Skip to content

Commit 2abef79

Browse files
authored
Merge pull request microsoft#948 from Microsoft/pgonzal/ae-reference-lib
[api-extractor] Emit /// <reference lib="" /> in rolled up .d.ts
2 parents 37991d9 + ea13e64 commit 2abef79

15 files changed

Lines changed: 184 additions & 46 deletions

File tree

apps/api-extractor/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"colors": "~1.2.1",
4040
"jju": "~1.3.0",
4141
"lodash": "~4.17.5",
42-
"typescript": "~3.0.3",
42+
"typescript": "~3.1.6",
4343
"z-schema": "~3.18.3"
4444
},
4545
"devDependencies": {

apps/api-extractor/src/generators/dtsRollup/DtsRollupGenerator.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as ts from 'typescript';
77
import * as tsdoc from '@microsoft/tsdoc';
8-
import { FileSystem, NewlineKind } from '@microsoft/node-core-library';
8+
import { FileSystem, NewlineKind, Sort } from '@microsoft/node-core-library';
99

1010
import { ExtractorContext } from '../../ExtractorContext';
1111
import { IndentedWriter } from '../../utils/IndentedWriter';
@@ -62,7 +62,14 @@ export class DtsRollupGenerator {
6262
*
6363
* /// <reference types="example-library" />
6464
*/
65-
private _dtsTypeDefinitionReferences: string[] = [];
65+
private _dtsTypeReferenceDirectives: Set<string> = new Set<string>();
66+
67+
/**
68+
* A list of names (e.g. "runtime-library") that should appear in a reference like this:
69+
*
70+
* /// <reference lib="runtime-library" />
71+
*/
72+
private _dtsLibReferenceDirectives: Set<string> = new Set<string>();
6673

6774
public constructor(context: ExtractorContext) {
6875
this._context = context;
@@ -104,8 +111,9 @@ export class DtsRollupGenerator {
104111

105112
this._makeUniqueNames();
106113

107-
this._dtsEntries.sort((a, b) => a.getSortKey().localeCompare(b.getSortKey()));
108-
this._dtsTypeDefinitionReferences.sort();
114+
Sort.sortBy(this._dtsEntries, x => x.getSortKey());
115+
Sort.sortSet(this._dtsTypeReferenceDirectives);
116+
Sort.sortSet(this._dtsLibReferenceDirectives);
109117
}
110118

111119
/**
@@ -222,12 +230,16 @@ export class DtsRollupGenerator {
222230
}
223231

224232
// Emit the triple slash directives
225-
for (const typeDirectiveReference of this._dtsTypeDefinitionReferences) {
233+
for (const typeDirectiveReference of this._dtsTypeReferenceDirectives) {
226234
// tslint:disable-next-line:max-line-length
227235
// https://github.com/Microsoft/TypeScript/blob/611ebc7aadd7a44a4c0447698bfda9222a78cb66/src/compiler/declarationEmitter.ts#L162
228236
indentedWriter.writeLine(`/// <reference types="${typeDirectiveReference}" />`);
229237
}
230238

239+
for (const libDirectiveReference of this._dtsLibReferenceDirectives) {
240+
indentedWriter.writeLine(`/// <reference lib="${libDirectiveReference}" />`);
241+
}
242+
231243
// Emit the imports
232244
for (const dtsEntry of this._dtsEntries) {
233245
if (dtsEntry.astSymbol.astImport) {
@@ -553,9 +565,12 @@ export class DtsRollupGenerator {
553565

554566
for (const typeReferenceDirective of sourceFile.typeReferenceDirectives) {
555567
const name: string = sourceFile.text.substring(typeReferenceDirective.pos, typeReferenceDirective.end);
556-
if (this._dtsTypeDefinitionReferences.indexOf(name) < 0) {
557-
this._dtsTypeDefinitionReferences.push(name);
558-
}
568+
this._dtsTypeReferenceDirectives.add(name);
569+
}
570+
571+
for (const libReferenceDirective of sourceFile.libReferenceDirectives) {
572+
const name: string = sourceFile.text.substring(libReferenceDirective.pos, libReferenceDirective.end);
573+
this._dtsLibReferenceDirectives.add(name);
559574
}
560575

561576
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
/// <reference types="jest" />
13+
/// <reference lib="es2018.intl" />
1314

1415
/**
1516
* Example of an abstract class that is directly exported.
@@ -205,6 +206,10 @@ export declare class ReexportedClass {
205206
getValue(): string;
206207
}
207208

209+
/** @public */
210+
export declare class ReferenceLibDirective extends Intl.PluralRules {
211+
}
212+
208213
/**
209214
* This class has links such as {@link TypeReferencesInAedoc}.
210215
* @public

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
/// <reference types="jest" />
13+
/// <reference lib="es2018.intl" />
1314

1415
/**
1516
* Example of an abstract class that is directly exported.
@@ -225,6 +226,10 @@ export declare class ReexportedClass {
225226
getValue(): string;
226227
}
227228

229+
/** @public */
230+
export declare class ReferenceLibDirective extends Intl.PluralRules {
231+
}
232+
228233
/**
229234
* This class has links such as {@link TypeReferencesInAedoc}.
230235
* @public

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
/// <reference types="jest" />
13+
/// <reference lib="es2018.intl" />
1314

1415
/**
1516
* Example of an abstract class that is directly exported.
@@ -198,6 +199,10 @@ export declare class ReexportedClass {
198199
getValue(): string;
199200
}
200201

202+
/** @public */
203+
export declare class ReferenceLibDirective extends Intl.PluralRules {
204+
}
205+
201206
/**
202207
* This class has links such as {@link TypeReferencesInAedoc}.
203208
* @public

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ class ReexportedClass {
107107
getValue(): string;
108108
}
109109

110+
// @public (undocumented)
111+
class ReferenceLibDirective extends Intl.PluralRules {
112+
}
113+
110114
// @public
111115
class TypeReferencesInAedoc {
112116
getValue(arg1: TypeReferencesInAedoc): TypeReferencesInAedoc;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
"@types/jest": "21.1.10",
1717
"@types/node": "8.5.8",
1818
"fs-extra": "~7.0.1",
19-
"typescript": "~3.0.3"
19+
"typescript": "~3.1.6"
2020
}
2121
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
/// <reference lib="es2018.intl" />
5+
6+
/** @public */
7+
export class ReferenceLibDirective extends Intl.PluralRules {
8+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,6 @@ export { default as IInterfaceAsDefaultExport } from './IInterfaceAsDefaultExpor
107107
export { ReexportedClass3 as ReexportedClass } from './ReexportedClass3/ReexportedClass3';
108108

109109
export { TypeReferencesInAedoc } from './TypeReferencesInAedoc';
110+
export { ReferenceLibDirective } from './ReferenceLibDirective';
110111

111112
export { VARIABLE } from './variableDeclarations';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/api-extractor",
5+
"comment": "Add support for emitting `/// <reference lib=\"___\" />` directives in .d.ts rollups (GitHub issue #946)",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor",
10+
"email": "pgonzal@users.noreply.github.com"
11+
}

0 commit comments

Comments
 (0)