/** * @typedef {import('unist').Point} Point * @typedef {import('../types.js').TrackFields} TrackFields */ /** * Functions to track output positions. * This info isn’t used yet but suchs functionality allows line wrapping, * and theoretically source maps (though, is there practical use in that?). * * @param {TrackFields} options_ */ export function track(options_) { // Defaults are used to prevent crashes when older utilities somehow activate // this code. /* c8 ignore next 5 */ const options = options_ || {} const now = options.now || {} let lineShift = options.lineShift || 0 let line = now.line || 1 let column = now.column || 1 return {move, current, shift} /** * Get the current tracked info. * * @returns {{now: Point, lineShift: number}} */ function current() { return {now: {line, column}, lineShift} } /** * Define an increased line shift (the typical indent for lines). * * @param {number} value */ function shift(value) { lineShift += value } /** * Move past a string. * * @param {string} value * @returns {string} */ function move(value = '') { const chunks = value.split(/\r?\n|\r/g) const tail = chunks[chunks.length - 1] line += chunks.length - 1 column = chunks.length === 1 ? column + tail.length : 1 + tail.length + lineShift return value } }