UNPKG

graphql

Version:

A Query Language and Runtime which can target any service.

1 lines • 4.6 kB
{"version":3,"file":"location.mjs","sourceRoot":"","sources":["../../src/language/location.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,sCAAqC;AAI9D,MAAM,UAAU,GAAG,cAAc,CAAC;AAwBlC,MAAM,UAAU,WAAW,CAAC,MAAc,EAAE,QAAgB;IAC1D,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;IACjD,IAAI,KAAK,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;IACjC,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,aAAa,GAAG,CAAC,CAAC;IACtB,OAAO,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,KAAK,GAAG,QAAQ,EAAE,CAAC;QACrD,aAAa,GAAG,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;QAC9C,IAAI,EAAE,CAAC;QACP,KAAK,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;IAC/B,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,GAAG,CAAC,GAAG,aAAa,EAAE,CAAC;AACxD,CAAC;AAGD,MAAM,UAAU,YAAY,CAC1B,SAA2D;IAE3D,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACzB,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,WAAW,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC;IAC1E,CAAC;IACD,MAAM,QAAQ,GAAG,IAAI,cAAc,EAGhC,CAAC;IACJ,SAAS,CAAC,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,KAAK,EAAE,EAAE;QAC7C,QAAQ,CAAC,GAAG,CAAC,MAAM,EAAE;YACnB,QAAQ,EAAE,KAAK;YACf,KAAK;SACN,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IACH,MAAM,MAAM,GAAG,IAAI,KAAK,CAAiB,SAAS,CAAC,MAAM,CAAC,CAAC;IAC3D,KAAK,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,IAAI,QAAQ,EAAE,CAAC;QAC3C,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;QACjD,IAAI,KAAK,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;QACjC,IAAI,IAAI,GAAG,CAAC,CAAC;QACb,IAAI,aAAa,GAAG,CAAC,CAAC;QACtB,KAAK,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,SAAS,CAAC,QAAQ,CAClD,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC,QAAQ,CAClC,EAAE,CAAC;YACF,OAAO,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,KAAK,GAAG,QAAQ,EAAE,CAAC;gBACrD,aAAa,GAAG,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;gBAC9C,IAAI,EAAE,CAAC;gBACP,KAAK,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;YAC/B,CAAC;YACD,MAAM,CAAC,KAAK,CAAC,GAAG;gBACd,IAAI;gBACJ,MAAM,EAAE,QAAQ,GAAG,CAAC,GAAG,aAAa;aACrC,CAAC;QACJ,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["/** @category Source */\n\nimport { AccumulatorMap } from '../jsutils/AccumulatorMap.ts';\n\nimport type { Source } from './source.ts';\n\nconst LineRegExp = /\\r\\n|[\\n\\r]/g;\n\n/** Represents a location in a Source. */\nexport interface SourceLocation {\n /** One-indexed line number in the source document. */\n readonly line: number;\n /** One-indexed column number in the source document. */\n readonly column: number;\n}\n\n/**\n * Takes a Source and a UTF-8 character offset, and returns the corresponding\n * line and column as a SourceLocation.\n * @param source - The source document that contains the position.\n * @param position - The UTF-8 character offset in the source body.\n * @returns The 1-indexed line and column for the given source position.\n * @example\n * ```ts\n * import { Source, getLocation } from 'graphql/language';\n *\n * const source = new Source('type Query { hello: String }');\n * const location = getLocation(source, 13); // => { line: 1, column: 14 }\n * ```\n */\nexport function getLocation(source: Source, position: number): SourceLocation {\n const matches = source.body.matchAll(LineRegExp);\n let match = matches.next().value;\n let line = 1;\n let lastLineStart = 0;\n while (match !== undefined && match.index < position) {\n lastLineStart = match.index + match[0].length;\n line++;\n match = matches.next().value;\n }\n return { line, column: position + 1 - lastLineStart };\n}\n\n/** @internal */\nexport function getLocations(\n locations: ReadonlyArray<{ source: Source; start: number }>,\n): Array<SourceLocation> {\n if (locations.length < 2) {\n return locations.map(({ source, start }) => getLocation(source, start));\n }\n const bySource = new AccumulatorMap<\n Source,\n { position: number; index: number }\n >();\n locations.forEach(({ source, start }, index) => {\n bySource.add(source, {\n position: start,\n index,\n });\n });\n const result = new Array<SourceLocation>(locations.length);\n for (const [source, positions] of bySource) {\n const matches = source.body.matchAll(LineRegExp);\n let match = matches.next().value;\n let line = 1;\n let lastLineStart = 0;\n for (const { position, index } of positions.toSorted(\n (a, b) => a.position - b.position,\n )) {\n while (match !== undefined && match.index < position) {\n lastLineStart = match.index + match[0].length;\n line++;\n match = matches.next().value;\n }\n result[index] = {\n line,\n column: position + 1 - lastLineStart,\n };\n }\n }\n return result;\n}\n"]}