Ensure string enums are generated in protocol.d.ts (#17914)

This commit is contained in:
Mohamed Hegazy 2017-08-21 09:44:03 -07:00 committed by GitHub
parent 5e8e735db5
commit 07e1d3b13d
2 changed files with 13 additions and 5 deletions

View File

@ -7,11 +7,15 @@ function endsWith(s: string, suffix: string) {
return s.lastIndexOf(suffix, s.length - suffix.length) !== -1;
}
function isStringEnum(declaration: ts.EnumDeclaration) {
return declaration.members.length && declaration.members.every(m => m.initializer && m.initializer.kind === ts.SyntaxKind.StringLiteral);
}
class DeclarationsWalker {
private visitedTypes: ts.Type[] = [];
private text = "";
private removedTypes: ts.Type[] = [];
private constructor(private typeChecker: ts.TypeChecker, private protocolFile: ts.SourceFile) {
}
@ -19,7 +23,7 @@ class DeclarationsWalker {
let text = "declare namespace ts.server.protocol {\n";
var walker = new DeclarationsWalker(typeChecker, protocolFile);
walker.visitTypeNodes(protocolFile);
text = walker.text
text = walker.text
? `declare namespace ts.server.protocol {\n${walker.text}}`
: "";
if (walker.removedTypes) {
@ -52,7 +56,7 @@ class DeclarationsWalker {
if (sourceFile === this.protocolFile || path.basename(sourceFile.fileName) === "lib.d.ts") {
return;
}
if (decl.kind === ts.SyntaxKind.EnumDeclaration) {
if (decl.kind === ts.SyntaxKind.EnumDeclaration && !isStringEnum(decl as ts.EnumDeclaration)) {
this.removedTypes.push(type);
return;
}
@ -91,7 +95,7 @@ class DeclarationsWalker {
for (const type of heritageClauses[0].types) {
this.processTypeOfNode(type);
}
}
}
break;
}
}
@ -110,7 +114,7 @@ class DeclarationsWalker {
this.processType(type);
}
}
}
}
}
function writeProtocolFile(outputFile: string, protocolTs: string, typeScriptServicesDts: string) {

View File

@ -2489,6 +2489,7 @@ namespace ts.server.protocol {
System = "System",
ES6 = "ES6",
ES2015 = "ES2015",
ESNext = "ESNext"
}
export const enum ModuleResolutionKind {
@ -2506,5 +2507,8 @@ namespace ts.server.protocol {
ES5 = "ES5",
ES6 = "ES6",
ES2015 = "ES2015",
ES2016 = "ES2016",
ES2017 = "ES2017",
ESNext = "ESNext"
}
}