|
5 | 5 |
|
6 | 6 | import * as ts from 'typescript'; |
7 | 7 | 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'; |
9 | 9 |
|
10 | 10 | import { ExtractorContext } from '../../ExtractorContext'; |
11 | 11 | import { IndentedWriter } from '../../utils/IndentedWriter'; |
@@ -62,7 +62,14 @@ export class DtsRollupGenerator { |
62 | 62 | * |
63 | 63 | * /// <reference types="example-library" /> |
64 | 64 | */ |
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>(); |
66 | 73 |
|
67 | 74 | public constructor(context: ExtractorContext) { |
68 | 75 | this._context = context; |
@@ -104,8 +111,9 @@ export class DtsRollupGenerator { |
104 | 111 |
|
105 | 112 | this._makeUniqueNames(); |
106 | 113 |
|
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); |
109 | 117 | } |
110 | 118 |
|
111 | 119 | /** |
@@ -215,12 +223,16 @@ export class DtsRollupGenerator { |
215 | 223 | } |
216 | 224 |
|
217 | 225 | // Emit the triple slash directives |
218 | | - for (const typeDirectiveReference of this._dtsTypeDefinitionReferences) { |
| 226 | + for (const typeDirectiveReference of this._dtsTypeReferenceDirectives) { |
219 | 227 | // tslint:disable-next-line:max-line-length |
220 | 228 | // https://github.com/Microsoft/TypeScript/blob/611ebc7aadd7a44a4c0447698bfda9222a78cb66/src/compiler/declarationEmitter.ts#L162 |
221 | 229 | indentedWriter.writeLine(`/// <reference types="${typeDirectiveReference}" />`); |
222 | 230 | } |
223 | 231 |
|
| 232 | + for (const libDirectiveReference of this._dtsLibReferenceDirectives) { |
| 233 | + indentedWriter.writeLine(`/// <reference lib="${libDirectiveReference}" />`); |
| 234 | + } |
| 235 | + |
224 | 236 | // Emit the imports |
225 | 237 | for (const dtsEntry of this._dtsEntries) { |
226 | 238 | if (dtsEntry.astSymbol.astImport) { |
@@ -546,9 +558,12 @@ export class DtsRollupGenerator { |
546 | 558 |
|
547 | 559 | for (const typeReferenceDirective of sourceFile.typeReferenceDirectives) { |
548 | 560 | const name: string = sourceFile.text.substring(typeReferenceDirective.pos, typeReferenceDirective.end); |
549 | | - if (this._dtsTypeDefinitionReferences.indexOf(name) < 0) { |
550 | | - this._dtsTypeDefinitionReferences.push(name); |
551 | | - } |
| 561 | + this._dtsTypeReferenceDirectives.add(name); |
| 562 | + } |
| 563 | + |
| 564 | + for (const libReferenceDirective of sourceFile.libReferenceDirectives) { |
| 565 | + const name: string = sourceFile.text.substring(libReferenceDirective.pos, libReferenceDirective.end); |
| 566 | + this._dtsLibReferenceDirectives.add(name); |
552 | 567 | } |
553 | 568 |
|
554 | 569 | } |
|
0 commit comments