diff options
author | Minteck <contact@minteck.org> | 2022-10-18 08:59:09 +0200 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2022-10-18 08:59:09 +0200 |
commit | 2c4ae43e688a9873e86211ea0e7aeb9ba770dd77 (patch) | |
tree | 17848d95522dab25d3cdeb9c4a6450e2a234861f /alarm/node_modules/graphql/language/printLocation.js.flow | |
parent | 108525534c28013cfe1897c30e4565f9893f3766 (diff) | |
download | pluralconnect-2c4ae43e688a9873e86211ea0e7aeb9ba770dd77.tar.gz pluralconnect-2c4ae43e688a9873e86211ea0e7aeb9ba770dd77.tar.bz2 pluralconnect-2c4ae43e688a9873e86211ea0e7aeb9ba770dd77.zip |
Update
Diffstat (limited to 'alarm/node_modules/graphql/language/printLocation.js.flow')
-rw-r--r-- | alarm/node_modules/graphql/language/printLocation.js.flow | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/alarm/node_modules/graphql/language/printLocation.js.flow b/alarm/node_modules/graphql/language/printLocation.js.flow new file mode 100644 index 0000000..db3bf91 --- /dev/null +++ b/alarm/node_modules/graphql/language/printLocation.js.flow @@ -0,0 +1,88 @@ +// @flow strict +import type { Source } from './source'; +import type { Location } from './ast'; +import type { SourceLocation } from './location'; +import { getLocation } from './location'; + +/** + * Render a helpful description of the location in the GraphQL Source document. + */ +export function printLocation(location: Location): string { + return printSourceLocation( + location.source, + getLocation(location.source, location.start), + ); +} + +/** + * Render a helpful description of the location in the GraphQL Source document. + */ +export function printSourceLocation( + source: Source, + sourceLocation: SourceLocation, +): string { + const firstLineColumnOffset = source.locationOffset.column - 1; + const body = whitespace(firstLineColumnOffset) + source.body; + + const lineIndex = sourceLocation.line - 1; + const lineOffset = source.locationOffset.line - 1; + const lineNum = sourceLocation.line + lineOffset; + + const columnOffset = sourceLocation.line === 1 ? firstLineColumnOffset : 0; + const columnNum = sourceLocation.column + columnOffset; + const locationStr = `${source.name}:${lineNum}:${columnNum}\n`; + + const lines = body.split(/\r\n|[\n\r]/g); + const locationLine = lines[lineIndex]; + + // Special case for minified documents + if (locationLine.length > 120) { + const subLineIndex = Math.floor(columnNum / 80); + const subLineColumnNum = columnNum % 80; + const subLines = []; + for (let i = 0; i < locationLine.length; i += 80) { + subLines.push(locationLine.slice(i, i + 80)); + } + + return ( + locationStr + + printPrefixedLines([ + [`${lineNum}`, subLines[0]], + ...subLines.slice(1, subLineIndex + 1).map((subLine) => ['', subLine]), + [' ', whitespace(subLineColumnNum - 1) + '^'], + ['', subLines[subLineIndex + 1]], + ]) + ); + } + + return ( + locationStr + + printPrefixedLines([ + // Lines specified like this: ["prefix", "string"], + [`${lineNum - 1}`, lines[lineIndex - 1]], + [`${lineNum}`, locationLine], + ['', whitespace(columnNum - 1) + '^'], + [`${lineNum + 1}`, lines[lineIndex + 1]], + ]) + ); +} + +function printPrefixedLines(lines: $ReadOnlyArray<[string, string]>): string { + const existingLines = lines.filter(([_, line]) => line !== undefined); + + const padLen = Math.max(...existingLines.map(([prefix]) => prefix.length)); + return existingLines + .map( + ([prefix, line]) => + leftPad(padLen, prefix) + (line ? ' | ' + line : ' |'), + ) + .join('\n'); +} + +function whitespace(len: number): string { + return Array(len + 1).join(' '); +} + +function leftPad(len: number, str: string): string { + return whitespace(len - str.length) + str; +} |