LineToLineMappedSource.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var SourceNode = require("source-map").SourceNode;
  6. var SourceListMap = require("source-list-map").SourceListMap;
  7. var Source = require("./Source");
  8. function LineToLineMappedSource(value, name, originalSource) {
  9. Source.call(this);
  10. this._value = value;
  11. this._name = name;
  12. this._originalSource = originalSource;
  13. }
  14. module.exports = LineToLineMappedSource;
  15. LineToLineMappedSource.prototype = Object.create(Source.prototype);
  16. LineToLineMappedSource.prototype.constructor = LineToLineMappedSource;
  17. LineToLineMappedSource.prototype.source = function() {
  18. return this._value;
  19. };
  20. require("./SourceAndMapMixin")(LineToLineMappedSource.prototype);
  21. LineToLineMappedSource.prototype.node = function(options) {
  22. var value = this._value;
  23. var name = this._name;
  24. var lines = value.split("\n");
  25. var node = new SourceNode(null, null, null,
  26. lines.map(function(line, idx) {
  27. return new SourceNode(idx+1, 0, name,
  28. (line + (idx != lines.length-1 ? "\n" : ""))
  29. );
  30. })
  31. );
  32. node.setSourceContent(name, this._originalSource);
  33. return node;
  34. };
  35. LineToLineMappedSource.prototype.listMap = function(options) {
  36. return new SourceListMap(this._value, this._name, this._originalSource)
  37. };
  38. LineToLineMappedSource.prototype.updateHash = function(hash) {
  39. hash.update(this._value);
  40. hash.update(this._originalSource);
  41. };