|
| 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 | +/* tslint:disable:no-bitwise */ |
| 5 | + |
| 6 | +import * as fs from 'fs'; |
| 7 | +import * as ts from 'typescript'; |
| 8 | +import * as dts from '../dts-dom'; |
| 9 | + |
| 10 | +import Extractor from '../Extractor'; |
| 11 | +import IndentedWriter from '../IndentedWriter'; |
| 12 | +import PrettyPrinter from '../PrettyPrinter'; |
| 13 | +import TypeScriptHelpers from '../TypeScriptHelpers'; |
| 14 | + |
| 15 | +class Entry { |
| 16 | + public localName: string; |
| 17 | + public followedSymbol: ts.Symbol; |
| 18 | + public dtsDeclarations: dts.TopLevelDeclaration[] = []; |
| 19 | +} |
| 20 | + |
| 21 | +export default class DtsGenerator { |
| 22 | + private _extractor: Extractor; |
| 23 | + private readonly _entriesBySymbol: Map<ts.Symbol, Entry> = new Map<ts.Symbol, Entry>(); |
| 24 | + private readonly _entries: Entry[] = []; |
| 25 | + |
| 26 | + /** |
| 27 | + * Walks up the tree from the given starting node. If each parent matches the expected kind |
| 28 | + * from parentKinds, then the matching node is returned. Otherwise, undefined is returned. |
| 29 | + */ |
| 30 | + private static _matchParent<T extends ts.Node>(node: ts.Node, parentKinds: ts.SyntaxKind[]): T { |
| 31 | + let current: ts.Node = node; |
| 32 | + |
| 33 | + for (let i: number = 0; ; ++i) { |
| 34 | + if (!current || current.kind !== parentKinds[i]) { |
| 35 | + return undefined; |
| 36 | + } |
| 37 | + |
| 38 | + if (i >= parentKinds.length) { |
| 39 | + break; |
| 40 | + } |
| 41 | + |
| 42 | + current = current.parent; |
| 43 | + } |
| 44 | + |
| 45 | + return current as T; |
| 46 | + } |
| 47 | + |
| 48 | + private static _followAliases(symbol: ts.Symbol, typeChecker: ts.TypeChecker): ts.Symbol { |
| 49 | + let current: ts.Symbol = symbol; |
| 50 | + |
| 51 | + while (true) { // tslint:disable-line:no-constant-condition |
| 52 | + if (!(current.flags & ts.SymbolFlags.Alias)) { |
| 53 | + break; |
| 54 | + } |
| 55 | + const currentAlias: ts.Symbol = TypeScriptHelpers.getImmediateAliasedSymbol(current, typeChecker); |
| 56 | + // Stop if we reach the end of the chain |
| 57 | + if (!currentAlias || currentAlias === current) { |
| 58 | + break; |
| 59 | + } |
| 60 | + |
| 61 | + // Is it an export declaration? |
| 62 | + if (currentAlias.declarations) { |
| 63 | + const exportDeclaration: ts.ExportDeclaration = DtsGenerator._matchParent<ts.ExportDeclaration>( |
| 64 | + currentAlias.declarations[0], |
| 65 | + [ts.SyntaxKind.ExportSpecifier, ts.SyntaxKind.NamedExports, ts.SyntaxKind.ExportDeclaration]); |
| 66 | + |
| 67 | + if (exportDeclaration) { |
| 68 | + // Example: " '@microsoft/sp-lodash-subset'" or " './MyClass'" |
| 69 | + const moduleSpecifier: string = exportDeclaration.moduleSpecifier.getFullText(); |
| 70 | + |
| 71 | + // Does it start with something like "'./"? |
| 72 | + // If not, then assume it's an import from an external package |
| 73 | + if (!/^['"\s]+\.[\/\\]/.test(moduleSpecifier)) { |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + current = currentAlias; |
| 80 | + } |
| 81 | + |
| 82 | + return current; |
| 83 | + } |
| 84 | + |
| 85 | + public constructor(extractor: Extractor) { |
| 86 | + this._extractor = extractor; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Generates the report and writes it to disk. |
| 91 | + * |
| 92 | + * @param reportFilename - The output filename |
| 93 | + * @param analyzer - An Analyzer object representing the input project. |
| 94 | + */ |
| 95 | + public writeDtsFile(reportFilename: string): void { |
| 96 | + const fileContent: string = this.generateDtsFileContent(); |
| 97 | + fs.writeFileSync(reportFilename, fileContent); |
| 98 | + } |
| 99 | + |
| 100 | + public generateDtsFileContent(): string { |
| 101 | + const packageSymbol: ts.Symbol = this._extractor.package.getDeclarationSymbol(); |
| 102 | + |
| 103 | + const exportSymbols: ts.Symbol[] = this._extractor.typeChecker.getExportsOfModule(packageSymbol) || []; |
| 104 | + for (const exportSymbol of exportSymbols) { |
| 105 | + |
| 106 | + const entry: Entry = this._fetchEntryForSymbol(exportSymbol); |
| 107 | + for (const dtsDeclaration of entry.dtsDeclarations) { |
| 108 | + dtsDeclaration.flags |= dts.DeclarationFlags.Export; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + this._entries.sort((a, b) => a.localName.localeCompare(b.localName)); |
| 113 | + |
| 114 | + let content: string = ''; |
| 115 | + for (const entry of this._entries) { |
| 116 | + for (const dtsDeclaration of entry.dtsDeclarations) { |
| 117 | + content += dts.emit(dtsDeclaration); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Normalize to CRLF |
| 122 | + const fileContent: string = content.toString().replace(/\r?\n/g, '\r\n'); |
| 123 | + return fileContent; |
| 124 | + } |
| 125 | + |
| 126 | + private _fetchEntryForSymbol(symbol: ts.Symbol): Entry { |
| 127 | + const followedSymbol: ts.Symbol = DtsGenerator._followAliases(symbol, this._extractor.typeChecker); |
| 128 | + |
| 129 | + const entry: Entry = this._createEntry(symbol.name, followedSymbol); |
| 130 | + |
| 131 | + for (const declaration of followedSymbol.declarations) { |
| 132 | + console.log(PrettyPrinter.dumpTree(declaration)); |
| 133 | + console.log('-------------------------------------'); |
| 134 | + console.log(declaration.getText()); |
| 135 | + console.log('====================================='); |
| 136 | + |
| 137 | + switch (declaration.kind) { |
| 138 | + case ts.SyntaxKind.ClassDeclaration: |
| 139 | + this._processClassDeclaration(entry, declaration as ts.ClassDeclaration); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return entry; |
| 144 | + } |
| 145 | + |
| 146 | + private _processClassDeclaration(entry: Entry, declaration: ts.ClassDeclaration): void { |
| 147 | + const classDts: dts.ClassDeclaration = dts.create.class(entry.localName); |
| 148 | + entry.dtsDeclarations.push(classDts); |
| 149 | + // classDts.implements.push(interfaceDts); |
| 150 | + } |
| 151 | + |
| 152 | + private _createEntry(localName: string, followedSymbol: ts.Symbol): Entry { |
| 153 | + const entry: Entry = new Entry(); |
| 154 | + entry.localName = localName; |
| 155 | + entry.followedSymbol = followedSymbol; |
| 156 | + this._entries.push(entry); |
| 157 | + this._entriesBySymbol.set(followedSymbol, entry); |
| 158 | + return entry; |
| 159 | + } |
| 160 | +} |
0 commit comments