location.js 635 B

12345678910111213141516171819202122232425262728293031
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getLocation = getLocation;
  6. /**
  7. * Represents a location in a Source.
  8. */
  9. /**
  10. * Takes a Source and a UTF-8 character offset, and returns the corresponding
  11. * line and column as a SourceLocation.
  12. */
  13. function getLocation(source, position) {
  14. var lineRegexp = /\r\n|[\n\r]/g;
  15. var line = 1;
  16. var column = position + 1;
  17. var match;
  18. while ((match = lineRegexp.exec(source.body)) && match.index < position) {
  19. line += 1;
  20. column = position + 1 - (match.index + match[0].length);
  21. }
  22. return {
  23. line: line,
  24. column: column
  25. };
  26. }