location.mjs 526 B

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