import Report from './report' ; import { SourceMapConsumer , MappingItem } from 'source-map' ; import { LineNotFoundError , BadTokenError , BadColumnError } from './errors' ; import { MAX_REPORT_SIZE } from './constants' ; param { object } mapping param { * } sourceLines param { * } generatedLines function validateMapping ( mapping , sourceLines , generatedLines ) { let origLine ; try { origLine = sourceLines [ mapping . originalLine - 1 ] ; } catch ( e ) { } if ( ! origLine ) { return new LineNotFoundError ( mapping . source , { line : mapping . originalLine , column : mapping . originalColumn } ) ; } let sourceToken = origLine . slice ( mapping . originalColumn , mapping . originalColumn + mapping . name . length ) . trim ( ) ; if ( sourceToken === mapping . name ) { return null ; } if ( sourceToken . startsWith ( "'" ) || sourceToken . startsWith ( '"' ) ) { sourceToken = origLine . slice ( mapping . originalColumn + 1 , mapping . originalColumn + mapping . name . length + 1 ) . trim ( ) ; } if ( sourceToken === mapping . name ) { return null ; } const ErrorClass = origLine . indexOf ( mapping . name ) > - 1 ? BadColumnError : BadTokenError ; const { generatedColumn } = mapping ; let generatedLine ; try { generatedLine = generatedLines [ mapping . generatedLine - 1 ] ; } catch ( e ) { } const contextLines = [ ] ; for ( let i = Math . max ( mapping . originalLine - 3 , 0 ) ; i < mapping . originalLine + 2 && i < sourceLines . length ; i ++ ) { contextLines . push ( [ i + 1 , sourceLines [ i ] ] ) ; } const generatedContext = ( generatedLine || '' ) . slice ( generatedColumn - 50 , generatedColumn + 50 ) ; return new ErrorClass ( mapping . source , { token : sourceToken , expected : mapping . name , mapping : { originalContext : contextLines , originalLine : mapping . originalLine , originalColumn : mapping . originalColumn , generatedContext , generatedLine : mapping . generatedLine , generatedColumn : mapping . generatedColumn } } ) ; } param { SourceMapConsumer } sourceMapConsumer param { array } generatedLines export default function validateMappings ( sourceMapConsumer , generatedLines ) { const report = new Report ( ) ; const sourceCache = { } ; sourceMapConsumer . eachMapping ( ( mapping ) => { if ( report . size ( ) >= MAX_REPORT_SIZE ) { return ; } if ( ! mapping . name ) { return ; } const { source } = mapping ; let sourceLines ; if ( { } . hasOwnProperty . call ( sourceCache , source ) ) { sourceLines = sourceCache [ source ] ; } else { const sourceContent = sourceMapConsumer . sourceContentFor ( mapping . source ) ; if ( ! sourceContent ) { return ; } sourceLines = sourceContent . split ( /\n/ ) ; sourceCache [ mapping . source ] = sourceLines ; } const error = validateMapping ( mapping , sourceLines , generatedLines ) ; if ( error && error . name === 'BadColumnError' ) { report . pushWarning ( error ) ; } else if ( error ) { report . pushError ( error ) ; } } ) ; return report ; }