UNPKG

@dillonkearns/elm-graphql

Version:

<img src="https://cdn.jsdelivr.net/gh/martimatix/logo-graphqelm/logo.svg" alt="dillonearns/elm-graphql logo" width="40%" align="right">

62 lines (53 loc) 2.26 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.syntaxError = syntaxError; var _location = require('../language/location'); var _GraphQLError = require('./GraphQLError'); /** * Produces a GraphQLError representing a syntax error, containing useful * descriptive information about the syntax error's position in the source. */ /** * Copyright (c) 2015-present, Facebook, Inc. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * */ function syntaxError(source, position, description) { var location = (0, _location.getLocation)(source, position); var line = location.line + source.locationOffset.line - 1; var columnOffset = getColumnOffset(source, location); var column = location.column + columnOffset; var error = new _GraphQLError.GraphQLError('Syntax Error ' + source.name + ' (' + line + ':' + column + ') ' + description + '\n\n' + highlightSourceAtLocation(source, location), undefined, source, [position]); return error; } /** * Render a helpful description of the location of the error in the GraphQL * Source document. */ function highlightSourceAtLocation(source, location) { var line = location.line; var lineOffset = source.locationOffset.line - 1; var columnOffset = getColumnOffset(source, location); var contextLine = line + lineOffset; var prevLineNum = (contextLine - 1).toString(); var lineNum = contextLine.toString(); var nextLineNum = (contextLine + 1).toString(); var padLen = nextLineNum.length; var lines = source.body.split(/\r\n|[\n\r]/g); lines[0] = whitespace(source.locationOffset.column - 1) + lines[0]; return (line >= 2 ? lpad(padLen, prevLineNum) + ': ' + lines[line - 2] + '\n' : '') + lpad(padLen, lineNum) + ': ' + lines[line - 1] + '\n' + whitespace(2 + padLen + location.column - 1 + columnOffset) + '^\n' + (line < lines.length ? lpad(padLen, nextLineNum) + ': ' + lines[line] + '\n' : ''); } function getColumnOffset(source, location) { return location.line === 1 ? source.locationOffset.column - 1 : 0; } function whitespace(len) { return Array(len + 1).join(' '); } function lpad(len, str) { return whitespace(len - str.length) + str; }