MCPcopy
hub / github.com/tailwindlabs/tailwindcss / parseSourceMap

Function parseSourceMap

integrations/utils.ts:827–870  ·  view source on GitHub ↗
(opts: string | SourceMapOptions)

Source from the content-addressed store, hash-verified

825}
826
827function parseSourceMap(opts: string | SourceMapOptions): SourceMap {
828 if (typeof opts === 'string') {
829 let lines = opts.trimEnd().split('\n')
830 let comment = lines.at(-1) ?? ''
831 let map = String(comment).match(SOURCE_MAP_COMMENT)?.[1] ?? null
832 if (!map) throw new Error('No source map comment found')
833
834 return parseSourceMap({
835 map,
836 content: lines.slice(0, -1).join('\n'),
837 encoding: 'base64',
838 })
839 }
840
841 let rawMap: RawSourceMap
842 let content = opts.content
843
844 if (typeof opts.map === 'object') {
845 rawMap = opts.map as RawSourceMap
846 } else {
847 rawMap = JSON.parse(Buffer.from(opts.map, opts.encoding ?? 'utf-8').toString())
848 }
849
850 let map = new SourceMapConsumer(rawMap)
851 let generatedTable = createLineTable(content)
852
853 return {
854 at(line: number, column: number) {
855 let pos = map.originalPositionFor({ line, column })
856 let source = pos.source ? map.sourceContentFor(pos.source) : null
857 let originalTable = createLineTable(source ?? '')
858 let originalOffset = originalTable.findOffset(pos)
859 let generatedOffset = generatedTable.findOffset({ line, column })
860
861 return {
862 source: pos.source,
863 original: source
864 ? source.slice(originalOffset, originalOffset + 10).trim() + '...'
865 : '(none)',
866 generated: content.slice(generatedOffset, generatedOffset + 10).trim() + '...',
867 }
868 },
869 }
870}

Callers 3

index.test.tsFile · 0.85
index.test.tsFile · 0.85

Calls 2

createLineTableFunction · 0.90
atMethod · 0.80

Tested by

no test coverage detected