|
| 1 | +/// <reference types="node"/> |
| 2 | + |
| 3 | +import * as ts from "../lib/typescript"; |
| 4 | +import * as path from "path"; |
| 5 | + |
| 6 | +function endsWith(s: string, suffix: string) { |
| 7 | + return s.lastIndexOf(suffix, s.length - suffix.length) !== -1; |
| 8 | +} |
| 9 | + |
| 10 | +class DeclarationsWalker { |
| 11 | + private visitedTypes: ts.Type[] = []; |
| 12 | + private text = ""; |
| 13 | + private constructor(private typeChecker: ts.TypeChecker, private protocolFile: ts.SourceFile) { |
| 14 | + } |
| 15 | + |
| 16 | + static getExtraDeclarations(typeChecker: ts.TypeChecker, protocolFile: ts.SourceFile): string { |
| 17 | + let text = "declare namespace ts.server.protocol {\n"; |
| 18 | + var walker = new DeclarationsWalker(typeChecker, protocolFile); |
| 19 | + walker.visitTypeNodes(protocolFile); |
| 20 | + return walker.text |
| 21 | + ? `declare namespace ts.server.protocol {\n${walker.text}}` |
| 22 | + : ""; |
| 23 | + } |
| 24 | + |
| 25 | + private processType(type: ts.Type): void { |
| 26 | + if (this.visitedTypes.indexOf(type) >= 0) { |
| 27 | + return; |
| 28 | + } |
| 29 | + this.visitedTypes.push(type); |
| 30 | + let s = type.aliasSymbol || type.getSymbol(); |
| 31 | + if (!s) { |
| 32 | + return; |
| 33 | + } |
| 34 | + if (s.name === "Array") { |
| 35 | + // we should process type argument instead |
| 36 | + return this.processType((<any>type).typeArguments[0]); |
| 37 | + } |
| 38 | + else { |
| 39 | + for (const decl of s.getDeclarations()) { |
| 40 | + const sourceFile = decl.getSourceFile(); |
| 41 | + if (sourceFile === this.protocolFile || path.basename(sourceFile.fileName) === "lib.d.ts") { |
| 42 | + return; |
| 43 | + } |
| 44 | + // splice declaration in final d.ts file |
| 45 | + const text = decl.getFullText(); |
| 46 | + this.text += `${text}\n`; |
| 47 | + |
| 48 | + // recursively pull all dependencies into result dts file |
| 49 | + this.visitTypeNodes(decl); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private visitTypeNodes(node: ts.Node) { |
| 55 | + if (node.parent) { |
| 56 | + switch (node.parent.kind) { |
| 57 | + case ts.SyntaxKind.VariableDeclaration: |
| 58 | + case ts.SyntaxKind.MethodDeclaration: |
| 59 | + case ts.SyntaxKind.MethodSignature: |
| 60 | + case ts.SyntaxKind.PropertyDeclaration: |
| 61 | + case ts.SyntaxKind.PropertySignature: |
| 62 | + case ts.SyntaxKind.Parameter: |
| 63 | + case ts.SyntaxKind.IndexSignature: |
| 64 | + if (((<ts.VariableDeclaration | ts.MethodDeclaration | ts.PropertyDeclaration | ts.ParameterDeclaration | ts.PropertySignature | ts.MethodSignature | ts.IndexSignatureDeclaration>node.parent).type) === node) { |
| 65 | + const type = this.typeChecker.getTypeAtLocation(node); |
| 66 | + if (type && !(type.flags & ts.TypeFlags.TypeParameter)) { |
| 67 | + this.processType(type); |
| 68 | + } |
| 69 | + } |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + ts.forEachChild(node, n => this.visitTypeNodes(n)); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function generateProtocolFile(protocolTs: string, typeScriptServicesDts: string): string { |
| 78 | + const options = { target: ts.ScriptTarget.ES5, declaration: true, noResolve: true, types: <string[]>[], stripInternal: true }; |
| 79 | + |
| 80 | + /** |
| 81 | + * 1st pass - generate a program from protocol.ts and typescriptservices.d.ts and emit core version of protocol.d.ts with all internal members stripped |
| 82 | + * @return text of protocol.d.t.s |
| 83 | + */ |
| 84 | + function getInitialDtsFileForProtocol() { |
| 85 | + const program = ts.createProgram([protocolTs, typeScriptServicesDts], options); |
| 86 | + |
| 87 | + let protocolDts: string; |
| 88 | + program.emit(program.getSourceFile(protocolTs), (file, content) => { |
| 89 | + if (endsWith(file, ".d.ts")) { |
| 90 | + protocolDts = content; |
| 91 | + } |
| 92 | + }); |
| 93 | + if (protocolDts === undefined) { |
| 94 | + throw new Error(`Declaration file for protocol.ts is not generated`) |
| 95 | + } |
| 96 | + return protocolDts; |
| 97 | + } |
| 98 | + |
| 99 | + const protocolFileName = "protocol.d.ts"; |
| 100 | + /** |
| 101 | + * Second pass - generate a program from protocol.d.ts and typescriptservices.d.ts, then augment core protocol.d.ts with extra types from typescriptservices.d.ts |
| 102 | + */ |
| 103 | + function getProgramWithProtocolText(protocolDts: string, includeTypeScriptServices: boolean) { |
| 104 | + const host = ts.createCompilerHost(options); |
| 105 | + const originalGetSourceFile = host.getSourceFile; |
| 106 | + host.getSourceFile = (fileName) => { |
| 107 | + if (fileName === protocolFileName) { |
| 108 | + return ts.createSourceFile(fileName, protocolDts, options.target); |
| 109 | + } |
| 110 | + return originalGetSourceFile.apply(host, [fileName]); |
| 111 | + } |
| 112 | + const rootFiles = includeTypeScriptServices ? [protocolFileName, typeScriptServicesDts] : [protocolFileName]; |
| 113 | + return ts.createProgram(rootFiles, options, host); |
| 114 | + } |
| 115 | + |
| 116 | + let protocolDts = getInitialDtsFileForProtocol(); |
| 117 | + const program = getProgramWithProtocolText(protocolDts, /*includeTypeScriptServices*/ true); |
| 118 | + |
| 119 | + const protocolFile = program.getSourceFile("protocol.d.ts"); |
| 120 | + const extraDeclarations = DeclarationsWalker.getExtraDeclarations(program.getTypeChecker(), protocolFile); |
| 121 | + if (extraDeclarations) { |
| 122 | + protocolDts += extraDeclarations; |
| 123 | + } |
| 124 | + // do sanity check and try to compile generated text as standalone program |
| 125 | + const sanityCheckProgram = getProgramWithProtocolText(protocolDts, /*includeTypeScriptServices*/ false); |
| 126 | + const diagnostics = [...program.getSyntacticDiagnostics(), ...program.getSemanticDiagnostics(), ...program.getGlobalDiagnostics()]; |
| 127 | + if (diagnostics.length) { |
| 128 | + const flattenedDiagnostics = diagnostics.map(d => ts.flattenDiagnosticMessageText(d.messageText, "\n")).join("\n"); |
| 129 | + throw new Error(`Unexpected errors during sanity check: ${flattenedDiagnostics}`); |
| 130 | + } |
| 131 | + return protocolDts; |
| 132 | +} |
| 133 | + |
| 134 | +if (process.argv.length < 5) { |
| 135 | + console.log(`Expected 3 arguments: path to 'protocol.ts', path to 'typescriptservices.d.ts' and path to output file`); |
| 136 | + process.exit(1); |
| 137 | +} |
| 138 | + |
| 139 | +const protocolTs = process.argv[2]; |
| 140 | +const typeScriptServicesDts = process.argv[3]; |
| 141 | +const outputFile = process.argv[4]; |
| 142 | +const generatedProtocolDts = generateProtocolFile(protocolTs, typeScriptServicesDts); |
| 143 | +ts.sys.writeFile(outputFile, generatedProtocolDts); |
0 commit comments