import ts from 'typescript' ; interface SemanticOrSyntacticError extends ts . Diagnostic { message : string ; } /**
 * By default, diagnostics from the TypeScript compiler contain all errors - regardless of whether
 * they are related to generic ECMAScript standards, or TypeScript-specific constructs.
 *
 * Therefore, we filter out all diagnostics, except for the ones we explicitly want to consider when
 * the user opts in to throwing errors on semantic issues.
 */ export function getFirstSemanticOrSyntacticError ( program , ast ) { try { const supportedSyntacticDiagnostics = whitelistSupportedDiagnostics ( program . getSyntacticDiagnostics ( ast ) ) ; if ( supportedSyntacticDiagnostics . length ) { return convertDiagnosticToSemanticOrSyntacticError ( supportedSyntacticDiagnostics [ 0 ] ) ; } const supportedSemanticDiagnostics = whitelistSupportedDiagnostics ( program . getSemanticDiagnostics ( ast ) ) ; if ( supportedSemanticDiagnostics . length ) { return convertDiagnosticToSemanticOrSyntacticError ( supportedSemanticDiagnostics [ 0 ] ) ; } return undefined ; } catch ( e ) { /**
         * TypeScript compiler has certain Debug.fail() statements in, which will cause the diagnostics
         * retrieval above to throw.
         *
         * E.g. from ast-alignment-tests
         * "Debug Failure. Shouldn't ever directly check a JsxOpeningElement"
         *
         * For our current use-cases this is undesired behavior, so we just suppress it
         * and log a a warning.
         */ console . warn ( `Warning From TSC: "${ e . message }` ) ; return undefined ; } } function whitelistSupportedDiagnostics ( diagnostics ) { return diagnostics . filter ( diagnostic => { switch ( diagnostic . code ) { case 1014 : case 1044 : case 1045 : case 1048 : case 1049 : case 1070 : case 1071 : case 1090 : case 1096 : case 1097 : case 1117 : case 1121 : case 1123 : case 1141 : case 1172 : case 1173 : case 1175 : case 1176 : case 1190 : case 1200 : case 1206 : case 1211 : case 1242 : case 1246 : case 2364 : case 2369 : case 2462 : case 17012 : case 17013 : return true ; } return false ; } ) ; } function convertDiagnosticToSemanticOrSyntacticError ( diagnostic ) { return { ... diagnostic , message : ts . flattenDiagnosticMessageText ( diagnostic . messageText , ts . sys . newLine ) } ; }