From 07e1d3b13d66e82f901ba8ceb02e6eb90de797ef Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Mon, 21 Aug 2017 09:44:03 -0700 Subject: [PATCH] Ensure string enums are generated in protocol.d.ts (#17914) --- scripts/buildProtocol.ts | 14 +++++++++----- src/server/protocol.ts | 4 ++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/scripts/buildProtocol.ts b/scripts/buildProtocol.ts index e03338bf60d..c2ac33c83fc 100644 --- a/scripts/buildProtocol.ts +++ b/scripts/buildProtocol.ts @@ -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) { diff --git a/src/server/protocol.ts b/src/server/protocol.ts index f50c8731126..37bf79837c9 100644 --- a/src/server/protocol.ts +++ b/src/server/protocol.ts @@ -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" } }