position-detector.js 950 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. var SourceMapConsumer = require('source-map').SourceMapConsumer;
  3. var extend = require('xtend');
  4. function isEmptyMapping (pos) {
  5. return ['source', 'line', 'column', 'name'].every(function (prop) {
  6. return pos[prop] === null;
  7. });
  8. }
  9. function PositionDetector (sourceMap) {
  10. if (sourceMap) {
  11. this.sourceMapConsumer = new SourceMapConsumer(sourceMap);
  12. }
  13. }
  14. PositionDetector.prototype.positionFor = function (currentNode) {
  15. var currentPosition = {
  16. source: currentNode.loc.source,
  17. line: currentNode.loc.start.line,
  18. column: currentNode.loc.start.column
  19. };
  20. if (this.sourceMapConsumer) {
  21. var found = this.sourceMapConsumer.originalPositionFor(currentPosition);
  22. if (found && !isEmptyMapping(found)) {
  23. return extend({ mapped: true }, found);
  24. }
  25. }
  26. return extend({ mapped: false }, currentPosition);
  27. };
  28. module.exports = PositionDetector;