|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +/* ------------------------------------------------------------------------ */ |
| 4 | + |
| 5 | +const O = Object, |
| 6 | + isBrowser = (typeof window !== 'undefined') && (window.window === window) && window.navigator, |
| 7 | + SourceMapConsumer = require ('source-map').SourceMapConsumer, |
| 8 | + path = require ('./impl/path'), |
| 9 | + memoize = require ('lodash.memoize'), |
| 10 | + lastOf = x => x[x.length - 1] |
| 11 | + |
| 12 | +/* ------------------------------------------------------------------------ */ |
| 13 | + |
| 14 | +const newSourceFileMemoized = memoize (file => new SourceFile (file)) |
| 15 | + |
| 16 | +const getSource = module.exports = file => newSourceFileMemoized (path.normalize (file)) |
| 17 | + |
| 18 | +/* ------------------------------------------------------------------------ */ |
| 19 | + |
| 20 | +class SourceMap { |
| 21 | + |
| 22 | + constructor (originalFilePath, sourceMapPath) { |
| 23 | + |
| 24 | + this.file = getSource (path.relativeToFile (originalFilePath, sourceMapPath)) |
| 25 | + this.parsed = (this.file.text && SourceMapConsumer (JSON.parse (this.file.text))) || null |
| 26 | + this.sourceFor = memoize (this.sourceFor.bind (this)) |
| 27 | + } |
| 28 | + |
| 29 | + sourceFor (file) { |
| 30 | + const content = this.parsed.sourceContentFor (file, true /* return null on missing */) |
| 31 | + const fullPath = path.relativeToFile (this.file.path, file) |
| 32 | + return content ? new SourceFile (fullPath, content) : getSource (fullPath) |
| 33 | + } |
| 34 | + |
| 35 | + resolve (loc) { |
| 36 | + |
| 37 | + const originalLoc = this.parsed.originalPositionFor (loc) |
| 38 | + |
| 39 | + return this.sourceFor (originalLoc.source) |
| 40 | + .resolve (O.assign ({}, loc, { |
| 41 | + line: originalLoc.line, |
| 42 | + column: originalLoc.column, |
| 43 | + name: originalLoc.name })) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/* ------------------------------------------------------------------------ */ |
| 48 | + |
| 49 | +class SourceFile { |
| 50 | + |
| 51 | + constructor (path, text /* optional */) { |
| 52 | + |
| 53 | + this.path = path |
| 54 | + |
| 55 | + if (text) { |
| 56 | + this.text = text } |
| 57 | + |
| 58 | + else { |
| 59 | + try { |
| 60 | + if (isBrowser) { |
| 61 | + |
| 62 | + let xhr = new XMLHttpRequest () |
| 63 | + |
| 64 | + xhr.open ('GET', path, false /* SYNCHRONOUS XHR FTW :) */) |
| 65 | + xhr.send (null) |
| 66 | + |
| 67 | + this.text = xhr.responseText } |
| 68 | + |
| 69 | + else { |
| 70 | + this.text = require ('fs').readFileSync (path, { encoding: 'utf8' }) } } |
| 71 | + |
| 72 | + catch (e) { |
| 73 | + this.error = e |
| 74 | + this.text = '' } } |
| 75 | + } |
| 76 | + |
| 77 | + get lines () { |
| 78 | + return (this.lines_ = this.lines_ || this.text.split ('\n')) |
| 79 | + } |
| 80 | + |
| 81 | + get sourceMap () { |
| 82 | + |
| 83 | + try { |
| 84 | + if (this.sourceMap_ === undefined) { |
| 85 | + let url = this.text.match (/\# sourceMappingURL=(.+\.map)/) |
| 86 | + if (url = (url && url[1])) { |
| 87 | + this.sourceMap_ = new SourceMap (this.path, url) } } } |
| 88 | + |
| 89 | + catch (e) { |
| 90 | + this.sourceMapError = e |
| 91 | + this.sourceMap_ = null } |
| 92 | + |
| 93 | + return this.sourceMap_ |
| 94 | + } |
| 95 | + |
| 96 | + resolve (loc /* { line, column } */) /* → { line, column, sourceFile, sourceLine } */ { |
| 97 | + |
| 98 | + return this.sourceMap ? this.sourceMap.resolve (loc) : O.assign ({}, loc, { |
| 99 | + |
| 100 | + sourceFile: this, |
| 101 | + sourceLine: (this.lines[loc.line - 1] || ''), |
| 102 | + error: this.error |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/* ------------------------------------------------------------------------ */ |
0 commit comments