import { DecodedMappings } from './interfaces' ; export interface Delegate { length : number ; newline ( ) : void ; separator ( ) : void ; write1 ( column ) : void ; write4 ( column , source , sourceLine , sourceColumn ) : void ; write5 ( column , source , sourceLine , sourceColumn , name ) : void ; } export default class MappingsEncoder { public column = 0 ; public source = 0 ; public sourceLine = 0 ; public sourceColumn = 0 ; public name = 0 ; public delegate : Delegate ; constructor ( delegate ) { this . delegate = delegate ; } public encode ( mappings ) { for ( let i = 0 ; i < mappings . length ; i ++ ) { const line = mappings [ i ] ; for ( let j = 0 ; j < line . length ; j ++ ) { const mapping = line [ j ] ; switch ( mapping . fieldCount ) { case 1 : this . write1 ( mapping ) ; break ; case 4 : this . write4 ( mapping ) ; break ; case 5 : this . write5 ( mapping ) ; break ; default : missingFieldCount ( ) ; } if ( j < line . length - 1 ) { this . separator ( ) ; } } if ( i < mappings . length - 1 ) { this . newline ( ) ; } this . column = 0 ; } return this . delegate . length ; } public separator ( ) { this . delegate . separator ( ) ; } public newline ( ) { this . delegate . newline ( ) ; } public write5 ( mapping ) { this . delegate . write5 ( mapping . col - this . column , mapping . src - this . source , mapping . srcLine - this . sourceLine , mapping . srcCol - this . sourceColumn , mapping . name - this . name ) ; this . column = mapping . col ; this . source = mapping . src ; this . sourceLine = mapping . srcLine ; this . sourceColumn = mapping . srcCol ; this . name = mapping . name ; } public write4 ( mapping ) { this . delegate . write4 ( mapping . col - this . column , mapping . src - this . source , mapping . srcLine - this . sourceLine , mapping . srcCol - this . sourceColumn ) ; this . column = mapping . col ; this . source = mapping . src ; this . sourceLine = mapping . srcLine ; this . sourceColumn = mapping . srcCol ; } public write1 ( mapping ) { this . delegate . write1 ( mapping . col - this . column ) ; this . column = mapping . col ; } } function missingFieldCount ( ) { throw new TypeError ( 'mappings to encode require fieldCount' ) ; }