UNPKG

@graphql-eslint/eslint-plugin

Version:
84 lines (83 loc) 2.98 kB
import { Kind } from 'graphql'; import lowerCase from 'lodash.lowercase'; import chalk from 'chalk'; export function requireSiblingsOperations(ruleId, context) { const { siblingOperations } = context.parserServices; if (!siblingOperations.available) { throw new Error(`Rule \`${ruleId}\` requires \`parserOptions.operations\` to be set and loaded. See https://bit.ly/graphql-eslint-operations for more info`); } return siblingOperations; } export function requireGraphQLSchemaFromContext(ruleId, context) { const { schema } = context.parserServices; if (!schema) { throw new Error(`Rule \`${ruleId}\` requires \`parserOptions.schema\` to be set and loaded. See https://bit.ly/graphql-eslint-schema for more info`); } else if (schema instanceof Error) { throw schema; } return schema; } export const logger = { // eslint-disable-next-line no-console error: (...args) => console.error(chalk.red('error'), '[graphql-eslint]', chalk(...args)), // eslint-disable-next-line no-console warn: (...args) => console.warn(chalk.yellow('warning'), '[graphql-eslint]', chalk(...args)), }; export const normalizePath = (path) => (path || '').replace(/\\/g, '/'); export const VIRTUAL_DOCUMENT_REGEX = /\/\d+_document.graphql$/; export const CWD = process.cwd(); export const getTypeName = (node) => 'type' in node ? getTypeName(node.type) : node.name.value; export const TYPES_KINDS = [ Kind.OBJECT_TYPE_DEFINITION, Kind.INTERFACE_TYPE_DEFINITION, Kind.ENUM_TYPE_DEFINITION, Kind.SCALAR_TYPE_DEFINITION, Kind.INPUT_OBJECT_TYPE_DEFINITION, Kind.UNION_TYPE_DEFINITION, ]; export const pascalCase = (str) => lowerCase(str) .split(' ') .map(word => word.charAt(0).toUpperCase() + word.slice(1)) .join(''); export const camelCase = (str) => { const result = pascalCase(str); return result.charAt(0).toLowerCase() + result.slice(1); }; export const convertCase = (style, str) => { switch (style) { case 'camelCase': return camelCase(str); case 'PascalCase': return pascalCase(str); case 'snake_case': return lowerCase(str).replace(/ /g, '_'); case 'UPPER_CASE': return lowerCase(str).replace(/ /g, '_').toUpperCase(); case 'kebab-case': return lowerCase(str).replace(/ /g, '-'); } }; export function getLocation(start, fieldName = '') { const { line, column } = start; return { start: { line, column, }, end: { line, column: column + fieldName.length, }, }; } export const REPORT_ON_FIRST_CHARACTER = { column: 0, line: 1 }; export const ARRAY_DEFAULT_OPTIONS = { type: 'array', uniqueItems: true, minItems: 1, items: { type: 'string', }, }; export const englishJoinWords = words => new Intl.ListFormat('en-US', { type: 'disjunction' }).format(words);