/// <summary> /// Gets a source mapping for the given filename. Line numbers are zero based. /// </summary> internal SourceMapInfo MapToOriginal(string filename, int line, int column = 0) { JavaScriptSourceMapInfo mapInfo = TryGetMapInfo(filename); if (mapInfo != null) { SourceMapInfo mapping; if (line < mapInfo.Lines.Length) { string lineText = mapInfo.Lines[line]; // map to the 1st non-whitespace character on the line // This ensures we get the correct line number, mapping to column 0 // can give us the previous line. if (!String.IsNullOrWhiteSpace(lineText)) { for (; column < lineText.Length; column++) { if (!Char.IsWhiteSpace(lineText[column])) { break; } } } } if (mapInfo.Map.TryMapPoint(line, column, out mapping)) { return(mapping); } } return(null); }
/// <summary> /// Given a filename finds the original filename /// </summary> /// <param name="filename">the mapped filename</param> /// <returns>The original filename /// null - if the file is not mapped /// </returns> internal string MapToOriginal(string filename) { JavaScriptSourceMapInfo mapInfo = TryGetMapInfo(filename); if (mapInfo != null && mapInfo.Map != null && mapInfo.Map.Sources.Count > 0) { return(mapInfo.Map.Sources[0]); } return(null); }
private JavaScriptSourceMapInfo TryGetMapInfo(string filename) { if (!this._originalFileToSourceMap.TryGetValue(filename, out var mapInfo)) { if (File.Exists(filename)) { var contents = File.ReadAllLines(filename); const string marker = "# sourceMappingURL="; int markerStart; var markerLine = contents.Reverse().FirstOrDefault(x => x.IndexOf(marker, StringComparison.Ordinal) != -1); if (markerLine != null && (markerStart = markerLine.IndexOf(marker, StringComparison.Ordinal)) != -1) { var sourceMapFilename = markerLine.Substring(markerStart + marker.Length).Trim(); try { if (!File.Exists(sourceMapFilename)) { sourceMapFilename = Path.Combine(Path.GetDirectoryName(filename) ?? string.Empty, Path.GetFileName(sourceMapFilename)); } if (File.Exists(sourceMapFilename)) { using (var reader = new StreamReader(sourceMapFilename)) { var sourceMap = new SourceMap(reader); this._originalFileToSourceMap[filename] = mapInfo = new JavaScriptSourceMapInfo(sourceMap, contents); // clear all of our cached _generatedFileToSourceMap files... foreach (var cachedInvalid in this._generatedFileToSourceMap.Where(x => x.Value == null).Select(x => x.Key).ToArray()) { this._generatedFileToSourceMap.Remove(cachedInvalid); } } } } catch (ArgumentException) { } catch (PathTooLongException) { } catch (NotSupportedException) { } catch (InvalidOperationException) { } } } } return(mapInfo); }
private JavaScriptSourceMapInfo TryGetMapInfo(string filename) { JavaScriptSourceMapInfo mapInfo; if (!_originalFileToSourceMap.TryGetValue(filename, out mapInfo)) { if (File.Exists(filename)) { string[] contents = File.ReadAllLines(filename); const string marker = "# sourceMappingURL="; int markerStart; string markerLine = contents.Reverse().FirstOrDefault(x => x.IndexOf(marker, StringComparison.Ordinal) != -1); if (markerLine != null && (markerStart = markerLine.IndexOf(marker, StringComparison.Ordinal)) != -1) { string sourceMapFilename = markerLine.Substring(markerStart + marker.Length).Trim(); try { if (!File.Exists(sourceMapFilename)) { sourceMapFilename = Path.Combine(Path.GetDirectoryName(filename) ?? string.Empty, Path.GetFileName(sourceMapFilename)); } } catch (ArgumentException) { } catch (PathTooLongException) { } try { if (File.Exists(sourceMapFilename)) { using (StreamReader reader = new StreamReader(sourceMapFilename)) { var sourceMap = new SourceMap(reader); _originalFileToSourceMap[filename] = mapInfo = new JavaScriptSourceMapInfo(sourceMap, contents); // clear all of our cached _generatedFileToSourceMap files... foreach (var cachedInvalid in _generatedFileToSourceMap.Where(x => x.Value == null).Select(x => x.Key).ToArray()) { _generatedFileToSourceMap.Remove(cachedInvalid); } } } } catch (ArgumentException) { } catch (PathTooLongException) { } catch (NotSupportedException) { } catch (InvalidOperationException) { } } } } return mapInfo; }