@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
90 lines (84 loc) • 4.22 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); 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 _nodepath = require('node:path');
var _graphqltagpluck = require('@graphql-tools/graphql-tag-pluck');
var _utils = require('@graphql-tools/utils');
var _graphqlconfigjs = require('./graphql-config.js');
var _metajs = require('./meta.js');
var _utilsjs = require('./utils.js');
const blocksMap = /* @__PURE__ */ new Map();
let onDiskConfig, onDiskConfigLoaded = !1;
const RELEVANT_KEYWORDS = ["gql", "graphql", "GraphQL"], processor = exports.processor = {
meta: {
name: "@graphql-eslint/processor",
version: _metajs.version
},
supportsAutofix: !0,
preprocess(code, filePath) {
if (process.env.ESLINT_USE_FLAT_CONFIG !== "false" && filePath.endsWith(".vue"))
throw new Error(
"Processing of `.vue` files is no longer supported, follow the new official vue example for ESLint's flat config https://github.com/dimaMachina/graphql-eslint/tree/master/examples/vue-code-file"
);
onDiskConfigLoaded || (onDiskConfig = _graphqlconfigjs.loadOnDiskGraphQLConfig.call(void 0, filePath), onDiskConfigLoaded = !0);
let keywords = RELEVANT_KEYWORDS;
const pluckConfig = _optionalChain([onDiskConfig, 'optionalAccess', _ => _.getProjectForFile, 'call', _2 => _2(filePath), 'access', _3 => _3.extensions, 'access', _4 => _4.pluckConfig]);
if (pluckConfig) {
const {
modules = [],
globalGqlIdentifierName = ["gql", "graphql"],
gqlMagicComment = "GraphQL"
} = pluckConfig;
keywords = [
...new Set(
[
...modules.map(({ identifier }) => identifier),
..._utils.asArray.call(void 0, globalGqlIdentifierName),
gqlMagicComment
].filter(_utilsjs.truthy)
)
];
}
if (keywords.every((keyword) => !code.includes(keyword)))
return [code];
try {
const blocks = _graphqltagpluck.gqlPluckFromCodeStringSync.call(void 0, filePath, code, {
skipIndent: !0,
...pluckConfig
}).map((item) => ({
filename: "document.graphql",
text: item.body,
lineOffset: item.locationOffset.line - 1,
// @ts-expect-error -- `index` field exist but show ts error
offset: item.locationOffset.index + 1
}));
return blocksMap.set(filePath, blocks), [
...blocks,
code
/* source code must be provided and be last */
];
} catch (error) {
return error instanceof Error && (error.message = `[graphql-eslint] Error while preprocessing "${_nodepath.relative.call(void 0,
_utilsjs.CWD,
filePath
)}" file
${error.message}`), console.error(error), [code];
}
},
postprocess(messages, filePath) {
const blocks = blocksMap.get(filePath) || [];
for (let i = 0; i < blocks.length; i += 1) {
const { lineOffset, offset } = blocks[i];
for (const message of messages[i] || []) {
if (/\.(vue|svelte)$/.test(filePath)) {
delete message.endLine, delete message.endColumn, delete message.fix, delete message.suggestions, Object.assign(message, _utilsjs.REPORT_ON_FIRST_CHARACTER);
continue;
}
message.line += lineOffset, typeof message.endLine == "number" && (message.endLine += lineOffset), message.fix && (message.fix.range[0] += offset, message.fix.range[1] += offset);
for (const suggestion of message.suggestions || []) {
const [start, end] = suggestion.fix.range;
suggestion.fix.range = [start + offset, end + offset];
}
}
}
return messages.flat().sort((a, b) => a.line - b.line || a.column - b.column);
}
};
exports.processor = processor;