@@ -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 ) {
0 commit comments