From 2e4f1297109f60d7a416116098ec5eb448c5253b Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 2 Aug 2022 15:50:31 -0700 Subject: [PATCH 1/2] Fix up code so we don't crash when TS itself is emitted with let/const --- src/compiler/checker.ts | 6 +++--- src/services/services.ts | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7ac4bb7d5a52b..aa0d50dcfbf9d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -27022,10 +27022,10 @@ namespace ts { case AssignmentDeclarationKind.ExportsProperty: case AssignmentDeclarationKind.Prototype: case AssignmentDeclarationKind.PrototypeProperty: - let valueDeclaration = binaryExpression.left.symbol?.valueDeclaration; - // falls through case AssignmentDeclarationKind.ModuleExports: - valueDeclaration ||= binaryExpression.symbol?.valueDeclaration; + const valueDeclaration = kind !== AssignmentDeclarationKind.ModuleExports + ? binaryExpression.left.symbol?.valueDeclaration + : binaryExpression.symbol?.valueDeclaration; const annotated = valueDeclaration && getEffectiveTypeAnnotationNode(valueDeclaration); return annotated ? getTypeFromTypeNode(annotated) : undefined; case AssignmentDeclarationKind.ObjectDefinePropertyValue: diff --git a/src/services/services.ts b/src/services/services.ts index ecd7eae9530c9..15540e28acf99 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1363,6 +1363,11 @@ namespace ts { onUnRecoverableConfigFileDiagnostic: noop, }; + // The call to isProgramUptoDate below may refer back to documentRegistryBucketKey; + // calculate this early so it's not undefined if downleveled to a var (or, if emitted + // as a const variable without downleveling, doesn't crash). + const documentRegistryBucketKey = documentRegistry.getKeyForCompilationSettings(newSettings); + // If the program is already up-to-date, we can reuse it if (isProgramUptoDate(program, rootFileNames, newSettings, (_path, fileName) => host.getScriptVersion(fileName), fileName => compilerHost!.fileExists(fileName), hasInvalidatedResolution, hasChangedAutomaticTypeDirectiveNames, getParsedCommandLine, projectReferences)) { return; @@ -1374,7 +1379,6 @@ namespace ts { // the program points to old source files that have been invalidated because of // incremental parsing. - const documentRegistryBucketKey = documentRegistry.getKeyForCompilationSettings(newSettings); const options: CreateProgramOptions = { rootNames: rootFileNames, options: newSettings, From e53273d131b3c2cec0dab1495fcd80aa1c3315ce Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 4 Aug 2022 09:28:55 -0700 Subject: [PATCH 2/2] Make switch case code equivalent --- src/compiler/checker.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index aa0d50dcfbf9d..a364e7d63ad5e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -27023,9 +27023,11 @@ namespace ts { case AssignmentDeclarationKind.Prototype: case AssignmentDeclarationKind.PrototypeProperty: case AssignmentDeclarationKind.ModuleExports: - const valueDeclaration = kind !== AssignmentDeclarationKind.ModuleExports - ? binaryExpression.left.symbol?.valueDeclaration - : binaryExpression.symbol?.valueDeclaration; + let valueDeclaration: Declaration | undefined; + if (kind !== AssignmentDeclarationKind.ModuleExports) { + valueDeclaration = binaryExpression.left.symbol?.valueDeclaration; + } + valueDeclaration ||= binaryExpression.symbol?.valueDeclaration; const annotated = valueDeclaration && getEffectiveTypeAnnotationNode(valueDeclaration); return annotated ? getTypeFromTypeNode(annotated) : undefined; case AssignmentDeclarationKind.ObjectDefinePropertyValue: