offset = new IndexMapOffset( 0, 0 ); $this->sections = []; } /** * Set the name of the output file, to be given as the "file" key. * * @param string $file * @return $this */ public function outputFile( string $file ) { $this->file = $file; return $this; } /** * Add a section with a source map which was encoded in the "raw" JSON format. * * @param string $mapJson The JSON-encoded source map. * @param IndexMapOffset $generatedSize The size of the generated output * corresponding to $mapJson. This is used to advance the current offset * and will be used to calculate the offset of the next section, if there * is one. * @return $this */ public function addEncodedMap( string $mapJson, IndexMapOffset $generatedSize ) { $this->sections[] = '{"offset":' . json_encode( [ 'line' => $this->offset->line, 'column' => $this->offset->column, ] ) . ',"map":' . $mapJson . '}'; $this->offset->add( $generatedSize ); return $this; } /** * Get the index map, encoded as JSON * * @return string */ public function getMap(): string { $map = "{\n" . "\"version\": 3,\n"; if ( $this->file !== null ) { $map .= '"file": ' . json_encode( $this->file ) . ",\n"; } $map .= "\"sections\": [\n"; if ( $this->sections ) { $map .= implode( ",\n", $this->sections ) . "\n"; } $map .= "]\n}"; return $map; } }