Skip to content

Commit 00a75c4

Browse files
authored
Retarget to es6 and fix the resulting bugs (#32221)
* Retarget to es6 and fix the resulting bugs * Set target back to es5 * Fix typos in declaration emitter
1 parent 33f362a commit 00a75c4

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

src/compiler/core.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,17 @@ namespace ts {
13811381
return keys;
13821382
}
13831383

1384+
export function getAllKeys(obj: object): string[] {
1385+
const result: string[] = [];
1386+
do {
1387+
const names = Object.getOwnPropertyNames(obj);
1388+
for (const name of names) {
1389+
pushIfUnique(result, name);
1390+
}
1391+
} while (obj = Object.getPrototypeOf(obj));
1392+
return result;
1393+
}
1394+
13841395
export function getOwnValues<T>(sparseArray: T[]): T[] {
13851396
const values: T[] = [];
13861397
for (const key in sparseArray) {

src/compiler/program.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,10 @@ namespace ts {
814814
let mapFromFileToProjectReferenceRedirects: Map<Path> | undefined;
815815

816816
const shouldCreateNewSourceFile = shouldProgramCreateNewSourceFiles(oldProgram, options);
817-
const structuralIsReused = tryReuseStructureFromOldProgram();
817+
// We set `structuralIsReused` to `undefined` because `tryReuseStructureFromOldProgram` calls `tryReuseStructureFromOldProgram` which checks
818+
// `structuralIsReused`, which would be a TDZ violation if it was not set in advance to `undefined`.
819+
let structuralIsReused: StructureIsReused | undefined;
820+
structuralIsReused = tryReuseStructureFromOldProgram();
818821
if (structuralIsReused !== StructureIsReused.Completely) {
819822
processingDefaultLibFiles = [];
820823
processingOtherFiles = [];

src/compiler/transformers/declarations.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ namespace ts {
8383
let currentSourceFile: SourceFile;
8484
let refs: Map<SourceFile>;
8585
let libs: Map<boolean>;
86+
let emittedImports: readonly AnyImportSyntax[] | undefined; // must be declared in container so it can be `undefined` while transformer's first pass
8687
const resolver = context.getEmitResolver();
8788
const options = context.getCompilerOptions();
8889
const newLine = getNewLineCharacter(options);
@@ -279,7 +280,7 @@ namespace ts {
279280
const statements = visitNodes(node.statements, visitDeclarationStatements);
280281
let combinedStatements = setTextRange(createNodeArray(transformAndReplaceLatePaintedStatements(statements)), node.statements);
281282
refs.forEach(referenceVisitor);
282-
const emittedImports = filter(combinedStatements, isAnyImportSyntax);
283+
emittedImports = filter(combinedStatements, isAnyImportSyntax);
283284
if (isExternalModule(node) && (!resultHasExternalModuleIndicator || (needsScopeFixMarker && !resultHasScopeMarker))) {
284285
combinedStatements = setTextRange(createNodeArray([...combinedStatements, createEmptyExports()]), combinedStatements);
285286
}
@@ -736,6 +737,12 @@ namespace ts {
736737
}
737738
const oldDiag = getSymbolAccessibilityDiagnostic;
738739

740+
// Setup diagnostic-related flags before first potential `cleanup` call, otherwise
741+
// We'd see a TDZ violation at runtime
742+
const canProduceDiagnostic = canProduceDiagnostics(input);
743+
const oldWithinObjectLiteralType = suppressNewDiagnosticContexts;
744+
let shouldEnterSuppressNewDiagnosticsContextContext = ((input.kind === SyntaxKind.TypeLiteral || input.kind === SyntaxKind.MappedType) && input.parent.kind !== SyntaxKind.TypeAliasDeclaration);
745+
739746
// Emit methods which are private as properties with no type information
740747
if (isMethodDeclaration(input) || isMethodSignature(input)) {
741748
if (hasModifier(input, ModifierFlags.Private)) {
@@ -744,17 +751,14 @@ namespace ts {
744751
}
745752
}
746753

747-
const canProdiceDiagnostic = canProduceDiagnostics(input);
748-
if (canProdiceDiagnostic && !suppressNewDiagnosticContexts) {
754+
if (canProduceDiagnostic && !suppressNewDiagnosticContexts) {
749755
getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode(input as DeclarationDiagnosticProducing);
750756
}
751757

752758
if (isTypeQueryNode(input)) {
753759
checkEntityNameVisibility(input.exprName, enclosingDeclaration);
754760
}
755761

756-
const oldWithinObjectLiteralType = suppressNewDiagnosticContexts;
757-
let shouldEnterSuppressNewDiagnosticsContextContext = ((input.kind === SyntaxKind.TypeLiteral || input.kind === SyntaxKind.MappedType) && input.parent.kind !== SyntaxKind.TypeAliasDeclaration);
758762
if (shouldEnterSuppressNewDiagnosticsContextContext) {
759763
// We stop making new diagnostic contexts within object literal types. Unless it's an object type on the RHS of a type alias declaration. Then we do.
760764
suppressNewDiagnosticContexts = true;
@@ -909,13 +913,13 @@ namespace ts {
909913
return cleanup(visitEachChild(input, visitDeclarationSubtree, context));
910914

911915
function cleanup<T extends Node>(returnValue: T | undefined): T | undefined {
912-
if (returnValue && canProdiceDiagnostic && hasDynamicName(input as Declaration)) {
916+
if (returnValue && canProduceDiagnostic && hasDynamicName(input as Declaration)) {
913917
checkName(input as DeclarationDiagnosticProducing);
914918
}
915919
if (isEnclosingDeclaration(input)) {
916920
enclosingDeclaration = previousEnclosingDeclaration;
917921
}
918-
if (canProdiceDiagnostic && !suppressNewDiagnosticContexts) {
922+
if (canProduceDiagnostic && !suppressNewDiagnosticContexts) {
919923
getSymbolAccessibilityDiagnostic = oldDiag;
920924
}
921925
if (shouldEnterSuppressNewDiagnosticsContextContext) {

src/harness/fourslash.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,8 @@ namespace FourSlash {
344344
"getDocumentHighlights",
345345
];
346346
const proxy = {} as ts.LanguageService;
347-
for (const k in ls) {
347+
const keys = ts.getAllKeys(ls);
348+
for (const k of keys) {
348349
const key = k as keyof typeof ls;
349350
if (cacheableMembers.indexOf(key) === -1) {
350351
proxy[key] = (...args: any[]) => (ls[key] as Function)(...args);

0 commit comments

Comments
 (0)