to-location.js 710 B

123456789101112131415161718192021
  1. 'use strict';
  2. /**
  3. Get location info for the given node or range.
  4. @param {import('estree').Node | number[]} nodeOrRange - The AST node or range to get the location for.
  5. @param {import('eslint').SourceCode} sourceCode - The source code object.
  6. @param {int} [startOffset] - Start position offset.
  7. @param {int} [endOffset] - End position offset.
  8. @returns {import('estree').SourceLocation}
  9. */
  10. function toLocation(nodeOrRange, sourceCode, startOffset = 0, endOffset = 0) {
  11. const [start, end] = Array.isArray(nodeOrRange) ? nodeOrRange : nodeOrRange.range;
  12. return {
  13. start: sourceCode.getLocFromIndex(start + startOffset),
  14. end: sourceCode.getLocFromIndex(end + endOffset),
  15. };
  16. }
  17. module.exports = toLocation;