import type * as d from '../../declarations' ; import { isIterable } from '../helpers' ; import { normalizePath } from '../normalize-path' ; import { splitLineBreaks } from './logger-utils' ; import type { Diagnostic , DiagnosticMessageChain , Node } from 'typescript' ; param d param node returns export const augmentDiagnosticWithNode = ( d , node ) : d . Diagnostic => { if ( ! node ) { return d ; } const sourceFile = node . getSourceFile ( ) ; if ( ! sourceFile ) { return d ; } d . absFilePath = normalizePath ( sourceFile . fileName ) ; const sourceText = sourceFile . text ; const srcLines = splitLineBreaks ( sourceText ) ; const start = node . getStart ( ) ; const end = node . getEnd ( ) ; const posStart = sourceFile . getLineAndCharacterOfPosition ( start ) ; const errorLine = { lineIndex : posStart . line , lineNumber : posStart . line + 1 , text : srcLines [ posStart . line ] , errorCharStart : posStart . character , errorLength : Math . max ( end - start , 1 ) , } ; d . lineNumber = errorLine . lineNumber ; d . columnNumber = errorLine . errorCharStart + 1 ; d . lines . push ( errorLine ) ; if ( errorLine . errorLength === 0 && errorLine . errorCharStart > 0 ) { errorLine . errorLength = 1 ; errorLine . errorCharStart -- ; } if ( errorLine . lineIndex > 0 ) { const previousLine = { lineIndex : errorLine . lineIndex - 1 , lineNumber : errorLine . lineNumber - 1 , text : srcLines [ errorLine . lineIndex - 1 ] , errorCharStart : - 1 , errorLength : - 1 , } ; d . lines . unshift ( previousLine ) ; } if ( errorLine . lineIndex + 1 < srcLines . length ) { const nextLine = { lineIndex : errorLine . lineIndex + 1 , lineNumber : errorLine . lineNumber + 1 , text : srcLines [ errorLine . lineIndex + 1 ] , errorCharStart : - 1 , errorLength : - 1 , } ; d . lines . push ( nextLine ) ; } return d ; } ; /**
 * Ok, so formatting overkill, we know. But whatever, it makes for great
 * error reporting within a terminal. So, yeah, let's code it up, shall we?
 */ export const loadTypeScriptDiagnostics = ( tsDiagnostics ) => { const diagnostics = [ ] ; const maxErrors = Math . min ( tsDiagnostics . length , 50 ) ; for ( let i = 0 ; i < maxErrors ; i ++ ) { diagnostics . push ( loadTypeScriptDiagnostic ( tsDiagnostics [ i ] ) ) ; } return diagnostics ; } ; export const loadTypeScriptDiagnostic = ( tsDiagnostic ) => { const d = { level : 'warn' , type : 'typescript' , language : 'typescript' , header : 'TypeScript' , code : tsDiagnostic . code . toString ( ) , messageText : flattenDiagnosticMessageText ( tsDiagnostic , tsDiagnostic . messageText ) , relFilePath : null , absFilePath : null , lines : [ ] , } ; if ( tsDiagnostic . category === 1 ) { d . level = 'error' ; } if ( tsDiagnostic . file ) { d . absFilePath = tsDiagnostic . file . fileName ; const sourceText = tsDiagnostic . file . text ; const srcLines = splitLineBreaks ( sourceText ) ; const posData = tsDiagnostic . file . getLineAndCharacterOfPosition ( tsDiagnostic . start ) ; const errorLine = { lineIndex : posData . line , lineNumber : posData . line + 1 , text : srcLines [ posData . line ] , errorCharStart : posData . character , errorLength : Math . max ( tsDiagnostic . length , 1 ) , } ; d . lineNumber = errorLine . lineNumber ; d . columnNumber = errorLine . errorCharStart + 1 ; d . lines . push ( errorLine ) ; if ( errorLine . errorLength === 0 && errorLine . errorCharStart > 0 ) { errorLine . errorLength = 1 ; errorLine . errorCharStart -- ; } if ( errorLine . lineIndex > 0 ) { const previousLine = { lineIndex : errorLine . lineIndex - 1 , lineNumber : errorLine . lineNumber - 1 , text : srcLines [ errorLine . lineIndex - 1 ] , errorCharStart : - 1 , errorLength : - 1 , } ; d . lines . unshift ( previousLine ) ; } if ( errorLine . lineIndex + 1 < srcLines . length ) { const nextLine = { lineIndex : errorLine . lineIndex + 1 , lineNumber : errorLine . lineNumber + 1 , text : srcLines [ errorLine . lineIndex + 1 ] , errorCharStart : - 1 , errorLength : - 1 , } ; d . lines . push ( nextLine ) ; } } return d ; } ; const flattenDiagnosticMessageText = ( tsDiagnostic , diag ) => { if ( typeof diag === 'string' ) { return diag ; } else if ( diag === undefined ) { return '' ; } const ignoreCodes = [ ] ; const isStencilConfig = tsDiagnostic . file . fileName . includes ( 'stencil.config' ) ; if ( isStencilConfig ) { ignoreCodes . push ( 2322 ) ; } let result = '' ; if ( ! ignoreCodes . includes ( diag . code ) ) { result = diag . messageText ; if ( isIterable ( diag . next ) ) { for ( const kid of diag . next ) { result += flattenDiagnosticMessageText ( tsDiagnostic , kid ) ; } } } if ( isStencilConfig ) { result = result . replace ( `type 'StencilConfig'` , `Stencil Config` ) ; result = result . replace ( `Object literal may only specify known properties, but ` , `` ) ; result = result . replace ( `Object literal may only specify known properties, and ` , `` ) ; } return result . trim ( ) ; } ;