@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
97 lines (96 loc) • 4.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.englishJoinWords = exports.ARRAY_DEFAULT_OPTIONS = exports.REPORT_ON_FIRST_CHARACTER = exports.getLocation = exports.convertCase = exports.camelCase = exports.pascalCase = exports.TYPES_KINDS = exports.getTypeName = exports.CWD = exports.VIRTUAL_DOCUMENT_REGEX = exports.normalizePath = exports.logger = exports.requireGraphQLSchemaFromContext = exports.requireSiblingsOperations = void 0;
const tslib_1 = require("tslib");
const graphql_1 = require("graphql");
const lodash_lowercase_1 = tslib_1.__importDefault(require("lodash.lowercase"));
const chalk_1 = tslib_1.__importDefault(require("chalk"));
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;
}
exports.requireSiblingsOperations = requireSiblingsOperations;
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;
}
exports.requireGraphQLSchemaFromContext = requireGraphQLSchemaFromContext;
exports.logger = {
// eslint-disable-next-line no-console
error: (...args) => console.error(chalk_1.default.red('error'), '[graphql-eslint]', (0, chalk_1.default)(...args)),
// eslint-disable-next-line no-console
warn: (...args) => console.warn(chalk_1.default.yellow('warning'), '[graphql-eslint]', (0, chalk_1.default)(...args)),
};
const normalizePath = (path) => (path || '').replace(/\\/g, '/');
exports.normalizePath = normalizePath;
exports.VIRTUAL_DOCUMENT_REGEX = /\/\d+_document.graphql$/;
exports.CWD = process.cwd();
const getTypeName = (node) => 'type' in node ? (0, exports.getTypeName)(node.type) : node.name.value;
exports.getTypeName = getTypeName;
exports.TYPES_KINDS = [
graphql_1.Kind.OBJECT_TYPE_DEFINITION,
graphql_1.Kind.INTERFACE_TYPE_DEFINITION,
graphql_1.Kind.ENUM_TYPE_DEFINITION,
graphql_1.Kind.SCALAR_TYPE_DEFINITION,
graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION,
graphql_1.Kind.UNION_TYPE_DEFINITION,
];
const pascalCase = (str) => (0, lodash_lowercase_1.default)(str)
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join('');
exports.pascalCase = pascalCase;
const camelCase = (str) => {
const result = (0, exports.pascalCase)(str);
return result.charAt(0).toLowerCase() + result.slice(1);
};
exports.camelCase = camelCase;
const convertCase = (style, str) => {
switch (style) {
case 'camelCase':
return (0, exports.camelCase)(str);
case 'PascalCase':
return (0, exports.pascalCase)(str);
case 'snake_case':
return (0, lodash_lowercase_1.default)(str).replace(/ /g, '_');
case 'UPPER_CASE':
return (0, lodash_lowercase_1.default)(str).replace(/ /g, '_').toUpperCase();
case 'kebab-case':
return (0, lodash_lowercase_1.default)(str).replace(/ /g, '-');
}
};
exports.convertCase = convertCase;
function getLocation(start, fieldName = '') {
const { line, column } = start;
return {
start: {
line,
column,
},
end: {
line,
column: column + fieldName.length,
},
};
}
exports.getLocation = getLocation;
exports.REPORT_ON_FIRST_CHARACTER = { column: 0, line: 1 };
exports.ARRAY_DEFAULT_OPTIONS = {
type: 'array',
uniqueItems: true,
minItems: 1,
items: {
type: 'string',
},
};
const englishJoinWords = words => new Intl.ListFormat('en-US', { type: 'disjunction' }).format(words);
exports.englishJoinWords = englishJoinWords;