-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Automatically generate protocol.d.ts by pulling in necessary dependencies #11550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
735e2fb
generate protocol on fly
vladima b8d5741
update tests
vladima d05530d
Merge remote-tracking branch 'origin/release-2.0.5' into vladima/gene…
vladima 9a70773
add missing requests/responses, add command types to requests
vladima a1b9416
Merge remote-tracking branch 'origin/release-2.0.5' into vladima/gene…
vladima c0d1c31
addressed PR feedback
vladima File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/// <reference types="node"/> | ||
|
||
import * as ts from "../lib/typescript"; | ||
import * as path from "path"; | ||
|
||
function endsWith(s: string, suffix: string) { | ||
return s.lastIndexOf(suffix, s.length - suffix.length) !== -1; | ||
} | ||
|
||
class DeclarationsWalker { | ||
private visitedTypes: ts.Type[] = []; | ||
private text = ""; | ||
private constructor(private typeChecker: ts.TypeChecker, private protocolFile: ts.SourceFile) { | ||
} | ||
|
||
static getExtraDeclarations(typeChecker: ts.TypeChecker, protocolFile: ts.SourceFile): string { | ||
let text = "declare namespace ts.server.protocol {\n"; | ||
var walker = new DeclarationsWalker(typeChecker, protocolFile); | ||
walker.visitTypeNodes(protocolFile); | ||
return walker.text | ||
? `declare namespace ts.server.protocol {\n${walker.text}}` | ||
: ""; | ||
} | ||
|
||
private processType(type: ts.Type): void { | ||
if (this.visitedTypes.indexOf(type) >= 0) { | ||
return; | ||
} | ||
this.visitedTypes.push(type); | ||
let s = type.aliasSymbol || type.getSymbol(); | ||
if (!s) { | ||
return; | ||
} | ||
if (s.name === "Array") { | ||
// we should process type argument instead | ||
return this.processType((<any>type).typeArguments[0]); | ||
} | ||
else { | ||
for (const decl of s.getDeclarations()) { | ||
const sourceFile = decl.getSourceFile(); | ||
if (sourceFile === this.protocolFile || path.basename(sourceFile.fileName) === "lib.d.ts") { | ||
return; | ||
} | ||
// splice declaration in final d.ts file | ||
const text = decl.getFullText(); | ||
this.text += `${text}\n`; | ||
|
||
// recursively pull all dependencies into result dts file | ||
this.visitTypeNodes(decl); | ||
} | ||
} | ||
} | ||
|
||
private visitTypeNodes(node: ts.Node) { | ||
if (node.parent) { | ||
switch (node.parent.kind) { | ||
case ts.SyntaxKind.VariableDeclaration: | ||
case ts.SyntaxKind.MethodDeclaration: | ||
case ts.SyntaxKind.MethodSignature: | ||
case ts.SyntaxKind.PropertyDeclaration: | ||
case ts.SyntaxKind.PropertySignature: | ||
case ts.SyntaxKind.Parameter: | ||
case ts.SyntaxKind.IndexSignature: | ||
if (((<ts.VariableDeclaration | ts.MethodDeclaration | ts.PropertyDeclaration | ts.ParameterDeclaration | ts.PropertySignature | ts.MethodSignature | ts.IndexSignatureDeclaration>node.parent).type) === node) { | ||
const type = this.typeChecker.getTypeAtLocation(node); | ||
if (type && !(type.flags & ts.TypeFlags.TypeParameter)) { | ||
this.processType(type); | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
ts.forEachChild(node, n => this.visitTypeNodes(n)); | ||
} | ||
} | ||
|
||
function generateProtocolFile(protocolTs: string, typeScriptServicesDts: string): string { | ||
const options = { target: ts.ScriptTarget.ES5, declaration: true, noResolve: true, types: <string[]>[], stripInternal: true }; | ||
|
||
/** | ||
* 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 | ||
* @return text of protocol.d.t.s | ||
*/ | ||
function getInitialDtsFileForProtocol() { | ||
const program = ts.createProgram([protocolTs, typeScriptServicesDts], options); | ||
|
||
let protocolDts: string; | ||
program.emit(program.getSourceFile(protocolTs), (file, content) => { | ||
if (endsWith(file, ".d.ts")) { | ||
protocolDts = content; | ||
} | ||
}); | ||
if (protocolDts === undefined) { | ||
throw new Error(`Declaration file for protocol.ts is not generated`) | ||
} | ||
return protocolDts; | ||
} | ||
|
||
const protocolFileName = "protocol.d.ts"; | ||
/** | ||
* 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 | ||
*/ | ||
function getProgramWithProtocolText(protocolDts: string, includeTypeScriptServices: boolean) { | ||
const host = ts.createCompilerHost(options); | ||
const originalGetSourceFile = host.getSourceFile; | ||
host.getSourceFile = (fileName) => { | ||
if (fileName === protocolFileName) { | ||
return ts.createSourceFile(fileName, protocolDts, options.target); | ||
} | ||
return originalGetSourceFile.apply(host, [fileName]); | ||
} | ||
const rootFiles = includeTypeScriptServices ? [protocolFileName, typeScriptServicesDts] : [protocolFileName]; | ||
return ts.createProgram(rootFiles, options, host); | ||
} | ||
|
||
let protocolDts = getInitialDtsFileForProtocol(); | ||
const program = getProgramWithProtocolText(protocolDts, /*includeTypeScriptServices*/ true); | ||
|
||
const protocolFile = program.getSourceFile("protocol.d.ts"); | ||
const extraDeclarations = DeclarationsWalker.getExtraDeclarations(program.getTypeChecker(), protocolFile); | ||
if (extraDeclarations) { | ||
protocolDts += extraDeclarations; | ||
} | ||
// do sanity check and try to compile generated text as standalone program | ||
const sanityCheckProgram = getProgramWithProtocolText(protocolDts, /*includeTypeScriptServices*/ false); | ||
const diagnostics = [...program.getSyntacticDiagnostics(), ...program.getSemanticDiagnostics(), ...program.getGlobalDiagnostics()]; | ||
if (diagnostics.length) { | ||
const flattenedDiagnostics = diagnostics.map(d => ts.flattenDiagnosticMessageText(d.messageText, "\n")).join("\n"); | ||
throw new Error(`Unexpected errors during sanity check: ${flattenedDiagnostics}`); | ||
} | ||
return protocolDts; | ||
} | ||
|
||
if (process.argv.length < 5) { | ||
console.log(`Expected 3 arguments: path to 'protocol.ts', path to 'typescriptservices.d.ts' and path to output file`); | ||
process.exit(1); | ||
} | ||
|
||
const protocolTs = process.argv[2]; | ||
const typeScriptServicesDts = process.argv[3]; | ||
const outputFile = process.argv[4]; | ||
const generatedProtocolDts = generateProtocolFile(protocolTs, typeScriptServicesDts); | ||
ts.sys.writeFile(outputFile, generatedProtocolDts); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to add the new file to the LKG list