import { SourceMapConsumer , LineRange } from 'source-map' ; import { SourcemapEntry , TSVueFileName , Location , VueScriptFileName , CompiledVueTemplateFileName , VueTemplateFileName , RawVueFileName } from '../types' ; import { isRawVueFile , isVueScriptFile , isCompiledVueTemplateFile , isVueTemplateFile , isTSVueFile , toVueTemplateFileName , toVueScriptFileName , toCompiledVueTemplateFileName , toTSVueFIleName , toRawVueFileName } from '../helpers' ; export function getOriginalPositionFor ( fileName , location , sourcemapEntry , retry = false ) { if ( isTSVueFile ( fileName ) ) { location = { line : location . line + 1 , column : location . column } ; } const entry = sourcemapEntry . get ( fileName ) ! ; const map = entry . map ! ; const consumer = new SourceMapConsumer ( map ) ; const result = consumer . originalPositionFor ( location ) ; if ( result && result . source ) { const { source , line , column } = result ; if ( isRawVueFile ( source ) ) { return { line : line - 1 , column : location . column } ; } if ( isVueScriptFile ( source ) ) { return getOriginalPositionFor ( source , { line : line , column : location . column } , sourcemapEntry ) ; } if ( isCompiledVueTemplateFile ( source ) ) { return getOriginalPositionFor ( source , { line , column : location . column } , sourcemapEntry , true ) ; } if ( isVueTemplateFile ( source ) ) { return getOriginalPositionFor ( source , { line , column } , sourcemapEntry ) ; } throw new Error ( 'could not resolve position from sourcemap for ' + fileName ) ; } else { console . log ( 'throwing error for: ' + fileName ) ; console . log ( JSON . stringify ( location ) ) ; if ( retry ) { return getOriginalPositionFor ( fileName , { ... location , line : location . line - 2 } , sourcemapEntry ) ; } throw new Error ( 'could not resolve position from sourcemap for ' + fileName ) ; } } export function getGeneratedPositionFor ( fileName , location , sourcemapEntry ) { if ( isRawVueFile ( fileName ) ) { location = { line : location . line + 1 , column : location . column } ; const script = sourcemapEntry . get ( toVueScriptFileName ( fileName ) ) ; if ( script ) { const consumer = new SourceMapConsumer ( script . map ! ) ; const result = consumer . generatedPositionFor ( { source : fileName , ... location } ) ; if ( result . line !== null && result . column !== null ) { return { line : result . line - 1 , column : location . column } ; } } const template = sourcemapEntry . get ( toVueTemplateFileName ( fileName ) ) ; if ( template ) { const consumer = new SourceMapConsumer ( template . map ! ) ; const result = consumer . generatedPositionFor ( { source : fileName , ... location } ) ; if ( result . line !== null && result . column !== null ) { return getGeneratedPositionFor ( toVueTemplateFileName ( fileName ) , { line : result . line , column : location . column } , sourcemapEntry ) ; } } throw new Error ( `could not get generated position for: ${ fileName } at ${ location }` ) ; } else if ( isVueTemplateFile ( fileName ) ) { const compiledVueTemplateFileName = toCompiledVueTemplateFileName ( fileName ) ; const entry = sourcemapEntry . get ( compiledVueTemplateFileName ) ! ; const consumer = new SourceMapConsumer ( entry . map ! ) ; const result = consumer . generatedPositionFor ( { source : fileName , ... location } ) ; return getGeneratedPositionFor ( compiledVueTemplateFileName , { line : result . line , column : result . column } , sourcemapEntry ) ; } else if ( isCompiledVueTemplateFile ( fileName ) ) { const rawVueFileName = toRawVueFileName ( fileName ) ; const tsVueFileName = toTSVueFIleName ( rawVueFileName ) ; const entry = sourcemapEntry . get ( tsVueFileName ) ! ; const map = entry . map ! ; if ( map && 'sections' in map ) { const sections = map . sections ; const templateSection = sections [ 1 ] ; const offset = templateSection . offset . line ; const result = { ... location , line : location . line + offset - 1 , } ; return result ; } else { throw new Error ( `could not get generated position for: ${ fileName } at ${ location }` ) ; } } else { throw new Error ( `could not get generated position for: ${ fileName } at ${ location }` ) ; } }