UNPKG

@graphql-eslint/eslint-plugin

Version:
53 lines (48 loc) 2.97 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _nodefs = require('node:fs'); var _nodefs2 = _interopRequireDefault(_nodefs); var _nodepath = require('node:path'); var _nodepath2 = _interopRequireDefault(_nodepath); var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug); var _graphqlconfig = require('graphql-config'); var _codefileloader = require('@graphql-tools/code-file-loader'); const debug = _debug2.default.call(void 0, "graphql-eslint:graphql-config"); let graphQLConfig; function getFirstExistingPath(filePath) { while (!_nodefs2.default.existsSync(filePath)) { filePath = _nodepath2.default.dirname(filePath); } return filePath; } function loadOnDiskGraphQLConfig(filePath) { return _graphqlconfig.loadConfigSync.call(void 0, { // load config relative to the file being linted rootDir: getFirstExistingPath(_nodepath2.default.dirname(filePath)), throwOnMissing: false, extensions: [codeFileLoaderExtension] }); } function loadGraphQLConfig({ graphQLConfig: config, filePath }) { if (process.env.NODE_ENV !== "test" && graphQLConfig) { return graphQLConfig; } debug("parserOptions.graphQLConfig: %o", config); const onDiskConfig = !config && loadOnDiskGraphQLConfig(filePath); if (onDiskConfig) { debug("GraphQL-Config path %o", onDiskConfig.filepath); } const configOptions = config && ("projects" in config || "schemaPath" in config) ? config : { // if `schema` is `undefined` will throw error `Project 'default' not found` schema: _nullishCoalesce(_optionalChain([config, 'optionalAccess', _ => _.schema]), () => ( "")), ...config }; graphQLConfig = onDiskConfig || new (0, _graphqlconfig.GraphQLConfig)({ config: configOptions, filepath: "" }, [codeFileLoaderExtension]); return graphQLConfig; } const codeFileLoaderExtension = (api) => { const { schema, documents } = api.loaders; schema.register(new (0, _codefileloader.CodeFileLoader)()); documents.register(new (0, _codefileloader.CodeFileLoader)()); return { name: "code-file-loaders" }; }; exports.getFirstExistingPath = getFirstExistingPath; exports.loadGraphQLConfig = loadGraphQLConfig; exports.loadOnDiskGraphQLConfig = loadOnDiskGraphQLConfig;