UNPKG

@0no-co/graphqlsp

Version:

TypeScript LSP plugin that finds GraphQL documents in your code and provides hints and auto-generates types.

1,997 lines (1,872 loc) 111 kB
import { Kind as e, specifiedRules as t, NoUnusedFragmentsRule as i, ExecutableDefinitionsRule as r, KnownFragmentNamesRule as a, validate as n, LoneSchemaDefinitionRule as s, UniqueOperationTypesRule as o, UniqueTypeNamesRule as l, UniqueEnumValueNamesRule as u, UniqueFieldDefinitionNamesRule as f, UniqueDirectiveNamesRule as c, KnownTypeNamesRule as h, KnownDirectivesRule as p, UniqueDirectivesPerLocationRule as d, PossibleTypeExtensionsRule as v, UniqueArgumentNamesRule as g, UniqueInputFieldNamesRule as m, print as S, parse as y, GraphQLError as x, NoDeprecatedCustomRule as T, visit as E } from "graphql"; import { createHash as A } from "crypto"; var b; function init(e) { b = e.typescript; } class CharacterStream { constructor(e) { this._start = 0; this._pos = 0; this.getStartOfToken = () => this._start; this.getCurrentPosition = () => this._pos; this.eol = () => this._sourceText.length === this._pos; this.sol = () => 0 === this._pos; this.peek = () => this._sourceText.charAt(this._pos) || null; this.next = () => { var e = this._sourceText.charAt(this._pos); this._pos++; return e; }; this.eat = e => { if (this._testNextCharacter(e)) { this._start = this._pos; this._pos++; return this._sourceText.charAt(this._pos - 1); } return; }; this.eatWhile = e => { var t = this._testNextCharacter(e); var i = !1; if (t) { i = t; this._start = this._pos; } while (t) { this._pos++; t = this._testNextCharacter(e); i = !0; } return i; }; this.eatSpace = () => this.eatWhile(/[\s\u00a0]/); this.skipToEnd = () => { this._pos = this._sourceText.length; }; this.skipTo = e => { this._pos = e; }; this.match = (e, t = !0, i = !1) => { var r = null; var a = null; if ("string" == typeof e) { a = new RegExp(e, i ? "i" : "g").test(this._sourceText.slice(this._pos, this._pos + e.length)); r = e; } else if (e instanceof RegExp) { r = null == (a = this._sourceText.slice(this._pos).match(e)) ? void 0 : a[0]; } if (null != a && ("string" == typeof e || a instanceof Array && this._sourceText.startsWith(a[0], this._pos))) { if (t) { this._start = this._pos; if (r && r.length) { this._pos += r.length; } } return a; } return !1; }; this.backUp = e => { this._pos -= e; }; this.column = () => this._pos; this.indentation = () => { var e = this._sourceText.match(/\s*/); var t = 0; if (e && 0 !== e.length) { var i = e[0]; var r = 0; while (i.length > r) { if (9 === i.charCodeAt(r)) { t += 2; } else { t++; } r++; } } return t; }; this.current = () => this._sourceText.slice(this._start, this._pos); this._sourceText = e; } _testNextCharacter(e) { var t = this._sourceText.charAt(this._pos); var i = !1; if ("string" == typeof e) { i = t === e; } else { i = e instanceof RegExp ? e.test(t) : e(t); } return i; } } function opt(e) { return { ofRule: e }; } function list(e, t) { return { ofRule: e, isList: !0, separator: t }; } function t$1(e, t) { return { style: t, match: t => t.kind === e }; } function p$1(e, t) { return { style: t || "punctuation", match: t => "Punctuation" === t.kind && t.value === e }; } var isIgnored = e => " " === e || "\t" === e || "," === e || "\n" === e || "\r" === e || "\ufeff" === e || " " === e; var w = { Name: /^[_A-Za-z][_0-9A-Za-z]*/, Punctuation: /^(?:!|\$|\(|\)|\.\.\.|:|=|&|@|\[|]|\{|\||\})/, Number: /^-?(?:0|(?:[1-9][0-9]*))(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?/, String: /^(?:"""(?:\\"""|[^"]|"[^"]|""[^"])*(?:""")?|"(?:[^"\\]|\\(?:"|\/|\\|b|f|n|r|t|u[0-9a-fA-F]{4}))*"?)/, Comment: /^#.*/ }; var D = { Document: [ list("Definition") ], Definition(t) { switch (t.value) { case "{": return "ShortQuery"; case "query": return "Query"; case "mutation": return "Mutation"; case "subscription": return "Subscription"; case "fragment": return e.FRAGMENT_DEFINITION; case "schema": return "SchemaDef"; case "scalar": return "ScalarDef"; case "type": return "ObjectTypeDef"; case "interface": return "InterfaceDef"; case "union": return "UnionDef"; case "enum": return "EnumDef"; case "input": return "InputDef"; case "extend": return "ExtendDef"; case "directive": return "DirectiveDef"; } }, ShortQuery: [ "SelectionSet" ], Query: [ word("query"), opt(name$1("def")), opt("VariableDefinitions"), list("Directive"), "SelectionSet" ], Mutation: [ word("mutation"), opt(name$1("def")), opt("VariableDefinitions"), list("Directive"), "SelectionSet" ], Subscription: [ word("subscription"), opt(name$1("def")), opt("VariableDefinitions"), list("Directive"), "SelectionSet" ], VariableDefinitions: [ p$1("("), list("VariableDefinition"), p$1(")") ], VariableDefinition: [ "Variable", p$1(":"), "Type", opt("DefaultValue") ], Variable: [ p$1("$", "variable"), name$1("variable") ], DefaultValue: [ p$1("="), "Value" ], SelectionSet: [ p$1("{"), list("Selection"), p$1("}") ], Selection: (e, t) => "..." === e.value ? t.match(/[\s\u00a0,]*(on\b|@|{)/, !1) ? "InlineFragment" : "FragmentSpread" : t.match(/[\s\u00a0,]*:/, !1) ? "AliasedField" : "Field", AliasedField: [ name$1("property"), p$1(":"), name$1("qualifier"), opt("Arguments"), list("Directive"), opt("SelectionSet") ], Field: [ name$1("property"), opt("Arguments"), list("Directive"), opt("SelectionSet") ], Arguments: [ p$1("("), list("Argument"), p$1(")") ], Argument: [ name$1("attribute"), p$1(":"), "Value" ], FragmentSpread: [ p$1("..."), name$1("def"), list("Directive") ], InlineFragment: [ p$1("..."), opt("TypeCondition"), list("Directive"), "SelectionSet" ], FragmentDefinition: [ word("fragment"), opt(function butNot(e, t) { var i = e.match; e.match = e => { var r = !1; if (i) { r = i(e); } return r && t.every(t => t.match && !t.match(e)); }; return e; }(name$1("def"), [ word("on") ])), "TypeCondition", list("Directive"), "SelectionSet" ], TypeCondition: [ word("on"), "NamedType" ], Value(e) { switch (e.kind) { case "Number": return "NumberValue"; case "String": return "StringValue"; case "Punctuation": switch (e.value) { case "[": return "ListValue"; case "{": return "ObjectValue"; case "$": return "Variable"; case "&": return "NamedType"; } return null; case "Name": switch (e.value) { case "true": case "false": return "BooleanValue"; } if ("null" === e.value) { return "NullValue"; } return "EnumValue"; } }, NumberValue: [ t$1("Number", "number") ], StringValue: [ { style: "string", match: e => "String" === e.kind, update(e, t) { if (t.value.startsWith('"""')) { e.inBlockstring = !t.value.slice(3).endsWith('"""'); } } } ], BooleanValue: [ t$1("Name", "builtin") ], NullValue: [ t$1("Name", "keyword") ], EnumValue: [ name$1("string-2") ], ListValue: [ p$1("["), list("Value"), p$1("]") ], ObjectValue: [ p$1("{"), list("ObjectField"), p$1("}") ], ObjectField: [ name$1("attribute"), p$1(":"), "Value" ], Type: e => "[" === e.value ? "ListType" : "NonNullType", ListType: [ p$1("["), "Type", p$1("]"), opt(p$1("!")) ], NonNullType: [ "NamedType", opt(p$1("!")) ], NamedType: [ function type$1(e) { return { style: e, match: e => "Name" === e.kind, update(e, t) { var i; if (null === (i = e.prevState) || void 0 === i ? void 0 : i.prevState) { e.name = t.value; e.prevState.prevState.type = t.value; } } }; }("atom") ], Directive: [ p$1("@", "meta"), name$1("meta"), opt("Arguments") ], DirectiveDef: [ word("directive"), p$1("@", "meta"), name$1("meta"), opt("ArgumentsDef"), word("on"), list("DirectiveLocation", p$1("|")) ], InterfaceDef: [ word("interface"), name$1("atom"), opt("Implements"), list("Directive"), p$1("{"), list("FieldDef"), p$1("}") ], Implements: [ word("implements"), list("NamedType", p$1("&")) ], DirectiveLocation: [ name$1("string-2") ], SchemaDef: [ word("schema"), list("Directive"), p$1("{"), list("OperationTypeDef"), p$1("}") ], OperationTypeDef: [ name$1("keyword"), p$1(":"), name$1("atom") ], ScalarDef: [ word("scalar"), name$1("atom"), list("Directive") ], ObjectTypeDef: [ word("type"), name$1("atom"), opt("Implements"), list("Directive"), p$1("{"), list("FieldDef"), p$1("}") ], FieldDef: [ name$1("property"), opt("ArgumentsDef"), p$1(":"), "Type", list("Directive") ], ArgumentsDef: [ p$1("("), list("InputValueDef"), p$1(")") ], InputValueDef: [ name$1("attribute"), p$1(":"), "Type", opt("DefaultValue"), list("Directive") ], UnionDef: [ word("union"), name$1("atom"), list("Directive"), p$1("="), list("UnionMember", p$1("|")) ], UnionMember: [ "NamedType" ], EnumDef: [ word("enum"), name$1("atom"), list("Directive"), p$1("{"), list("EnumValueDef"), p$1("}") ], EnumValueDef: [ name$1("string-2"), list("Directive") ], InputDef: [ word("input"), name$1("atom"), list("Directive"), p$1("{"), list("InputValueDef"), p$1("}") ], ExtendDef: [ word("extend"), "ExtensionDefinition" ], ExtensionDefinition(t) { switch (t.value) { case "schema": return e.SCHEMA_EXTENSION; case "scalar": return e.SCALAR_TYPE_EXTENSION; case "type": return e.OBJECT_TYPE_EXTENSION; case "interface": return e.INTERFACE_TYPE_EXTENSION; case "union": return e.UNION_TYPE_EXTENSION; case "enum": return e.ENUM_TYPE_EXTENSION; case "input": return e.INPUT_OBJECT_TYPE_EXTENSION; } }, [e.SCHEMA_EXTENSION]: [ "SchemaDef" ], [e.SCALAR_TYPE_EXTENSION]: [ "ScalarDef" ], [e.OBJECT_TYPE_EXTENSION]: [ "ObjectTypeDef" ], [e.INTERFACE_TYPE_EXTENSION]: [ "InterfaceDef" ], [e.UNION_TYPE_EXTENSION]: [ "UnionDef" ], [e.ENUM_TYPE_EXTENSION]: [ "EnumDef" ], [e.INPUT_OBJECT_TYPE_EXTENSION]: [ "InputDef" ] }; function word(e) { return { style: "keyword", match: t => "Name" === t.kind && t.value === e }; } function name$1(e) { return { style: e, match: e => "Name" === e.kind, update(e, t) { e.name = t.value; } }; } function onlineParser(t = { eatWhitespace: e => e.eatWhile(isIgnored), lexRules: w, parseRules: D, editorConfig: {} }) { return { startState() { var i = { level: 0, step: 0, name: null, kind: null, type: null, rule: null, needsSeparator: !1, prevState: null }; pushRule(t.parseRules, i, e.DOCUMENT); return i; }, token: (e, i) => function getToken(e, t, i) { var r; if (t.inBlockstring) { if (e.match(/.*"""/)) { t.inBlockstring = !1; return "string"; } e.skipToEnd(); return "string"; } var {lexRules: a, parseRules: n, eatWhitespace: s, editorConfig: o} = i; if (t.rule && 0 === t.rule.length) { popRule(t); } else if (t.needsAdvance) { t.needsAdvance = !1; advanceRule(t, !0); } if (e.sol()) { var l = (null == o ? void 0 : o.tabSize) || 2; t.indentLevel = Math.floor(e.indentation() / l); } if (s(e)) { return "ws"; } var u = function lex(e, t) { var i = Object.keys(e); for (var r = 0; r < i.length; r++) { var a = t.match(e[i[r]]); if (a && a instanceof Array) { return { kind: i[r], value: a[0] }; } } }(a, e); if (!u) { if (!e.match(/\S+/)) { e.match(/\s/); } pushRule(k, t, "Invalid"); return "invalidchar"; } if ("Comment" === u.kind) { pushRule(k, t, "Comment"); return "comment"; } var f = assign({}, t); if ("Punctuation" === u.kind) { if (/^[{([]/.test(u.value)) { if (void 0 !== t.indentLevel) { t.levels = (t.levels || []).concat(t.indentLevel + 1); } } else if (/^[})\]]/.test(u.value)) { var c = t.levels = (t.levels || []).slice(0, -1); if (t.indentLevel && c.length > 0 && c.at(-1) < t.indentLevel) { t.indentLevel = c.at(-1); } } } while (t.rule) { var h = "function" == typeof t.rule ? 0 === t.step ? t.rule(u, e) : null : t.rule[t.step]; if (t.needsSeparator) { h = null == h ? void 0 : h.separator; } if (h) { if (h.ofRule) { h = h.ofRule; } if ("string" == typeof h) { pushRule(n, t, h); continue; } if (null === (r = h.match) || void 0 === r ? void 0 : r.call(h, u)) { if (h.update) { h.update(t, u); } if ("Punctuation" === u.kind) { advanceRule(t, !0); } else { t.needsAdvance = !0; } return h.style; } } unsuccessful(t); } assign(t, f); pushRule(k, t, "Invalid"); return "invalidchar"; }(e, i, t) }; } function assign(e, t) { var i = Object.keys(t); for (var r = 0; r < i.length; r++) { e[i[r]] = t[i[r]]; } return e; } var k = { Invalid: [], Comment: [] }; function pushRule(e, t, i) { if (!e[i]) { throw new TypeError("Unknown rule: " + i); } t.prevState = Object.assign({}, t); t.kind = i; t.name = null; t.type = null; t.rule = e[i]; t.step = 0; t.needsSeparator = !1; } function popRule(e) { if (!e.prevState) { return; } e.kind = e.prevState.kind; e.name = e.prevState.name; e.type = e.prevState.type; e.rule = e.prevState.rule; e.step = e.prevState.step; e.needsSeparator = e.prevState.needsSeparator; e.prevState = e.prevState.prevState; } function advanceRule(e, t) { var i; if (isList(e) && e.rule) { var r = e.rule[e.step]; if (r.separator) { var {separator: a} = r; e.needsSeparator = !e.needsSeparator; if (!e.needsSeparator && a.ofRule) { return; } } if (t) { return; } } e.needsSeparator = !1; e.step++; while (e.rule && !(Array.isArray(e.rule) && e.step < e.rule.length)) { popRule(e); if (e.rule) { if (isList(e)) { if (null === (i = e.rule) || void 0 === i ? void 0 : i[e.step].separator) { e.needsSeparator = !e.needsSeparator; } } else { e.needsSeparator = !1; e.step++; } } } } function isList(e) { var t = Array.isArray(e.rule) && "string" != typeof e.rule[e.step] && e.rule[e.step]; return t && t.isList; } function unsuccessful(e) { while (e.rule && (!Array.isArray(e.rule) || !e.rule[e.step].ofRule)) { popRule(e); } if (e.rule) { advanceRule(e, !1); } } function getDefaultExportFromCjs(e) { return e && e.__esModule && Object.prototype.hasOwnProperty.call(e, "default") ? e.default : e; } class Range { constructor(e, t) { this.containsPosition = e => { if (this.start.line === e.line) { return this.start.character <= e.character; } if (this.end.line === e.line) { return this.end.character >= e.character; } return this.start.line <= e.line && this.end.line >= e.line; }; this.start = e; this.end = t; } setStart(e, t) { this.start = new Position(e, t); } setEnd(e, t) { this.end = new Position(e, t); } } class Position { constructor(e, t) { this.lessThanOrEqualTo = e => this.line < e.line || this.line === e.line && this.character <= e.character; this.line = e; this.character = t; } setLine(e) { this.line = e; } setCharacter(e) { this.character = e; } } var C = [ s, o, l, u, f, c, h, p, d, v, g, m ]; var F = "Error", L = "Warning", _ = "Information", N = "Hint"; var O = { [F]: 1, [L]: 2, [_]: 3, [N]: 4 }; var invariant = (e, t) => { if (!e) { throw new Error(t); } }; function getDiagnostics(s, o = null, l, u, f) { var c, h; var p = null; var d = ""; if (f) { d = "string" == typeof f ? f : f.reduce((e, t) => e + S(t) + "\n\n", ""); } var v = d ? `${s}\n\n${d}` : s; try { p = y(v); } catch (e) { if (e instanceof x) { var g = function getRange(e, t) { var i = onlineParser(); var r = i.startState(); var a = t.split("\n"); invariant(a.length >= e.line, "Query text must have more lines than where the error happened"); var n = null; for (var s = 0; s < e.line; s++) { n = new CharacterStream(a[s]); while (!n.eol()) { if ("invalidchar" === i.token(n, r)) { break; } } } invariant(n, "Expected Parser stream to be available."); var o = e.line - 1; var l = n.getStartOfToken(); var u = n.getCurrentPosition(); return new Range(new Position(o, l), new Position(o, u)); }(null !== (h = null === (c = e.locations) || void 0 === c ? void 0 : c[0]) && void 0 !== h ? h : { line: 0, column: 0 }, v); return [ { severity: O.Error, message: e.message, source: "GraphQL: Syntax", range: g } ]; } throw e; } return function validateQuery(s, o = null, l, u) { if (!o) { return []; } var f = function validateWithCustomRules(s, o, l, u, f) { var c = t.filter(e => { if (e === i || e === r) { return !1; } if (u && e === a) { return !1; } return !0; }); if (l) { Array.prototype.push.apply(c, l); } if (f) { Array.prototype.push.apply(c, C); } return n(s, o, c).filter(t => { if (t.message.includes("Unknown directive") && t.nodes) { var i = t.nodes[0]; if (i && i.kind === e.DIRECTIVE) { var r = i.name.value; if ("arguments" === r || "argumentDefinitions" === r) { return !1; } } } return !0; }); }(o, s, l, u).flatMap(e => annotations(e, O.Error, "Validation")); var c = n(o, s, [ T ]).flatMap(e => annotations(e, O.Warning, "Deprecation")); return f.concat(c); }(p, o, l, u); } function annotations(e, t, i) { if (!e.nodes) { return []; } var r = []; for (var [a, n] of e.nodes.entries()) { var s = "Variable" !== n.kind && "name" in n && void 0 !== n.name ? n.name : "variable" in n && void 0 !== n.variable ? n.variable : n; if (s) { invariant(e.locations, "GraphQL validation error requires locations."); var o = e.locations[a]; var l = getLocation(s); var u = o.column + (l.end - l.start); r.push({ source: `GraphQL: ${i}`, message: e.message, severity: t, range: new Range(new Position(o.line - 1, o.column - 1), new Position(o.line - 1, u)) }); } } return r; } function getLocation(e) { var t = e.loc; invariant(t, "Expected ASTNode to have a location."); return t; } var I = "FragmentDefinition"; class GraphQLError extends Error { constructor(e, t, i, r, a, n, s) { super(e); this.name = "GraphQLError"; this.message = e; if (a) { this.path = a; } if (t) { this.nodes = Array.isArray(t) ? t : [ t ]; } if (i) { this.source = i; } if (r) { this.positions = r; } if (n) { this.originalError = n; } var o = s; if (!o && n) { var l = n.extensions; if (l && "object" == typeof l) { o = l; } } this.extensions = o || {}; } toJSON() { return { ...this, message: this.message }; } toString() { return this.message; } get [Symbol.toStringTag]() { return "GraphQLError"; } } var z; var P; function error(e) { return new GraphQLError(`Syntax Error: Unexpected token at ${P} in ${e}`); } function advance(e) { e.lastIndex = P; if (e.test(z)) { return z.slice(P, P = e.lastIndex); } } var $ = / +(?=[^\s])/y; function blockString(e) { var t = e.split("\n"); var i = ""; var r = 0; var a = 0; var n = t.length - 1; for (var s = 0; s < t.length; s++) { $.lastIndex = 0; if ($.test(t[s])) { if (s && (!r || $.lastIndex < r)) { r = $.lastIndex; } a = a || s; n = s; } } for (var o = a; o <= n; o++) { if (o !== a) { i += "\n"; } i += t[o].slice(r).replace(/\\"""/g, '"""'); } return i; } function ignored() { for (var e = 0 | z.charCodeAt(P++); 9 === e || 10 === e || 13 === e || 32 === e || 35 === e || 44 === e || 65279 === e; e = 0 | z.charCodeAt(P++)) { if (35 === e) { while (10 !== (e = z.charCodeAt(P++)) && 13 !== e) {} } } P--; } var V = /[_A-Za-z]\w*/y; function name() { var e; if (e = advance(V)) { return { kind: "Name", value: e }; } } var R = /(?:null|true|false)/y; var j = /\$[_A-Za-z]\w*/y; var B = /-?\d+/y; var M = /(?:\.\d+)?[eE][+-]?\d+|\.\d+/y; var W = /\\/g; var U = /"""(?:"""|(?:[\s\S]*?[^\\])""")/y; var K = /"(?:"|[^\r\n]*?[^\\]")/y; function value(e) { var t; var i; if (i = advance(R)) { t = "null" === i ? { kind: "NullValue" } : { kind: "BooleanValue", value: "true" === i }; } else if (!e && (i = advance(j))) { t = { kind: "Variable", name: { kind: "Name", value: i.slice(1) } }; } else if (i = advance(B)) { var r = i; if (i = advance(M)) { t = { kind: "FloatValue", value: r + i }; } else { t = { kind: "IntValue", value: r }; } } else if (i = advance(V)) { t = { kind: "EnumValue", value: i }; } else if (i = advance(U)) { t = { kind: "StringValue", value: blockString(i.slice(3, -3)), block: !0 }; } else if (i = advance(K)) { t = { kind: "StringValue", value: W.test(i) ? JSON.parse(i) : i.slice(1, -1), block: !1 }; } else if (t = function list(e) { var t; if (91 === z.charCodeAt(P)) { P++; ignored(); var i = []; while (t = value(e)) { i.push(t); } if (93 !== z.charCodeAt(P++)) { throw error("ListValue"); } ignored(); return { kind: "ListValue", values: i }; } }(e) || function object(e) { if (123 === z.charCodeAt(P)) { P++; ignored(); var t = []; var i; while (i = name()) { ignored(); if (58 !== z.charCodeAt(P++)) { throw error("ObjectField"); } ignored(); var r = value(e); if (!r) { throw error("ObjectField"); } t.push({ kind: "ObjectField", name: i, value: r }); } if (125 !== z.charCodeAt(P++)) { throw error("ObjectValue"); } ignored(); return { kind: "ObjectValue", fields: t }; } }(e)) { return t; } ignored(); return t; } function arguments_(e) { var t = []; ignored(); if (40 === z.charCodeAt(P)) { P++; ignored(); var i; while (i = name()) { ignored(); if (58 !== z.charCodeAt(P++)) { throw error("Argument"); } ignored(); var r = value(e); if (!r) { throw error("Argument"); } t.push({ kind: "Argument", name: i, value: r }); } if (!t.length || 41 !== z.charCodeAt(P++)) { throw error("Argument"); } ignored(); } return t; } function directives(e) { var t = []; ignored(); while (64 === z.charCodeAt(P)) { P++; var i = name(); if (!i) { throw error("Directive"); } ignored(); t.push({ kind: "Directive", name: i, arguments: arguments_(e) }); } return t; } function field() { var e = name(); if (e) { ignored(); var t; if (58 === z.charCodeAt(P)) { P++; ignored(); t = e; if (!(e = name())) { throw error("Field"); } ignored(); } return { kind: "Field", alias: t, name: e, arguments: arguments_(!1), directives: directives(!1), selectionSet: selectionSet() }; } } function type() { var e; ignored(); if (91 === z.charCodeAt(P)) { P++; ignored(); var t = type(); if (!t || 93 !== z.charCodeAt(P++)) { throw error("ListType"); } e = { kind: "ListType", type: t }; } else if (e = name()) { e = { kind: "NamedType", name: e }; } else { throw error("NamedType"); } ignored(); if (33 === z.charCodeAt(P)) { P++; ignored(); return { kind: "NonNullType", type: e }; } else { return e; } } var G = /on/y; function typeCondition() { if (advance(G)) { ignored(); var e = name(); if (!e) { throw error("NamedType"); } ignored(); return { kind: "NamedType", name: e }; } } var Q = /\.\.\./y; function fragmentSpread() { if (advance(Q)) { ignored(); var e = P; var t; if ((t = name()) && "on" !== t.value) { return { kind: "FragmentSpread", name: t, directives: directives(!1) }; } else { P = e; var i = typeCondition(); var r = directives(!1); var a = selectionSet(); if (!a) { throw error("InlineFragment"); } return { kind: "InlineFragment", typeCondition: i, directives: r, selectionSet: a }; } } } function selectionSet() { var e; ignored(); if (123 === z.charCodeAt(P)) { P++; ignored(); var t = []; while (e = fragmentSpread() || field()) { t.push(e); } if (!t.length || 125 !== z.charCodeAt(P++)) { throw error("SelectionSet"); } ignored(); return { kind: "SelectionSet", selections: t }; } } var q = /fragment/y; function fragmentDefinition() { if (advance(q)) { ignored(); var e = name(); if (!e) { throw error("FragmentDefinition"); } ignored(); var t = typeCondition(); if (!t) { throw error("FragmentDefinition"); } var i = directives(!1); var r = selectionSet(); if (!r) { throw error("FragmentDefinition"); } return { kind: "FragmentDefinition", name: e, typeCondition: t, directives: i, selectionSet: r }; } } var X = /(?:query|mutation|subscription)/y; function operationDefinition() { var e; var t; var i = []; var r = []; if (e = advance(X)) { ignored(); t = name(); i = function variableDefinitions() { var e; var t = []; ignored(); if (40 === z.charCodeAt(P)) { P++; ignored(); while (e = advance(j)) { ignored(); if (58 !== z.charCodeAt(P++)) { throw error("VariableDefinition"); } var i = type(); var r = void 0; if (61 === z.charCodeAt(P)) { P++; ignored(); if (!(r = value(!0))) { throw error("VariableDefinition"); } } ignored(); t.push({ kind: "VariableDefinition", variable: { kind: "Variable", name: { kind: "Name", value: e.slice(1) } }, type: i, defaultValue: r, directives: directives(!0) }); } if (41 !== z.charCodeAt(P++)) { throw error("VariableDefinition"); } ignored(); } return t; }(); r = directives(!1); } var a = selectionSet(); if (a) { return { kind: "OperationDefinition", operation: e || "query", name: t, variableDefinitions: i, directives: r, selectionSet: a }; } } function parse(e, t) { z = "string" == typeof e.body ? e.body : e; P = 0; return function document() { var e; ignored(); var t = []; while (e = fragmentDefinition() || operationDefinition()) { t.push(e); } return { kind: "Document", definitions: t }; }(); } var Y = {}; function visit(e, t) { var i = []; var r = []; try { var a = function traverse(e, a, n) { var s = !1; var o = t[e.kind] && t[e.kind].enter || t[e.kind] || t.enter; var l = o && o.call(t, e, a, n, r, i); if (!1 === l) { return e; } else if (null === l) { return null; } else if (l === Y) { throw Y; } else if (l && "string" == typeof l.kind) { s = l !== e; e = l; } if (n) { i.push(n); } var u; var f = { ...e }; for (var c in e) { r.push(c); var h = e[c]; if (Array.isArray(h)) { var p = []; for (var d = 0; d < h.length; d++) { if (null != h[d] && "string" == typeof h[d].kind) { i.push(e); r.push(d); u = traverse(h[d], d, h); r.pop(); i.pop(); if (null == u) { s = !0; } else { s = s || u !== h[d]; p.push(u); } } } h = p; } else if (null != h && "string" == typeof h.kind) { if (void 0 !== (u = traverse(h, c, e))) { s = s || h !== u; h = u; } } r.pop(); if (s) { f[c] = h; } } if (n) { i.pop(); } var v = t[e.kind] && t[e.kind].leave || t.leave; var g = v && v.call(t, e, a, n, r, i); if (g === Y) { throw Y; } else if (void 0 !== g) { return g; } else if (void 0 !== l) { return s ? f : l; } else { return s ? f : e; } }(e); return void 0 !== a && !1 !== a ? a : e; } catch (t) { if (t !== Y) { throw t; } return e; } } var hasItems = e => !(!e || !e.length); var J = { OperationDefinition(e) { if ("query" === e.operation && !e.name && !hasItems(e.variableDefinitions) && !hasItems(e.directives)) { return J.SelectionSet(e.selectionSet); } var t = e.operation; if (e.name) { t += " " + e.name.value; } if (hasItems(e.variableDefinitions)) { if (!e.name) { t += " "; } t += "(" + e.variableDefinitions.map(J.VariableDefinition).join(", ") + ")"; } if (hasItems(e.directives)) { t += " " + e.directives.map(J.Directive).join(" "); } return t + " " + J.SelectionSet(e.selectionSet); }, VariableDefinition(e) { var t = J.Variable(e.variable) + ": " + print(e.type); if (e.defaultValue) { t += " = " + print(e.defaultValue); } if (hasItems(e.directives)) { t += " " + e.directives.map(J.Directive).join(" "); } return t; }, Field(e) { var t = (e.alias ? e.alias.value + ": " : "") + e.name.value; if (hasItems(e.arguments)) { var i = e.arguments.map(J.Argument); var r = t + "(" + i.join(", ") + ")"; t = r.length > 80 ? t + "(\n " + i.join("\n").replace(/\n/g, "\n ") + "\n)" : r; } if (hasItems(e.directives)) { t += " " + e.directives.map(J.Directive).join(" "); } return e.selectionSet ? t + " " + J.SelectionSet(e.selectionSet) : t; }, StringValue: e => e.block ? function printBlockString(e) { return '"""\n' + e.replace(/"""/g, '\\"""') + '\n"""'; }(e.value) : function printString(e) { return JSON.stringify(e); }(e.value), BooleanValue: e => "" + e.value, NullValue: e => "null", IntValue: e => e.value, FloatValue: e => e.value, EnumValue: e => e.value, Name: e => e.value, Variable: e => "$" + e.name.value, ListValue: e => "[" + e.values.map(print).join(", ") + "]", ObjectValue: e => "{" + e.fields.map(J.ObjectField).join(", ") + "}", ObjectField: e => e.name.value + ": " + print(e.value), Document: e => hasItems(e.definitions) ? e.definitions.map(print).join("\n\n") : "", SelectionSet: e => "{\n " + e.selections.map(print).join("\n").replace(/\n/g, "\n ") + "\n}", Argument: e => e.name.value + ": " + print(e.value), FragmentSpread(e) { var t = "..." + e.name.value; if (hasItems(e.directives)) { t += " " + e.directives.map(J.Directive).join(" "); } return t; }, InlineFragment(e) { var t = "..."; if (e.typeCondition) { t += " on " + e.typeCondition.name.value; } if (hasItems(e.directives)) { t += " " + e.directives.map(J.Directive).join(" "); } return t + " " + print(e.selectionSet); }, FragmentDefinition(e) { var t = "fragment " + e.name.value; t += " on " + e.typeCondition.name.value; if (hasItems(e.directives)) { t += " " + e.directives.map(J.Directive).join(" "); } return t + " " + print(e.selectionSet); }, Directive(e) { var t = "@" + e.name.value; if (hasItems(e.arguments)) { t += "(" + e.arguments.map(J.Argument).join(", ") + ")"; } return t; }, NamedType: e => e.name.value, ListType: e => "[" + print(e.type) + "]", NonNullType: e => print(e.type) + "!" }; function print(e) { return J[e.kind] ? J[e.kind](e) : ""; } var H = new Set([ "gql", "graphql" ]); var isIIFE = e => b.isCallExpression(e) && 0 === e.arguments.length && (b.isFunctionExpression(e.expression) || b.isArrowFunction(e.expression)) && !e.expression.asteriskToken && !e.expression.modifiers?.length; var isGraphQLFunctionIdentifier = e => b.isIdentifier(e) && H.has(e.escapedText); var isTadaGraphQLFunction = (e, t) => { if (!b.isLeftHandSideExpression(e)) { return !1; } var i = t?.getTypeAtLocation(e); return null != i && null != i.getProperty("scalar") && null != i.getProperty("persisted"); }; var isTadaGraphQLCall = (e, t) => { if (!b.isCallExpression(e)) { return !1; } else if (e.arguments.length < 1 || e.arguments.length > 2) { return !1; } else if (!b.isStringLiteralLike(e.arguments[0])) { return !1; } return t ? isTadaGraphQLFunction(e.expression, t) : !1; }; var isTadaPersistedCall = (e, t) => { if (!e) { return !1; } else if (!b.isCallExpression(e)) { return !1; } else if (!b.isPropertyAccessExpression(e.expression)) { return !1; } else if (!b.isIdentifier(e.expression.name) || "persisted" !== e.expression.name.escapedText) { return !1; } else if (isGraphQLFunctionIdentifier(e.expression.expression)) { return !0; } else { return isTadaGraphQLFunction(e.expression.expression, t); } }; var isGraphQLCall = (e, t) => b.isCallExpression(e) && e.arguments.length >= 1 && e.arguments.length <= 2 && (isGraphQLFunctionIdentifier(e.expression) || isTadaGraphQLCall(e, t)); var isGraphQLTag = e => b.isTaggedTemplateExpression(e) && isGraphQLFunctionIdentifier(e.tag); var getSchemaName = (e, t, i = !1) => { if (!t) { return null; } var r = t.getTypeAtLocation(i ? e.getChildAt(0).getChildAt(0) : e.expression); if (r) { var a = r.getProperty("__name"); if (a) { var n = t.getTypeOfSymbol(a); if (n.isUnionOrIntersection()) { var s = n.types.find(e => e.isStringLiteral()); return s && s.isStringLiteral() ? s.value : null; } else if (n.isStringLiteral()) { return n.value; } } } return null; }; function isValueDeclaration(e) { switch (e.kind) { case b.SyntaxKind.BinaryExpression: case b.SyntaxKind.ArrowFunction: case b.SyntaxKind.BindingElement: case b.SyntaxKind.ClassDeclaration: case b.SyntaxKind.ClassExpression: case b.SyntaxKind.ClassStaticBlockDeclaration: case b.SyntaxKind.Constructor: case b.SyntaxKind.EnumDeclaration: case b.SyntaxKind.EnumMember: case b.SyntaxKind.ExportAssignment: case b.SyntaxKind.FunctionDeclaration: case b.SyntaxKind.FunctionExpression: case b.SyntaxKind.GetAccessor: case b.SyntaxKind.JsxAttribute: case b.SyntaxKind.MethodDeclaration: case b.SyntaxKind.Parameter: case b.SyntaxKind.PropertyAssignment: case b.SyntaxKind.PropertyDeclaration: case b.SyntaxKind.SetAccessor: case b.SyntaxKind.ShorthandPropertyAssignment: case b.SyntaxKind.VariableDeclaration: return !0; default: return !1; } } function getValueOfValueDeclaration(e) { switch (e.kind) { case b.SyntaxKind.ClassExpression: case b.SyntaxKind.ClassDeclaration: case b.SyntaxKind.ArrowFunction: case b.SyntaxKind.ClassStaticBlockDeclaration: case b.SyntaxKind.Constructor: case b.SyntaxKind.EnumDeclaration: case b.SyntaxKind.FunctionDeclaration: case b.SyntaxKind.FunctionExpression: case b.SyntaxKind.GetAccessor: case b.SyntaxKind.SetAccessor: case b.SyntaxKind.MethodDeclaration: return e; case b.SyntaxKind.BindingElement: case b.SyntaxKind.EnumMember: case b.SyntaxKind.JsxAttribute: case b.SyntaxKind.Parameter: case b.SyntaxKind.PropertyAssignment: case b.SyntaxKind.PropertyDeclaration: case b.SyntaxKind.VariableDeclaration: return e.initializer; case b.SyntaxKind.ExportAssignment: return e.expression; case b.SyntaxKind.BinaryExpression: return function isAssignmentOperator(e) { switch (e.kind) { case b.SyntaxKind.EqualsToken: case b.SyntaxKind.BarBarEqualsToken: case b.SyntaxKind.AmpersandAmpersandEqualsToken: case b.SyntaxKind.QuestionQuestionEqualsToken: return !0; default: return !1; } }(e.operatorToken) ? e.right : void 0; case b.SyntaxKind.ShorthandPropertyAssignment: return e.objectAssignmentInitializer; default: return; } } function climbPastPropertyOrElementAccess(e) { if (e.parent && b.isPropertyAccessExpression(e.parent) && e.parent.name === e) { return e.parent; } else if (e.parent && b.isElementAccessExpression(e.parent) && e.parent.argumentExpression === e) { return e.parent; } else { return e; } } function getNameFromPropertyName(e) { if (b.isComputedPropertyName(e)) { return b.isStringLiteralLike(e.expression) || b.isNumericLiteral(e.expression) ? e.expression.text : void 0; } else if (b.isPrivateIdentifier(e) || b.isMemberName(e)) { return b.idText(e); } else { return e.text; } } function getDeclarationOfIdentifier(e, t) { var i = t.getSymbolAtLocation(e); if (i?.declarations?.[0] && i.flags & b.SymbolFlags.Alias && (e.parent === i?.declarations?.[0] || !b.isNamespaceImport(i.declarations[0]))) { var r = t.getAliasedSymbol(i); if (r.declarations) { i = r; } } if (i && b.isShorthandPropertyAssignment(e.parent)) { var a = t.getShorthandAssignmentValueSymbol(i.valueDeclaration); if (a) { i = a; } } else if (b.isBindingElement(e.parent) && b.isObjectBindingPattern(e.parent.parent) && e === (e.parent.propertyName || e.parent.name)) { var n = getNameFromPropertyName(e); var s = n ? t.getTypeAtLocation(e.parent.parent).getProperty(n) : void 0; if (s) { i = s; } } else if (b.isObjectLiteralElement(e.parent) && (b.isObjectLiteralExpression(e.parent.parent) || b.isJsxAttributes(e.parent.parent)) && e.parent.name === e) { var o = getNameFromPropertyName(e); var l = o ? t.getContextualType(e.parent.parent)?.getProperty(o) : void 0; if (l) { i = l; } } if (i && i.declarations?.length) { if (i.flags & b.SymbolFlags.Class && !(i.flags & (b.SymbolFlags.Function | b.SymbolFlags.Variable)) && function isNewExpressionTarget(e) { var t = climbPastPropertyOrElementAccess(e).parent; return b.isNewExpression(t) && t.expression === e; }(e)) { for (var u of i.declarations) { if (b.isClassLike(u)) { return u; } } } else if (function isCallOrNewExpressionTarget(e) { var t = climbPastPropertyOrElementAccess(e).parent; return b.isCallOrNewExpression(t) && t.expression === e; }(e) || function isNameOfFunctionDeclaration(e) { return b.isIdentifier(e) && e.parent && b.isFunctionLike(e.parent) && e.parent.name === e; }(e)) { for (var f of i.declarations) { if (b.isFunctionLike(f) && f.body && isValueDeclaration(f)) { return f; } } } if (i.valueDeclaration && b.isPropertyAccessExpression(i.valueDeclaration)) { var c = i.valueDeclaration.parent; if (c && b.isBinaryExpression(c) && c.left === i.valueDeclaration) { return c; } } if (i.valueDeclaration && isValueDeclaration(i.valueDeclaration)) { return i.valueDeclaration; } for (var h of i.declarations) { if (isValueDeclaration(h)) { return h; } } } return; } function getValueOfIdentifier(e, t) { while (b.isIdentifier(e)) { var i = getDeclarationOfIdentifier(e, t); if (!i) { return; } else { var r = getValueOfValueDeclaration(i); if (r && b.isIdentifier(r) && r !== e) { e = r; } else { return r; } } } } function resolveTemplate(e, t, i) { if (b.isStringLiteralLike(e)) { return { combinedText: e.getText().slice(1, -1), resolvedSpans: [] }; } var r = e.template.getText().slice(1, -1); if (b.isNoSubstitutionTemplateLiteral(e.template) || 0 === e.template.templateSpans.length) { return { combinedText: r, resolvedSpans: [] }; } var a = 0; var n = e.template.templateSpans.map(e => { if (b.isIdentifier(e.expression)) { var t = i.languageService.getProgram()?.getTypeChecker(); if (!t) { return; } var n = getDeclarationOfIdentifier(e.expression, t); if (!n) { return; } var s = n; if (b.isVariableDeclaration(s)) { var o = e.expression.escapedText; var l = getValueOfValueDeclaration(s); if (!l) { return; } var u = e.expression.getStart() - 2; var f = { start: u, length: e.expression.end - u + 1 }; if (b.isTaggedTemplateExpression(l)) { var c = resolveTemplate(l, s.getSourceFile().fileName, i); r = r.replace("${" + e.expression.escapedText + "}", c.combinedText); var h = { lines: c.combinedText.split("\n").length, identifier: o, original: f, new: { start: f.start + a, length: c.combinedText.length } }; a += c.combinedText.length - f.length; return h; } else if (b.isAsExpression(l) && b.isTaggedTemplateExpression(l.expression)) { var p = resolveTemplate(l.expression, s.getSourceFile().fileName, i); r = r.replace("${" + e.expression.escapedText + "}", p.combinedText); var d = { lines: p.combinedText.split("\n").length, identifier: o, original: f, new: { start: f.start + a, length: p.combinedText.length } }; a += p.combinedText.length - f.length; return d; } else if (b.isAsExpression(l) && b.isAsExpression(l.expression) && b.isObjectLiteralExpression(l.expression.expression)) { var v = print(JSON.parse(l.expression.expression.getText())); r = r.replace("${" + e.expression.escapedText + "}", v); var g = { lines: v.split("\n").length, identifier: o, original: f, new: { start: f.start + a, length: v.length } }; a += v.length - f.length; return g; } return; } } return; }).filter(Boolean); return { combinedText: r, resolvedSpans: n }; } var resolveTadaFragmentArray = e => { if (!e) { return; } while (b.isAsExpression(e)) { e = e.expression; } if (!b.isArrayLiteralExpression(e)) { return; } if (e.elements.every(b.isIdentifier)) { return e.elements; } var t = []; for (var i of e.elements) { while (b.isPropertyAccessExpression(i)) { i = i.name; } if (b.isIdentifier(i)) { t.push(i); } } return t; }; function getSource(e, t) { var i = e.languageService.getProgram(); if (!i) { return; } var r = i.getSourceFile(t); if (!r) { return; } return r; } function findNode(e, t) { return function find(e) { if (t >= e.getStart() && t < e.getEnd()) { return b.forEachChild(e, find) || e; } }(e); } function unrollFragment(e, t, i) { var r = []; var a = [ e ]; var n = new WeakSet; var _unrollElement = e => { if (n.has(e)) { return; } n.add(e); var t = function resolveIdentifierToGraphQLCall(e, t, i) { if (!i) { return null; } var r = getValueOfIdentifier(e, i); if (!r) { return null; } return isGraphQLCall(r, i) ? r : null; }(e, 0, i); if (!t) { return; } var s = resolveTadaFragmentArray(t.arguments[1]); if (s) { a.push(...s); } try { y(t.arguments[0].getText().slice(1, -1), { noLocation: !0 }).definitions.forEach(e => { if ("FragmentDefinition" === e.kind) { r.push(e); } }); } catch (e) {} }; var s; while (void 0 !== (s = a.shift())) { _unrollElement(s); } return r; } function unrollTadaFragments(e, t, i) { var r = i.languageService.getProgram()?.getTypeChecker(); e.elements.forEach(e => { if (b.isIdentifier(e)) { t.push(...unrollFragment(e, 0, r)); } else if (b.isPropertyAccessExpression(e)) { var i = e; while (b.isPropertyAccessExpression(i.expression)) { i = i.expression; } if (b.isIdentifier(i.name)) { t.push(...unrollFragment(i.name, 0, r)); } } }); return t; } function findAllCallExpressions(e, t, i = !0) { var r = t.languageService.getProgram()?.getTypeChecker(); var a = []; var n = []; var s = i ? !1 : !0; !function find(e) { if (!b.isCallExpression(e) || isIIFE(e)) { return b.forEachChild(e, find); } if (!isGraphQLCall(e, r)) { return b.forEachChild(e, find); } var i = getSchemaName(e, r); var o = e.arguments[0]; var l = resolveTadaFragmentArray(e.arguments[1]); var u = isTadaGraphQLCall(e, r); if (!s && !l) { if (!u) { s = !0; n.push(...getAllFragments(e, t)); } } else if (l) { for (var f of l) { n.push(...unrollFragment(f, 0, r)); } } if (o && b.isStringLiteralLike(o)) { a.push({ node: o, schema: i, tadaFragmentRefs: u ? void 0 === l ? [] : l : void 0 }); } }(e); return { nodes: a, fragments: n }; } function findAllPersistedCallExpressions(e, t) { var i = []; var r = t?.languageService.getProgram()?.getTypeChecker(); !function find(e) { if (!b.isCallExpression(e) || isIIFE(e)) { return b.forEachChild(e, find); } if (!isTadaPersistedCall(e, r)) { return; } else if (t) { var a = getSchemaName(e, r, !0); i.push({ node: e, schema: a }); } else { i.push(e); } }(e); return i; } function getAllFragments(e, t) { var i = []; var r = t.languageService.getProgram()?.getTypeChecker(); if (!b.isCallExpression(e)) { return i; } var a = resolveTadaFragmentArray(e.arguments[1]); if (a) { var n = t.languageService.getProgram()?.getTypeChecker(); for (var s of a) { i.push(...unrollFragment(s, 0, n)); } return i; } else if (isTadaGraphQLCall(e, r)) { return i; } if (!r) { return i; } var o = function getIdentifierOfChainExpression(e) { var t = e; while (t) { if (b.isPropertyAccessExpression(t)) { t = t.name; } else if (b.isAsExpression(t) || b.isSatisfiesExpression(t) || b.isNonNullExpression(t) || b.isParenthesizedExpression(t) || b.isExpressionWithTypeArguments(t)) { t = t.expression; } else if (b.isCommaListExpression(t)) { t = t.elements[t.elements.length - 1]; } else if (b.isIdentifier(t)) { return t; } else { return; } } }(e.expression); if (!o) { return i; } var l = getDeclarationOfIdentifier(o, r); if (!l) { return i; } var u = l.getSourceFile(); if (!u) { return i; } var f = [ { fileName: u.fileName, textSpan: { start: l.getStart(), length: l.getWidth() } } ]; if (!f || !f.length) { return i; } var c = f[0]; if (!c) { return i; } var h =