@mo36924/graphql-plugin 1.5.1 → 1.6.1
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.cjs +7 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -7
- package/src/index.ts +0 -357
package/dist/index.cjs
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
const fs = require('fs');
|
4
|
+
const path = require('path');
|
5
|
+
const graphqlSchema = require('@mo36924/graphql-schema');
|
6
|
+
const graphql = require('graphql');
|
7
|
+
const graphqlLanguageServiceInterface = require('graphql-language-service-interface');
|
8
|
+
const graphqlLanguageServiceUtils = require('graphql-language-service-utils');
|
9
|
+
const vscodeLanguageserverTypes = require('vscode-languageserver-types');
|
10
10
|
|
11
11
|
const init = ({ typescript: ts }) => {
|
12
12
|
return {
|
package/dist/index.cjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import { readFileSync, unwatchFile, watch, watchFile } from \"fs\";\nimport { resolve } from \"path\";\nimport { printSchemaModel } from \"@mo36924/graphql-schema\";\nimport { buildSchema, GraphQLError, parse, validate } from \"graphql\";\nimport {\n getAutocompleteSuggestions,\n getDiagnostics,\n getHoverInformation,\n getTokenAtPosition,\n} from \"graphql-language-service-interface\";\nimport { Position } from \"graphql-language-service-utils\";\nimport type { LanguageService, server, SourceFile, TaggedTemplateExpression } from \"typescript/lib/tsserverlibrary\";\nimport { CompletionItemKind, DiagnosticSeverity } from \"vscode-languageserver-types\";\n\nconst init: server.PluginModuleFactory = ({ typescript: ts }) => {\n return {\n create(info) {\n const languageService = info.languageService;\n const config = info.config;\n const cwd = info.project.getCurrentDirectory();\n const modelPath = config.model && resolve(cwd, config.model);\n const schemaPath = config.schema && resolve(cwd, config.schema);\n const watchPath = modelPath || schemaPath;\n let schema = buildSchema(\"scalar Unknown\");\n\n const addScalarUnknownType = (schemaCode: string) =>\n schemaCode.includes(\"scalar Unknown\") ? schemaCode : `${schemaCode}\\nscalar Unknown`;\n\n const changeModel = () => {\n schema = buildSchema(addScalarUnknownType(printSchemaModel(readFileSync(modelPath, \"utf8\"))));\n };\n\n const changeSchema = () => {\n schema = buildSchema(addScalarUnknownType(readFileSync(schemaPath, \"utf8\")));\n };\n\n const update = modelPath ? changeModel : changeSchema;\n\n const listener = () => {\n try {\n update();\n } catch {}\n };\n\n try {\n update();\n watch(watchPath, listener);\n } catch {\n watchFile(watchPath, () => {\n try {\n update();\n watch(watchPath, listener);\n unwatchFile(watchPath);\n } catch {}\n });\n }\n\n const getSourceFile = (fileName: string) => languageService.getProgram()?.getSourceFile(fileName);\n\n const isGraphqlTag = (tag: string): tag is \"query\" | \"mutation\" | \"subscription\" => {\n switch (tag) {\n case \"query\":\n case \"mutation\":\n case \"subscription\":\n return true;\n default:\n return false;\n }\n };\n\n const getDiagnosticCategory = (severity?: number) => {\n switch (severity) {\n case DiagnosticSeverity.Error:\n return ts.DiagnosticCategory.Error;\n case DiagnosticSeverity.Warning:\n return ts.DiagnosticCategory.Warning;\n case DiagnosticSeverity.Information:\n return ts.DiagnosticCategory.Message;\n case DiagnosticSeverity.Hint:\n return ts.DiagnosticCategory.Suggestion;\n default:\n return ts.DiagnosticCategory.Error;\n }\n };\n\n const hover = (sourceFile: SourceFile, position: number) => {\n const tag = ts.forEachChild(sourceFile, function visitor(node): true | undefined | TaggedTemplateExpression {\n if (position < node.pos) {\n return true;\n }\n\n if (position >= node.end) {\n return;\n }\n\n if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && isGraphqlTag(node.tag.getText())) {\n const template = node.template;\n\n if (ts.isNoSubstitutionTemplateLiteral(template)) {\n if (position >= template.getStart() + 1 && position < template.getEnd() - 1) {\n return node;\n }\n } else {\n const head = template.head;\n\n if (position >= head.getStart() + 1 && position < head.getEnd() - 2) {\n return node;\n }\n\n for (const { literal } of template.templateSpans) {\n if (\n position >= literal.getStart() + 1 &&\n position < literal.getEnd() - (ts.isTemplateMiddle(literal) ? 2 : 1)\n ) {\n return node;\n }\n }\n }\n }\n\n return ts.forEachChild(node, visitor);\n });\n\n if (tag === true) {\n return;\n }\n\n return tag;\n };\n\n const fix = (node: TaggedTemplateExpression) => {\n const template = node.template;\n let query = \"\";\n let variables = \"\";\n\n if (ts.isNoSubstitutionTemplateLiteral(template)) {\n // 2 ``\n const templateWidth = template.getWidth() - 2;\n query = template.text.padStart(templateWidth);\n } else {\n const head = template.head;\n const templateSpans = template.templateSpans;\n\n // 3 `...${\n const templateWidth = head.getWidth() - 3;\n query = head.text.padStart(templateWidth);\n\n templateSpans.forEach((span, i) => {\n const spanWidth = span.getFullWidth();\n const literal = span.literal;\n const literalWidth = literal.getWidth();\n const expressionWidth = spanWidth - literalWidth;\n const variableName = `$_${i}`;\n const variable = variableName.padStart(expressionWidth + 2).padEnd(expressionWidth + 3);\n const templateWidth = literalWidth - (ts.isTemplateTail(literal) ? 2 : 3);\n const template = literal.text.padStart(templateWidth);\n query += variable + template;\n variables += variableName + \":Unknown\";\n });\n }\n\n const tag = node.tag.getText();\n let offset = template.getStart() + 1;\n query = query.replace(/\\n|\\r/g, \" \");\n\n if (variables) {\n query = `${tag}(${variables}){${query}}`;\n offset -= tag.length + variables.length + 3;\n } else if (tag === \"query\") {\n query = `{${query}}`;\n offset -= 1;\n } else {\n query = `${tag}{${query}}`;\n offset -= tag.length + 1;\n }\n\n const documentNode = parse(query);\n const errors = validate(schema, documentNode);\n\n for (const error of errors) {\n const match = error.message.match(\n /^Variable \".*?\" of type \"Unknown\" used in position expecting type \"(.*?)\"\\.$/,\n );\n\n if (match) {\n query = query.replace(\"Unknown\", match[1]);\n offset += 7 - match[1].length;\n }\n }\n\n return {\n query,\n offset,\n };\n };\n\n const proxy: LanguageService = Object.create(null);\n\n for (const [key, value] of Object.entries(languageService)) {\n (proxy as any)[key] = value.bind(languageService);\n }\n\n proxy.getQuickInfoAtPosition = (fileName, position) => {\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return undefined;\n }\n\n const tag = hover(sourceFile, position);\n\n if (!tag) {\n return languageService.getQuickInfoAtPosition(fileName, position);\n }\n\n let result: { query: string; offset: number };\n\n try {\n result = fix(tag);\n } catch {\n return languageService.getQuickInfoAtPosition(fileName, position);\n }\n\n const { query, offset } = result;\n const cursor = new Position(0, position - offset + 1);\n const token = getTokenAtPosition(query, cursor);\n const marked = getHoverInformation(schema, query, cursor, token);\n\n if (marked === \"\" || typeof marked !== \"string\") {\n return;\n }\n\n return {\n kind: ts.ScriptElementKind.string,\n textSpan: {\n start: offset + token.start,\n length: token.end - token.start,\n },\n kindModifiers: \"\",\n displayParts: [{ text: marked, kind: \"\" }],\n };\n };\n\n proxy.getCompletionsAtPosition = (fileName, position, options) => {\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return undefined;\n }\n\n const tag = hover(sourceFile, position);\n\n if (!tag) {\n return languageService.getCompletionsAtPosition(fileName, position, options);\n }\n\n let result: { query: string; offset: number };\n\n try {\n result = fix(tag);\n } catch {\n return languageService.getCompletionsAtPosition(fileName, position, options);\n }\n\n const { query, offset } = result;\n const cursor = new Position(0, position - offset);\n const items = getAutocompleteSuggestions(schema, query, cursor);\n\n if (!items.length) {\n return;\n }\n\n return {\n isGlobalCompletion: false,\n isMemberCompletion: false,\n isNewIdentifierLocation: false,\n entries: items.map((item) => {\n let kind: ts.ScriptElementKind;\n\n switch (item.kind) {\n case CompletionItemKind.Function:\n case CompletionItemKind.Constructor:\n kind = ts.ScriptElementKind.functionElement;\n break;\n case CompletionItemKind.Field:\n case CompletionItemKind.Variable:\n kind = ts.ScriptElementKind.memberVariableElement;\n break;\n default:\n kind = ts.ScriptElementKind.unknown;\n break;\n }\n\n return {\n name: item.label,\n kindModifiers: \"\",\n kind,\n sortText: \"\",\n };\n }),\n };\n };\n\n proxy.getSemanticDiagnostics = (fileName) => {\n const diagnostics = languageService.getSemanticDiagnostics(fileName);\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return diagnostics;\n }\n\n ts.forEachChild(sourceFile, function visitor(node) {\n if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && isGraphqlTag(node.tag.getText())) {\n try {\n const { query, offset } = fix(node);\n const _diagnostics = getDiagnostics(query, schema);\n\n for (const {\n range: { start, end },\n severity,\n message,\n } of _diagnostics) {\n diagnostics.push({\n category: getDiagnosticCategory(severity),\n code: 9999,\n messageText: message,\n file: sourceFile,\n start: start.character + offset,\n length: end.character - start.character,\n });\n }\n } catch (error) {\n if (error instanceof GraphQLError) {\n diagnostics.push({\n category: ts.DiagnosticCategory.Error,\n code: 9999,\n messageText: error.message,\n file: sourceFile,\n start: node.template.getStart() + 1,\n length: node.template.getWidth() - 2,\n });\n }\n }\n }\n\n ts.forEachChild(node, visitor);\n });\n\n return diagnostics;\n };\n\n return proxy;\n },\n };\n};\n\nexport default init;\n"],"names":["resolve","buildSchema","printSchemaModel","readFileSync","watch","watchFile","unwatchFile","DiagnosticSeverity","parse","validate","Position","getTokenAtPosition","getHoverInformation","getAutocompleteSuggestions","CompletionItemKind","getDiagnostics","GraphQLError"],"mappings":";;;;;;;;;;MAcM,IAAI,GAA+B,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE;IAC1D,OAAO;QACL,MAAM,CAAC,IAAI;YACT,MAAM,eAAe,GAAG,IAAI,CAAC,eAAe,CAAC;YAC7C,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;YAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,mBAAmB,EAAE,CAAC;YAC/C,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,IAAIA,YAAO,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;YAC7D,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,IAAI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AAI,EAAE,IAAI;oCACV,WAAW,EAAE,OAAO;oCACpB,IAAI,EAAE,UAAU;oCAChB,KAAK,EAAE,KAAK,CAAC,SAAS,GAAG,MAAM;oCAC/B,MAAM,EAAE,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC,SAAS;iCACxC,CAAC,CAAC;6BACJ;yBACF;wBAAC,OAAO,KAAK,EAAE;4BACd,IAAI,KAAK,YAAYC,oBAAY,EAAE;gCACjC,WAAW,CAAC,IAAI,CAAC;oCACf,QAAQ,EAAE,EAAE,CAAC,kBAAkB,CAAC,KAAK;oCACrC,IAAI,EAAE,IAAI;oCACV,WAAW,EAAE,KAAK,CAAC,OAAO;oCAC1B,IAAI,EAAE,UAAU;oCAChB,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC;oCACnC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC;iCACrC,CAAC,CAAC;6BACJ;yBACF;qBACF;oBAED,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;iBAChC,CAAC,CAAC;gBAEH,OAAO,WAAW,CAAC;aACpB,CAAC;YAEF,OAAO,KAAK,CAAC;SACd;KACF,CAAC;AACJ;;;;"}
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import { readFileSync, unwatchFile, watch, watchFile } from \"fs\";\nimport { resolve } from \"path\";\nimport { printSchemaModel } from \"@mo36924/graphql-schema\";\nimport { GraphQLError, buildSchema, parse, validate } from \"graphql\";\nimport {\n getAutocompleteSuggestions,\n getDiagnostics,\n getHoverInformation,\n getTokenAtPosition,\n} from \"graphql-language-service-interface\";\nimport { Position } from \"graphql-language-service-utils\";\nimport type { LanguageService, SourceFile, TaggedTemplateExpression, server } from \"typescript/lib/tsserverlibrary\";\nimport { CompletionItemKind, DiagnosticSeverity } from \"vscode-languageserver-types\";\n\nconst init: server.PluginModuleFactory = ({ typescript: ts }) => {\n return {\n create(info) {\n const languageService = info.languageService;\n const config = info.config;\n const cwd = info.project.getCurrentDirectory();\n const modelPath = config.model && resolve(cwd, config.model);\n const schemaPath = config.schema && resolve(cwd, config.schema);\n const watchPath = modelPath || schemaPath;\n let schema = buildSchema(\"scalar Unknown\");\n\n const addScalarUnknownType = (schemaCode: string) =>\n schemaCode.includes(\"scalar Unknown\") ? schemaCode : `${schemaCode}\\nscalar Unknown`;\n\n const changeModel = () => {\n schema = buildSchema(addScalarUnknownType(printSchemaModel(readFileSync(modelPath, \"utf8\"))));\n };\n\n const changeSchema = () => {\n schema = buildSchema(addScalarUnknownType(readFileSync(schemaPath, \"utf8\")));\n };\n\n const update = modelPath ? changeModel : changeSchema;\n\n const listener = () => {\n try {\n update();\n } catch {}\n };\n\n try {\n update();\n watch(watchPath, listener);\n } catch {\n watchFile(watchPath, () => {\n try {\n update();\n watch(watchPath, listener);\n unwatchFile(watchPath);\n } catch {}\n });\n }\n\n const getSourceFile = (fileName: string) => languageService.getProgram()?.getSourceFile(fileName);\n\n const isGraphqlTag = (tag: string): tag is \"query\" | \"mutation\" | \"subscription\" => {\n switch (tag) {\n case \"query\":\n case \"mutation\":\n case \"subscription\":\n return true;\n default:\n return false;\n }\n };\n\n const getDiagnosticCategory = (severity?: number) => {\n switch (severity) {\n case DiagnosticSeverity.Error:\n return ts.DiagnosticCategory.Error;\n case DiagnosticSeverity.Warning:\n return ts.DiagnosticCategory.Warning;\n case DiagnosticSeverity.Information:\n return ts.DiagnosticCategory.Message;\n case DiagnosticSeverity.Hint:\n return ts.DiagnosticCategory.Suggestion;\n default:\n return ts.DiagnosticCategory.Error;\n }\n };\n\n const hover = (sourceFile: SourceFile, position: number) => {\n const tag = ts.forEachChild(sourceFile, function visitor(node): true | undefined | TaggedTemplateExpression {\n if (position < node.pos) {\n return true;\n }\n\n if (position >= node.end) {\n return;\n }\n\n if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && isGraphqlTag(node.tag.getText())) {\n const template = node.template;\n\n if (ts.isNoSubstitutionTemplateLiteral(template)) {\n if (position >= template.getStart() + 1 && position < template.getEnd() - 1) {\n return node;\n }\n } else {\n const head = template.head;\n\n if (position >= head.getStart() + 1 && position < head.getEnd() - 2) {\n return node;\n }\n\n for (const { literal } of template.templateSpans) {\n if (\n position >= literal.getStart() + 1 &&\n position < literal.getEnd() - (ts.isTemplateMiddle(literal) ? 2 : 1)\n ) {\n return node;\n }\n }\n }\n }\n\n return ts.forEachChild(node, visitor);\n });\n\n if (tag === true) {\n return;\n }\n\n return tag;\n };\n\n const fix = (node: TaggedTemplateExpression) => {\n const template = node.template;\n let query = \"\";\n let variables = \"\";\n\n if (ts.isNoSubstitutionTemplateLiteral(template)) {\n // 2 ``\n const templateWidth = template.getWidth() - 2;\n query = template.text.padStart(templateWidth);\n } else {\n const head = template.head;\n const templateSpans = template.templateSpans;\n\n // 3 `...${\n const templateWidth = head.getWidth() - 3;\n query = head.text.padStart(templateWidth);\n\n templateSpans.forEach((span, i) => {\n const spanWidth = span.getFullWidth();\n const literal = span.literal;\n const literalWidth = literal.getWidth();\n const expressionWidth = spanWidth - literalWidth;\n const variableName = `$_${i}`;\n const variable = variableName.padStart(expressionWidth + 2).padEnd(expressionWidth + 3);\n const templateWidth = literalWidth - (ts.isTemplateTail(literal) ? 2 : 3);\n const template = literal.text.padStart(templateWidth);\n query += variable + template;\n variables += variableName + \":Unknown\";\n });\n }\n\n const tag = node.tag.getText();\n let offset = template.getStart() + 1;\n query = query.replace(/\\n|\\r/g, \" \");\n\n if (variables) {\n query = `${tag}(${variables}){${query}}`;\n offset -= tag.length + variables.length + 3;\n } else if (tag === \"query\") {\n query = `{${query}}`;\n offset -= 1;\n } else {\n query = `${tag}{${query}}`;\n offset -= tag.length + 1;\n }\n\n const documentNode = parse(query);\n const errors = validate(schema, documentNode);\n\n for (const error of errors) {\n const match = error.message.match(\n /^Variable \".*?\" of type \"Unknown\" used in position expecting type \"(.*?)\"\\.$/,\n );\n\n if (match) {\n query = query.replace(\"Unknown\", match[1]);\n offset += 7 - match[1].length;\n }\n }\n\n return {\n query,\n offset,\n };\n };\n\n const proxy: LanguageService = Object.create(null);\n\n for (const [key, value] of Object.entries(languageService)) {\n (proxy as any)[key] = value.bind(languageService);\n }\n\n proxy.getQuickInfoAtPosition = (fileName, position) => {\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return undefined;\n }\n\n const tag = hover(sourceFile, position);\n\n if (!tag) {\n return languageService.getQuickInfoAtPosition(fileName, position);\n }\n\n let result: { query: string; offset: number };\n\n try {\n result = fix(tag);\n } catch {\n return languageService.getQuickInfoAtPosition(fileName, position);\n }\n\n const { query, offset } = result;\n const cursor = new Position(0, position - offset + 1);\n const token = getTokenAtPosition(query, cursor);\n const marked = getHoverInformation(schema, query, cursor, token);\n\n if (marked === \"\" || typeof marked !== \"string\") {\n return;\n }\n\n return {\n kind: ts.ScriptElementKind.string,\n textSpan: {\n start: offset + token.start,\n length: token.end - token.start,\n },\n kindModifiers: \"\",\n displayParts: [{ text: marked, kind: \"\" }],\n };\n };\n\n proxy.getCompletionsAtPosition = (fileName, position, options) => {\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return undefined;\n }\n\n const tag = hover(sourceFile, position);\n\n if (!tag) {\n return languageService.getCompletionsAtPosition(fileName, position, options);\n }\n\n let result: { query: string; offset: number };\n\n try {\n result = fix(tag);\n } catch {\n return languageService.getCompletionsAtPosition(fileName, position, options);\n }\n\n const { query, offset } = result;\n const cursor = new Position(0, position - offset);\n const items = getAutocompleteSuggestions(schema, query, cursor);\n\n if (!items.length) {\n return;\n }\n\n return {\n isGlobalCompletion: false,\n isMemberCompletion: false,\n isNewIdentifierLocation: false,\n entries: items.map((item) => {\n let kind: ts.ScriptElementKind;\n\n switch (item.kind) {\n case CompletionItemKind.Function:\n case CompletionItemKind.Constructor:\n kind = ts.ScriptElementKind.functionElement;\n break;\n case CompletionItemKind.Field:\n case CompletionItemKind.Variable:\n kind = ts.ScriptElementKind.memberVariableElement;\n break;\n default:\n kind = ts.ScriptElementKind.unknown;\n break;\n }\n\n return {\n name: item.label,\n kindModifiers: \"\",\n kind,\n sortText: \"\",\n };\n }),\n };\n };\n\n proxy.getSemanticDiagnostics = (fileName) => {\n const diagnostics = languageService.getSemanticDiagnostics(fileName);\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return diagnostics;\n }\n\n ts.forEachChild(sourceFile, function visitor(node) {\n if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && isGraphqlTag(node.tag.getText())) {\n try {\n const { query, offset } = fix(node);\n const _diagnostics = getDiagnostics(query, schema);\n\n for (const {\n range: { start, end },\n severity,\n message,\n } of _diagnostics) {\n diagnostics.push({\n category: getDiagnosticCategory(severity),\n code: 9999,\n messageText: message,\n file: sourceFile,\n start: start.character + offset,\n length: end.character - start.character,\n });\n }\n } catch (error) {\n if (error instanceof GraphQLError) {\n diagnostics.push({\n category: ts.DiagnosticCategory.Error,\n code: 9999,\n messageText: error.message,\n file: sourceFile,\n start: node.template.getStart() + 1,\n length: node.template.getWidth() - 2,\n });\n }\n }\n }\n\n ts.forEachChild(node, visitor);\n });\n\n return diagnostics;\n };\n\n return proxy;\n },\n };\n};\n\nexport default init;\n"],"names":["resolve","buildSchema","printSchemaModel","readFileSync","watch","watchFile","unwatchFile","DiagnosticSeverity","parse","validate","Position","getTokenAtPosition","getHoverInformation","getAutocompleteSuggestions","CompletionItemKind","getDiagnostics","GraphQLError"],"mappings":";;;;;;;;;;MAcM,IAAI,GAA+B,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE;IAC1D,OAAO;QACL,MAAM,CAAC,IAAI;YACT,MAAM,eAAe,GAAG,IAAI,CAAC,eAAe,CAAC;YAC7C,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;YAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,mBAAmB,EAAE,CAAC;YAC/C,MAAM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,EAAE,EACrB,QAAQ,EACR,OAAO,GACR,IAAI,YAAY,EAAE;gCACjB,WAAW,CAAC,IAAI,CAAC;oCACf,QAAQ,EAAE,qBAAqB,CAAC,QAAQ,CAAC;oCACzC,IAAI,EAAE,IAAI;oCACV,WAAW,EAAE,OAAO;oCACpB,IAAI,EAAE,UAAU;oCAChB,KAAK,EAAE,KAAK,CAAC,SAAS,GAAG,MAAM;oCAC/B,MAAM,EAAE,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC,SAAS;iCACxC,CAAC,CAAC;6BACJ;yBACF;wBAAC,OAAO,KAAK,EAAE;4BACd,IAAI,KAAK,YAAYC,oBAAY,EAAE;gCACjC,WAAW,CAAC,IAAI,CAAC;oCACf,QAAQ,EAAE,EAAE,CAAC,kBAAkB,CAAC,KAAK;oCACrC,IAAI,EAAE,IAAI;oCACV,WAAW,EAAE,KAAK,CAAC,OAAO;oCAC1B,IAAI,EAAE,UAAU;oCAChB,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC;oCACnC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC;iCACrC,CAAC,CAAC;6BACJ;yBACF;qBACF;oBAED,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;iBAChC,CAAC,CAAC;gBAEH,OAAO,WAAW,CAAC;aACpB,CAAC;YAEF,OAAO,KAAK,CAAC;SACd;KACF,CAAC;AACJ;;;;"}
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { readFileSync, unwatchFile, watch, watchFile } from \"fs\";\nimport { resolve } from \"path\";\nimport { printSchemaModel } from \"@mo36924/graphql-schema\";\nimport { buildSchema, GraphQLError, parse, validate } from \"graphql\";\nimport {\n getAutocompleteSuggestions,\n getDiagnostics,\n getHoverInformation,\n getTokenAtPosition,\n} from \"graphql-language-service-interface\";\nimport { Position } from \"graphql-language-service-utils\";\nimport type { LanguageService, server, SourceFile, TaggedTemplateExpression } from \"typescript/lib/tsserverlibrary\";\nimport { CompletionItemKind, DiagnosticSeverity } from \"vscode-languageserver-types\";\n\nconst init: server.PluginModuleFactory = ({ typescript: ts }) => {\n return {\n create(info) {\n const languageService = info.languageService;\n const config = info.config;\n const cwd = info.project.getCurrentDirectory();\n const modelPath = config.model && resolve(cwd, config.model);\n const schemaPath = config.schema && resolve(cwd, config.schema);\n const watchPath = modelPath || schemaPath;\n let schema = buildSchema(\"scalar Unknown\");\n\n const addScalarUnknownType = (schemaCode: string) =>\n schemaCode.includes(\"scalar Unknown\") ? schemaCode : `${schemaCode}\\nscalar Unknown`;\n\n const changeModel = () => {\n schema = buildSchema(addScalarUnknownType(printSchemaModel(readFileSync(modelPath, \"utf8\"))));\n };\n\n const changeSchema = () => {\n schema = buildSchema(addScalarUnknownType(readFileSync(schemaPath, \"utf8\")));\n };\n\n const update = modelPath ? changeModel : changeSchema;\n\n const listener = () => {\n try {\n update();\n } catch {}\n };\n\n try {\n update();\n watch(watchPath, listener);\n } catch {\n watchFile(watchPath, () => {\n try {\n update();\n watch(watchPath, listener);\n unwatchFile(watchPath);\n } catch {}\n });\n }\n\n const getSourceFile = (fileName: string) => languageService.getProgram()?.getSourceFile(fileName);\n\n const isGraphqlTag = (tag: string): tag is \"query\" | \"mutation\" | \"subscription\" => {\n switch (tag) {\n case \"query\":\n case \"mutation\":\n case \"subscription\":\n return true;\n default:\n return false;\n }\n };\n\n const getDiagnosticCategory = (severity?: number) => {\n switch (severity) {\n case DiagnosticSeverity.Error:\n return ts.DiagnosticCategory.Error;\n case DiagnosticSeverity.Warning:\n return ts.DiagnosticCategory.Warning;\n case DiagnosticSeverity.Information:\n return ts.DiagnosticCategory.Message;\n case DiagnosticSeverity.Hint:\n return ts.DiagnosticCategory.Suggestion;\n default:\n return ts.DiagnosticCategory.Error;\n }\n };\n\n const hover = (sourceFile: SourceFile, position: number) => {\n const tag = ts.forEachChild(sourceFile, function visitor(node): true | undefined | TaggedTemplateExpression {\n if (position < node.pos) {\n return true;\n }\n\n if (position >= node.end) {\n return;\n }\n\n if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && isGraphqlTag(node.tag.getText())) {\n const template = node.template;\n\n if (ts.isNoSubstitutionTemplateLiteral(template)) {\n if (position >= template.getStart() + 1 && position < template.getEnd() - 1) {\n return node;\n }\n } else {\n const head = template.head;\n\n if (position >= head.getStart() + 1 && position < head.getEnd() - 2) {\n return node;\n }\n\n for (const { literal } of template.templateSpans) {\n if (\n position >= literal.getStart() + 1 &&\n position < literal.getEnd() - (ts.isTemplateMiddle(literal) ? 2 : 1)\n ) {\n return node;\n }\n }\n }\n }\n\n return ts.forEachChild(node, visitor);\n });\n\n if (tag === true) {\n return;\n }\n\n return tag;\n };\n\n const fix = (node: TaggedTemplateExpression) => {\n const template = node.template;\n let query = \"\";\n let variables = \"\";\n\n if (ts.isNoSubstitutionTemplateLiteral(template)) {\n // 2 ``\n const templateWidth = template.getWidth() - 2;\n query = template.text.padStart(templateWidth);\n } else {\n const head = template.head;\n const templateSpans = template.templateSpans;\n\n // 3 `...${\n const templateWidth = head.getWidth() - 3;\n query = head.text.padStart(templateWidth);\n\n templateSpans.forEach((span, i) => {\n const spanWidth = span.getFullWidth();\n const literal = span.literal;\n const literalWidth = literal.getWidth();\n const expressionWidth = spanWidth - literalWidth;\n const variableName = `$_${i}`;\n const variable = variableName.padStart(expressionWidth + 2).padEnd(expressionWidth + 3);\n const templateWidth = literalWidth - (ts.isTemplateTail(literal) ? 2 : 3);\n const template = literal.text.padStart(templateWidth);\n query += variable + template;\n variables += variableName + \":Unknown\";\n });\n }\n\n const tag = node.tag.getText();\n let offset = template.getStart() + 1;\n query = query.replace(/\\n|\\r/g, \" \");\n\n if (variables) {\n query = `${tag}(${variables}){${query}}`;\n offset -= tag.length + variables.length + 3;\n } else if (tag === \"query\") {\n query = `{${query}}`;\n offset -= 1;\n } else {\n query = `${tag}{${query}}`;\n offset -= tag.length + 1;\n }\n\n const documentNode = parse(query);\n const errors = validate(schema, documentNode);\n\n for (const error of errors) {\n const match = error.message.match(\n /^Variable \".*?\" of type \"Unknown\" used in position expecting type \"(.*?)\"\\.$/,\n );\n\n if (match) {\n query = query.replace(\"Unknown\", match[1]);\n offset += 7 - match[1].length;\n }\n }\n\n return {\n query,\n offset,\n };\n };\n\n const proxy: LanguageService = Object.create(null);\n\n for (const [key, value] of Object.entries(languageService)) {\n (proxy as any)[key] = value.bind(languageService);\n }\n\n proxy.getQuickInfoAtPosition = (fileName, position) => {\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return undefined;\n }\n\n const tag = hover(sourceFile, position);\n\n if (!tag) {\n return languageService.getQuickInfoAtPosition(fileName, position);\n }\n\n let result: { query: string; offset: number };\n\n try {\n result = fix(tag);\n } catch {\n return languageService.getQuickInfoAtPosition(fileName, position);\n }\n\n const { query, offset } = result;\n const cursor = new Position(0, position - offset + 1);\n const token = getTokenAtPosition(query, cursor);\n const marked = getHoverInformation(schema, query, cursor, token);\n\n if (marked === \"\" || typeof marked !== \"string\") {\n return;\n }\n\n return {\n kind: ts.ScriptElementKind.string,\n textSpan: {\n start: offset + token.start,\n length: token.end - token.start,\n },\n kindModifiers: \"\",\n displayParts: [{ text: marked, kind: \"\" }],\n };\n };\n\n proxy.getCompletionsAtPosition = (fileName, position, options) => {\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return undefined;\n }\n\n const tag = hover(sourceFile, position);\n\n if (!tag) {\n return languageService.getCompletionsAtPosition(fileName, position, options);\n }\n\n let result: { query: string; offset: number };\n\n try {\n result = fix(tag);\n } catch {\n return languageService.getCompletionsAtPosition(fileName, position, options);\n }\n\n const { query, offset } = result;\n const cursor = new Position(0, position - offset);\n const items = getAutocompleteSuggestions(schema, query, cursor);\n\n if (!items.length) {\n return;\n }\n\n return {\n isGlobalCompletion: false,\n isMemberCompletion: false,\n isNewIdentifierLocation: false,\n entries: items.map((item) => {\n let kind: ts.ScriptElementKind;\n\n switch (item.kind) {\n case CompletionItemKind.Function:\n case CompletionItemKind.Constructor:\n kind = ts.ScriptElementKind.functionElement;\n break;\n case CompletionItemKind.Field:\n case CompletionItemKind.Variable:\n kind = ts.ScriptElementKind.memberVariableElement;\n break;\n default:\n kind = ts.ScriptElementKind.unknown;\n break;\n }\n\n return {\n name: item.label,\n kindModifiers: \"\",\n kind,\n sortText: \"\",\n };\n }),\n };\n };\n\n proxy.getSemanticDiagnostics = (fileName) => {\n const diagnostics = languageService.getSemanticDiagnostics(fileName);\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return diagnostics;\n }\n\n ts.forEachChild(sourceFile, function visitor(node) {\n if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && isGraphqlTag(node.tag.getText())) {\n try {\n const { query, offset } = fix(node);\n const _diagnostics = getDiagnostics(query, schema);\n\n for (const {\n range: { start, end },\n severity,\n message,\n } of _diagnostics) {\n diagnostics.push({\n category: getDiagnosticCategory(severity),\n code: 9999,\n messageText: message,\n file: sourceFile,\n start: start.character + offset,\n length: end.character - start.character,\n });\n }\n } catch (error) {\n if (error instanceof GraphQLError) {\n diagnostics.push({\n category: ts.DiagnosticCategory.Error,\n code: 9999,\n messageText: error.message,\n file: sourceFile,\n start: node.template.getStart() + 1,\n length: node.template.getWidth() - 2,\n });\n }\n }\n }\n\n ts.forEachChild(node, visitor);\n });\n\n return diagnostics;\n };\n\n return proxy;\n },\n };\n};\n\nexport default init;\n"],"names":[],"mappings":";;;;;;;;MAcM,IAAI,GAA+B,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE;IAC1D,OAAO;QACL,MAAM,CAAC,IAAI;YACT,MAAM,eAAe,GAAG,IAAI,CAAC,eAAe,CAAC;YAC7C,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;YAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,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,EAAE,EACrB,QAAQ,EACR,OAAO,GACR,IAAI,YAAY,EAAE;gCACjB,WAAW,CAAC,IAAI,CAAC;oCACf,QAAQ,EAAE,qBAAqB,CAAC,QAAQ,CAAC;oCACzC,IAAI,EAAE,IAAI;oCACV,WAAW,EAAE,OAAO;oCACpB,IAAI,EAAE,UAAU;oCAChB,KAAK,EAAE,KAAK,CAAC,SAAS,GAAG,MAAM;oCAC/B,MAAM,EAAE,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC,SAAS;iCACxC,CAAC,CAAC;6BACJ;yBACF;wBAAC,OAAO,KAAK,EAAE;4BACd,IAAI,KAAK,YAAY,YAAY,EAAE;gCACjC,WAAW,CAAC,IAAI,CAAC;oCACf,QAAQ,EAAE,EAAE,CAAC,kBAAkB,CAAC,KAAK;oCACrC,IAAI,EAAE,IAAI;oCACV,WAAW,EAAE,KAAK,CAAC,OAAO;oCAC1B,IAAI,EAAE,UAAU;oCAChB,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC;oCACnC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC;iCACrC,CAAC,CAAC;6BACJ;yBACF;qBACF;oBAED,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;iBAChC,CAAC,CAAC;gBAEH,OAAO,WAAW,CAAC;aACpB,CAAC;YAEF,OAAO,KAAK,CAAC;SACd;KACF,CAAC;AACJ;;;;"}
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { readFileSync, unwatchFile, watch, watchFile } from \"fs\";\nimport { resolve } from \"path\";\nimport { printSchemaModel } from \"@mo36924/graphql-schema\";\nimport { GraphQLError, buildSchema, parse, validate } from \"graphql\";\nimport {\n getAutocompleteSuggestions,\n getDiagnostics,\n getHoverInformation,\n getTokenAtPosition,\n} from \"graphql-language-service-interface\";\nimport { Position } from \"graphql-language-service-utils\";\nimport type { LanguageService, SourceFile, TaggedTemplateExpression, server } from \"typescript/lib/tsserverlibrary\";\nimport { CompletionItemKind, DiagnosticSeverity } from \"vscode-languageserver-types\";\n\nconst init: server.PluginModuleFactory = ({ typescript: ts }) => {\n return {\n create(info) {\n const languageService = info.languageService;\n const config = info.config;\n const cwd = info.project.getCurrentDirectory();\n const modelPath = config.model && resolve(cwd, config.model);\n const schemaPath = config.schema && resolve(cwd, config.schema);\n const watchPath = modelPath || schemaPath;\n let schema = buildSchema(\"scalar Unknown\");\n\n const addScalarUnknownType = (schemaCode: string) =>\n schemaCode.includes(\"scalar Unknown\") ? schemaCode : `${schemaCode}\\nscalar Unknown`;\n\n const changeModel = () => {\n schema = buildSchema(addScalarUnknownType(printSchemaModel(readFileSync(modelPath, \"utf8\"))));\n };\n\n const changeSchema = () => {\n schema = buildSchema(addScalarUnknownType(readFileSync(schemaPath, \"utf8\")));\n };\n\n const update = modelPath ? changeModel : changeSchema;\n\n const listener = () => {\n try {\n update();\n } catch {}\n };\n\n try {\n update();\n watch(watchPath, listener);\n } catch {\n watchFile(watchPath, () => {\n try {\n update();\n watch(watchPath, listener);\n unwatchFile(watchPath);\n } catch {}\n });\n }\n\n const getSourceFile = (fileName: string) => languageService.getProgram()?.getSourceFile(fileName);\n\n const isGraphqlTag = (tag: string): tag is \"query\" | \"mutation\" | \"subscription\" => {\n switch (tag) {\n case \"query\":\n case \"mutation\":\n case \"subscription\":\n return true;\n default:\n return false;\n }\n };\n\n const getDiagnosticCategory = (severity?: number) => {\n switch (severity) {\n case DiagnosticSeverity.Error:\n return ts.DiagnosticCategory.Error;\n case DiagnosticSeverity.Warning:\n return ts.DiagnosticCategory.Warning;\n case DiagnosticSeverity.Information:\n return ts.DiagnosticCategory.Message;\n case DiagnosticSeverity.Hint:\n return ts.DiagnosticCategory.Suggestion;\n default:\n return ts.DiagnosticCategory.Error;\n }\n };\n\n const hover = (sourceFile: SourceFile, position: number) => {\n const tag = ts.forEachChild(sourceFile, function visitor(node): true | undefined | TaggedTemplateExpression {\n if (position < node.pos) {\n return true;\n }\n\n if (position >= node.end) {\n return;\n }\n\n if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && isGraphqlTag(node.tag.getText())) {\n const template = node.template;\n\n if (ts.isNoSubstitutionTemplateLiteral(template)) {\n if (position >= template.getStart() + 1 && position < template.getEnd() - 1) {\n return node;\n }\n } else {\n const head = template.head;\n\n if (position >= head.getStart() + 1 && position < head.getEnd() - 2) {\n return node;\n }\n\n for (const { literal } of template.templateSpans) {\n if (\n position >= literal.getStart() + 1 &&\n position < literal.getEnd() - (ts.isTemplateMiddle(literal) ? 2 : 1)\n ) {\n return node;\n }\n }\n }\n }\n\n return ts.forEachChild(node, visitor);\n });\n\n if (tag === true) {\n return;\n }\n\n return tag;\n };\n\n const fix = (node: TaggedTemplateExpression) => {\n const template = node.template;\n let query = \"\";\n let variables = \"\";\n\n if (ts.isNoSubstitutionTemplateLiteral(template)) {\n // 2 ``\n const templateWidth = template.getWidth() - 2;\n query = template.text.padStart(templateWidth);\n } else {\n const head = template.head;\n const templateSpans = template.templateSpans;\n\n // 3 `...${\n const templateWidth = head.getWidth() - 3;\n query = head.text.padStart(templateWidth);\n\n templateSpans.forEach((span, i) => {\n const spanWidth = span.getFullWidth();\n const literal = span.literal;\n const literalWidth = literal.getWidth();\n const expressionWidth = spanWidth - literalWidth;\n const variableName = `$_${i}`;\n const variable = variableName.padStart(expressionWidth + 2).padEnd(expressionWidth + 3);\n const templateWidth = literalWidth - (ts.isTemplateTail(literal) ? 2 : 3);\n const template = literal.text.padStart(templateWidth);\n query += variable + template;\n variables += variableName + \":Unknown\";\n });\n }\n\n const tag = node.tag.getText();\n let offset = template.getStart() + 1;\n query = query.replace(/\\n|\\r/g, \" \");\n\n if (variables) {\n query = `${tag}(${variables}){${query}}`;\n offset -= tag.length + variables.length + 3;\n } else if (tag === \"query\") {\n query = `{${query}}`;\n offset -= 1;\n } else {\n query = `${tag}{${query}}`;\n offset -= tag.length + 1;\n }\n\n const documentNode = parse(query);\n const errors = validate(schema, documentNode);\n\n for (const error of errors) {\n const match = error.message.match(\n /^Variable \".*?\" of type \"Unknown\" used in position expecting type \"(.*?)\"\\.$/,\n );\n\n if (match) {\n query = query.replace(\"Unknown\", match[1]);\n offset += 7 - match[1].length;\n }\n }\n\n return {\n query,\n offset,\n };\n };\n\n const proxy: LanguageService = Object.create(null);\n\n for (const [key, value] of Object.entries(languageService)) {\n (proxy as any)[key] = value.bind(languageService);\n }\n\n proxy.getQuickInfoAtPosition = (fileName, position) => {\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return undefined;\n }\n\n const tag = hover(sourceFile, position);\n\n if (!tag) {\n return languageService.getQuickInfoAtPosition(fileName, position);\n }\n\n let result: { query: string; offset: number };\n\n try {\n result = fix(tag);\n } catch {\n return languageService.getQuickInfoAtPosition(fileName, position);\n }\n\n const { query, offset } = result;\n const cursor = new Position(0, position - offset + 1);\n const token = getTokenAtPosition(query, cursor);\n const marked = getHoverInformation(schema, query, cursor, token);\n\n if (marked === \"\" || typeof marked !== \"string\") {\n return;\n }\n\n return {\n kind: ts.ScriptElementKind.string,\n textSpan: {\n start: offset + token.start,\n length: token.end - token.start,\n },\n kindModifiers: \"\",\n displayParts: [{ text: marked, kind: \"\" }],\n };\n };\n\n proxy.getCompletionsAtPosition = (fileName, position, options) => {\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return undefined;\n }\n\n const tag = hover(sourceFile, position);\n\n if (!tag) {\n return languageService.getCompletionsAtPosition(fileName, position, options);\n }\n\n let result: { query: string; offset: number };\n\n try {\n result = fix(tag);\n } catch {\n return languageService.getCompletionsAtPosition(fileName, position, options);\n }\n\n const { query, offset } = result;\n const cursor = new Position(0, position - offset);\n const items = getAutocompleteSuggestions(schema, query, cursor);\n\n if (!items.length) {\n return;\n }\n\n return {\n isGlobalCompletion: false,\n isMemberCompletion: false,\n isNewIdentifierLocation: false,\n entries: items.map((item) => {\n let kind: ts.ScriptElementKind;\n\n switch (item.kind) {\n case CompletionItemKind.Function:\n case CompletionItemKind.Constructor:\n kind = ts.ScriptElementKind.functionElement;\n break;\n case CompletionItemKind.Field:\n case CompletionItemKind.Variable:\n kind = ts.ScriptElementKind.memberVariableElement;\n break;\n default:\n kind = ts.ScriptElementKind.unknown;\n break;\n }\n\n return {\n name: item.label,\n kindModifiers: \"\",\n kind,\n sortText: \"\",\n };\n }),\n };\n };\n\n proxy.getSemanticDiagnostics = (fileName) => {\n const diagnostics = languageService.getSemanticDiagnostics(fileName);\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return diagnostics;\n }\n\n ts.forEachChild(sourceFile, function visitor(node) {\n if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && isGraphqlTag(node.tag.getText())) {\n try {\n const { query, offset } = fix(node);\n const _diagnostics = getDiagnostics(query, schema);\n\n for (const {\n range: { start, end },\n severity,\n message,\n } of _diagnostics) {\n diagnostics.push({\n category: getDiagnosticCategory(severity),\n code: 9999,\n messageText: message,\n file: sourceFile,\n start: start.character + offset,\n length: end.character - start.character,\n });\n }\n } catch (error) {\n if (error instanceof GraphQLError) {\n diagnostics.push({\n category: ts.DiagnosticCategory.Error,\n code: 9999,\n messageText: error.message,\n file: sourceFile,\n start: node.template.getStart() + 1,\n length: node.template.getWidth() - 2,\n });\n }\n }\n }\n\n ts.forEachChild(node, visitor);\n });\n\n return diagnostics;\n };\n\n return proxy;\n },\n };\n};\n\nexport default init;\n"],"names":[],"mappings":";;;;;;;;MAcM,IAAI,GAA+B,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE;IAC1D,OAAO;QACL,MAAM,CAAC,IAAI;YACT,MAAM,eAAe,GAAG,IAAI,CAAC,eAAe,CAAC;YAC7C,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;YAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,mBAAmB,EAAE,CAAC;YAC/C,MAAM,SA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YAAY,EAAE;gCACjB,WAAW,CAAC,IAAI,CAAC;oCACf,QAAQ,EAAE,qBAAqB,CAAC,QAAQ,CAAC;oCACzC,IAAI,EAAE,IAAI;oCACV,WAAW,EAAE,OAAO;oCACpB,IAAI,EAAE,UAAU;oCAChB,KAAK,EAAE,KAAK,CAAC,SAAS,GAAG,MAAM;oCAC/B,MAAM,EAAE,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC,SAAS;iCACxC,CAAC,CAAC;6BACJ;yBACF;wBAAC,OAAO,KAAK,EAAE;4BACd,IAAI,KAAK,YAAY,YAAY,EAAE;gCACjC,WAAW,CAAC,IAAI,CAAC;oCACf,QAAQ,EAAE,EAAE,CAAC,kBAAkB,CAAC,KAAK;oCACrC,IAAI,EAAE,IAAI;oCACV,WAAW,EAAE,KAAK,CAAC,OAAO;oCAC1B,IAAI,EAAE,UAAU;oCAChB,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC;oCACnC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC;iCACrC,CAAC,CAAC;6BACJ;yBACF;qBACF;oBAED,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;iBAChC,CAAC,CAAC;gBAEH,OAAO,WAAW,CAAC;aACpB,CAAC;YAEF,OAAO,KAAK,CAAC;SACd;KACF,CAAC;AACJ;;;;"}
|
package/dist/index.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["import { readFileSync, unwatchFile, watch, watchFile } from \"fs\";\nimport { resolve } from \"path\";\nimport { printSchemaModel } from \"@mo36924/graphql-schema\";\nimport { buildSchema, GraphQLError, parse, validate } from \"graphql\";\nimport {\n getAutocompleteSuggestions,\n getDiagnostics,\n getHoverInformation,\n getTokenAtPosition,\n} from \"graphql-language-service-interface\";\nimport { Position } from \"graphql-language-service-utils\";\nimport type { LanguageService, server, SourceFile, TaggedTemplateExpression } from \"typescript/lib/tsserverlibrary\";\nimport { CompletionItemKind, DiagnosticSeverity } from \"vscode-languageserver-types\";\n\nconst init: server.PluginModuleFactory = ({ typescript: ts }) => {\n return {\n create(info) {\n const languageService = info.languageService;\n const config = info.config;\n const cwd = info.project.getCurrentDirectory();\n const modelPath = config.model && resolve(cwd, config.model);\n const schemaPath = config.schema && resolve(cwd, config.schema);\n const watchPath = modelPath || schemaPath;\n let schema = buildSchema(\"scalar Unknown\");\n\n const addScalarUnknownType = (schemaCode: string) =>\n schemaCode.includes(\"scalar Unknown\") ? schemaCode : `${schemaCode}\\nscalar Unknown`;\n\n const changeModel = () => {\n schema = buildSchema(addScalarUnknownType(printSchemaModel(readFileSync(modelPath, \"utf8\"))));\n };\n\n const changeSchema = () => {\n schema = buildSchema(addScalarUnknownType(readFileSync(schemaPath, \"utf8\")));\n };\n\n const update = modelPath ? changeModel : changeSchema;\n\n const listener = () => {\n try {\n update();\n } catch {}\n };\n\n try {\n update();\n watch(watchPath, listener);\n } catch {\n watchFile(watchPath, () => {\n try {\n update();\n watch(watchPath, listener);\n unwatchFile(watchPath);\n } catch {}\n });\n }\n\n const getSourceFile = (fileName: string) => languageService.getProgram()?.getSourceFile(fileName);\n\n const isGraphqlTag = (tag: string): tag is \"query\" | \"mutation\" | \"subscription\" => {\n switch (tag) {\n case \"query\":\n case \"mutation\":\n case \"subscription\":\n return true;\n default:\n return false;\n }\n };\n\n const getDiagnosticCategory = (severity?: number) => {\n switch (severity) {\n case DiagnosticSeverity.Error:\n return ts.DiagnosticCategory.Error;\n case DiagnosticSeverity.Warning:\n return ts.DiagnosticCategory.Warning;\n case DiagnosticSeverity.Information:\n return ts.DiagnosticCategory.Message;\n case DiagnosticSeverity.Hint:\n return ts.DiagnosticCategory.Suggestion;\n default:\n return ts.DiagnosticCategory.Error;\n }\n };\n\n const hover = (sourceFile: SourceFile, position: number) => {\n const tag = ts.forEachChild(sourceFile, function visitor(node): true | undefined | TaggedTemplateExpression {\n if (position < node.pos) {\n return true;\n }\n\n if (position >= node.end) {\n return;\n }\n\n if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && isGraphqlTag(node.tag.getText())) {\n const template = node.template;\n\n if (ts.isNoSubstitutionTemplateLiteral(template)) {\n if (position >= template.getStart() + 1 && position < template.getEnd() - 1) {\n return node;\n }\n } else {\n const head = template.head;\n\n if (position >= head.getStart() + 1 && position < head.getEnd() - 2) {\n return node;\n }\n\n for (const { literal } of template.templateSpans) {\n if (\n position >= literal.getStart() + 1 &&\n position < literal.getEnd() - (ts.isTemplateMiddle(literal) ? 2 : 1)\n ) {\n return node;\n }\n }\n }\n }\n\n return ts.forEachChild(node, visitor);\n });\n\n if (tag === true) {\n return;\n }\n\n return tag;\n };\n\n const fix = (node: TaggedTemplateExpression) => {\n const template = node.template;\n let query = \"\";\n let variables = \"\";\n\n if (ts.isNoSubstitutionTemplateLiteral(template)) {\n // 2 ``\n const templateWidth = template.getWidth() - 2;\n query = template.text.padStart(templateWidth);\n } else {\n const head = template.head;\n const templateSpans = template.templateSpans;\n\n // 3 `...${\n const templateWidth = head.getWidth() - 3;\n query = head.text.padStart(templateWidth);\n\n templateSpans.forEach((span, i) => {\n const spanWidth = span.getFullWidth();\n const literal = span.literal;\n const literalWidth = literal.getWidth();\n const expressionWidth = spanWidth - literalWidth;\n const variableName = `$_${i}`;\n const variable = variableName.padStart(expressionWidth + 2).padEnd(expressionWidth + 3);\n const templateWidth = literalWidth - (ts.isTemplateTail(literal) ? 2 : 3);\n const template = literal.text.padStart(templateWidth);\n query += variable + template;\n variables += variableName + \":Unknown\";\n });\n }\n\n const tag = node.tag.getText();\n let offset = template.getStart() + 1;\n query = query.replace(/\\n|\\r/g, \" \");\n\n if (variables) {\n query = `${tag}(${variables}){${query}}`;\n offset -= tag.length + variables.length + 3;\n } else if (tag === \"query\") {\n query = `{${query}}`;\n offset -= 1;\n } else {\n query = `${tag}{${query}}`;\n offset -= tag.length + 1;\n }\n\n const documentNode = parse(query);\n const errors = validate(schema, documentNode);\n\n for (const error of errors) {\n const match = error.message.match(\n /^Variable \".*?\" of type \"Unknown\" used in position expecting type \"(.*?)\"\\.$/,\n );\n\n if (match) {\n query = query.replace(\"Unknown\", match[1]);\n offset += 7 - match[1].length;\n }\n }\n\n return {\n query,\n offset,\n };\n };\n\n const proxy: LanguageService = Object.create(null);\n\n for (const [key, value] of Object.entries(languageService)) {\n (proxy as any)[key] = value.bind(languageService);\n }\n\n proxy.getQuickInfoAtPosition = (fileName, position) => {\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return undefined;\n }\n\n const tag = hover(sourceFile, position);\n\n if (!tag) {\n return languageService.getQuickInfoAtPosition(fileName, position);\n }\n\n let result: { query: string; offset: number };\n\n try {\n result = fix(tag);\n } catch {\n return languageService.getQuickInfoAtPosition(fileName, position);\n }\n\n const { query, offset } = result;\n const cursor = new Position(0, position - offset + 1);\n const token = getTokenAtPosition(query, cursor);\n const marked = getHoverInformation(schema, query, cursor, token);\n\n if (marked === \"\" || typeof marked !== \"string\") {\n return;\n }\n\n return {\n kind: ts.ScriptElementKind.string,\n textSpan: {\n start: offset + token.start,\n length: token.end - token.start,\n },\n kindModifiers: \"\",\n displayParts: [{ text: marked, kind: \"\" }],\n };\n };\n\n proxy.getCompletionsAtPosition = (fileName, position, options) => {\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return undefined;\n }\n\n const tag = hover(sourceFile, position);\n\n if (!tag) {\n return languageService.getCompletionsAtPosition(fileName, position, options);\n }\n\n let result: { query: string; offset: number };\n\n try {\n result = fix(tag);\n } catch {\n return languageService.getCompletionsAtPosition(fileName, position, options);\n }\n\n const { query, offset } = result;\n const cursor = new Position(0, position - offset);\n const items = getAutocompleteSuggestions(schema, query, cursor);\n\n if (!items.length) {\n return;\n }\n\n return {\n isGlobalCompletion: false,\n isMemberCompletion: false,\n isNewIdentifierLocation: false,\n entries: items.map((item) => {\n let kind: ts.ScriptElementKind;\n\n switch (item.kind) {\n case CompletionItemKind.Function:\n case CompletionItemKind.Constructor:\n kind = ts.ScriptElementKind.functionElement;\n break;\n case CompletionItemKind.Field:\n case CompletionItemKind.Variable:\n kind = ts.ScriptElementKind.memberVariableElement;\n break;\n default:\n kind = ts.ScriptElementKind.unknown;\n break;\n }\n\n return {\n name: item.label,\n kindModifiers: \"\",\n kind,\n sortText: \"\",\n };\n }),\n };\n };\n\n proxy.getSemanticDiagnostics = (fileName) => {\n const diagnostics = languageService.getSemanticDiagnostics(fileName);\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return diagnostics;\n }\n\n ts.forEachChild(sourceFile, function visitor(node) {\n if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && isGraphqlTag(node.tag.getText())) {\n try {\n const { query, offset } = fix(node);\n const _diagnostics = getDiagnostics(query, schema);\n\n for (const {\n range: { start, end },\n severity,\n message,\n } of _diagnostics) {\n diagnostics.push({\n category: getDiagnosticCategory(severity),\n code: 9999,\n messageText: message,\n file: sourceFile,\n start: start.character + offset,\n length: end.character - start.character,\n });\n }\n } catch (error) {\n if (error instanceof GraphQLError) {\n diagnostics.push({\n category: ts.DiagnosticCategory.Error,\n code: 9999,\n messageText: error.message,\n file: sourceFile,\n start: node.template.getStart() + 1,\n length: node.template.getWidth() - 2,\n });\n }\n }\n }\n\n ts.forEachChild(node, visitor);\n });\n\n return diagnostics;\n };\n\n return proxy;\n },\n };\n};\n\nexport default init;\n"],"names":[],"mappings":";;;;;;;;MAcM,IAAI,GAA+B,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE;IAC1D,OAAO;QACL,MAAM,CAAC,IAAI;YACT,MAAM,eAAe,GAAG,IAAI,CAAC,eAAe,CAAC;YAC7C,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;YAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,mBAAmB,EAAE,CAAC;YAC/C,MAAM,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I,YAAY,EAAE;gCACjB,WAAW,CAAC,IAAI,CAAC;oCACf,QAAQ,EAAE,qBAAqB,CAAC,QAAQ,CAAC;oCACzC,IAAI,EAAE,IAAI;oCACV,WAAW,EAAE,OAAO;oCACpB,IAAI,EAAE,UAAU;oCAChB,KAAK,EAAE,KAAK,CAAC,SAAS,GAAG,MAAM;oCAC/B,MAAM,EAAE,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC,SAAS;iCACxC,CAAC,CAAC;6BACJ;yBACF;wBAAC,OAAO,KAAK,EAAE;4BACd,IAAI,KAAK,YAAY,YAAY,EAAE;gCACjC,WAAW,CAAC,IAAI,CAAC;oCACf,QAAQ,EAAE,EAAE,CAAC,kBAAkB,CAAC,KAAK;oCACrC,IAAI,EAAE,IAAI;oCACV,WAAW,EAAE,KAAK,CAAC,OAAO;oCAC1B,IAAI,EAAE,UAAU;oCAChB,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC;oCACnC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC;iCACrC,CAAC,CAAC;6BACJ;yBACF;qBACF;oBAED,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;iBAChC,CAAC,CAAC;gBAEH,OAAO,WAAW,CAAC;aACpB,CAAC;YAEF,OAAO,KAAK,CAAC;SACd;KACF,CAAC;AACJ;;;;"}
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["import { readFileSync, unwatchFile, watch, watchFile } from \"fs\";\nimport { resolve } from \"path\";\nimport { printSchemaModel } from \"@mo36924/graphql-schema\";\nimport { GraphQLError, buildSchema, parse, validate } from \"graphql\";\nimport {\n getAutocompleteSuggestions,\n getDiagnostics,\n getHoverInformation,\n getTokenAtPosition,\n} from \"graphql-language-service-interface\";\nimport { Position } from \"graphql-language-service-utils\";\nimport type { LanguageService, SourceFile, TaggedTemplateExpression, server } from \"typescript/lib/tsserverlibrary\";\nimport { CompletionItemKind, DiagnosticSeverity } from \"vscode-languageserver-types\";\n\nconst init: server.PluginModuleFactory = ({ typescript: ts }) => {\n return {\n create(info) {\n const languageService = info.languageService;\n const config = info.config;\n const cwd = info.project.getCurrentDirectory();\n const modelPath = config.model && resolve(cwd, config.model);\n const schemaPath = config.schema && resolve(cwd, config.schema);\n const watchPath = modelPath || schemaPath;\n let schema = buildSchema(\"scalar Unknown\");\n\n const addScalarUnknownType = (schemaCode: string) =>\n schemaCode.includes(\"scalar Unknown\") ? schemaCode : `${schemaCode}\\nscalar Unknown`;\n\n const changeModel = () => {\n schema = buildSchema(addScalarUnknownType(printSchemaModel(readFileSync(modelPath, \"utf8\"))));\n };\n\n const changeSchema = () => {\n schema = buildSchema(addScalarUnknownType(readFileSync(schemaPath, \"utf8\")));\n };\n\n const update = modelPath ? changeModel : changeSchema;\n\n const listener = () => {\n try {\n update();\n } catch {}\n };\n\n try {\n update();\n watch(watchPath, listener);\n } catch {\n watchFile(watchPath, () => {\n try {\n update();\n watch(watchPath, listener);\n unwatchFile(watchPath);\n } catch {}\n });\n }\n\n const getSourceFile = (fileName: string) => languageService.getProgram()?.getSourceFile(fileName);\n\n const isGraphqlTag = (tag: string): tag is \"query\" | \"mutation\" | \"subscription\" => {\n switch (tag) {\n case \"query\":\n case \"mutation\":\n case \"subscription\":\n return true;\n default:\n return false;\n }\n };\n\n const getDiagnosticCategory = (severity?: number) => {\n switch (severity) {\n case DiagnosticSeverity.Error:\n return ts.DiagnosticCategory.Error;\n case DiagnosticSeverity.Warning:\n return ts.DiagnosticCategory.Warning;\n case DiagnosticSeverity.Information:\n return ts.DiagnosticCategory.Message;\n case DiagnosticSeverity.Hint:\n return ts.DiagnosticCategory.Suggestion;\n default:\n return ts.DiagnosticCategory.Error;\n }\n };\n\n const hover = (sourceFile: SourceFile, position: number) => {\n const tag = ts.forEachChild(sourceFile, function visitor(node): true | undefined | TaggedTemplateExpression {\n if (position < node.pos) {\n return true;\n }\n\n if (position >= node.end) {\n return;\n }\n\n if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && isGraphqlTag(node.tag.getText())) {\n const template = node.template;\n\n if (ts.isNoSubstitutionTemplateLiteral(template)) {\n if (position >= template.getStart() + 1 && position < template.getEnd() - 1) {\n return node;\n }\n } else {\n const head = template.head;\n\n if (position >= head.getStart() + 1 && position < head.getEnd() - 2) {\n return node;\n }\n\n for (const { literal } of template.templateSpans) {\n if (\n position >= literal.getStart() + 1 &&\n position < literal.getEnd() - (ts.isTemplateMiddle(literal) ? 2 : 1)\n ) {\n return node;\n }\n }\n }\n }\n\n return ts.forEachChild(node, visitor);\n });\n\n if (tag === true) {\n return;\n }\n\n return tag;\n };\n\n const fix = (node: TaggedTemplateExpression) => {\n const template = node.template;\n let query = \"\";\n let variables = \"\";\n\n if (ts.isNoSubstitutionTemplateLiteral(template)) {\n // 2 ``\n const templateWidth = template.getWidth() - 2;\n query = template.text.padStart(templateWidth);\n } else {\n const head = template.head;\n const templateSpans = template.templateSpans;\n\n // 3 `...${\n const templateWidth = head.getWidth() - 3;\n query = head.text.padStart(templateWidth);\n\n templateSpans.forEach((span, i) => {\n const spanWidth = span.getFullWidth();\n const literal = span.literal;\n const literalWidth = literal.getWidth();\n const expressionWidth = spanWidth - literalWidth;\n const variableName = `$_${i}`;\n const variable = variableName.padStart(expressionWidth + 2).padEnd(expressionWidth + 3);\n const templateWidth = literalWidth - (ts.isTemplateTail(literal) ? 2 : 3);\n const template = literal.text.padStart(templateWidth);\n query += variable + template;\n variables += variableName + \":Unknown\";\n });\n }\n\n const tag = node.tag.getText();\n let offset = template.getStart() + 1;\n query = query.replace(/\\n|\\r/g, \" \");\n\n if (variables) {\n query = `${tag}(${variables}){${query}}`;\n offset -= tag.length + variables.length + 3;\n } else if (tag === \"query\") {\n query = `{${query}}`;\n offset -= 1;\n } else {\n query = `${tag}{${query}}`;\n offset -= tag.length + 1;\n }\n\n const documentNode = parse(query);\n const errors = validate(schema, documentNode);\n\n for (const error of errors) {\n const match = error.message.match(\n /^Variable \".*?\" of type \"Unknown\" used in position expecting type \"(.*?)\"\\.$/,\n );\n\n if (match) {\n query = query.replace(\"Unknown\", match[1]);\n offset += 7 - match[1].length;\n }\n }\n\n return {\n query,\n offset,\n };\n };\n\n const proxy: LanguageService = Object.create(null);\n\n for (const [key, value] of Object.entries(languageService)) {\n (proxy as any)[key] = value.bind(languageService);\n }\n\n proxy.getQuickInfoAtPosition = (fileName, position) => {\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return undefined;\n }\n\n const tag = hover(sourceFile, position);\n\n if (!tag) {\n return languageService.getQuickInfoAtPosition(fileName, position);\n }\n\n let result: { query: string; offset: number };\n\n try {\n result = fix(tag);\n } catch {\n return languageService.getQuickInfoAtPosition(fileName, position);\n }\n\n const { query, offset } = result;\n const cursor = new Position(0, position - offset + 1);\n const token = getTokenAtPosition(query, cursor);\n const marked = getHoverInformation(schema, query, cursor, token);\n\n if (marked === \"\" || typeof marked !== \"string\") {\n return;\n }\n\n return {\n kind: ts.ScriptElementKind.string,\n textSpan: {\n start: offset + token.start,\n length: token.end - token.start,\n },\n kindModifiers: \"\",\n displayParts: [{ text: marked, kind: \"\" }],\n };\n };\n\n proxy.getCompletionsAtPosition = (fileName, position, options) => {\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return undefined;\n }\n\n const tag = hover(sourceFile, position);\n\n if (!tag) {\n return languageService.getCompletionsAtPosition(fileName, position, options);\n }\n\n let result: { query: string; offset: number };\n\n try {\n result = fix(tag);\n } catch {\n return languageService.getCompletionsAtPosition(fileName, position, options);\n }\n\n const { query, offset } = result;\n const cursor = new Position(0, position - offset);\n const items = getAutocompleteSuggestions(schema, query, cursor);\n\n if (!items.length) {\n return;\n }\n\n return {\n isGlobalCompletion: false,\n isMemberCompletion: false,\n isNewIdentifierLocation: false,\n entries: items.map((item) => {\n let kind: ts.ScriptElementKind;\n\n switch (item.kind) {\n case CompletionItemKind.Function:\n case CompletionItemKind.Constructor:\n kind = ts.ScriptElementKind.functionElement;\n break;\n case CompletionItemKind.Field:\n case CompletionItemKind.Variable:\n kind = ts.ScriptElementKind.memberVariableElement;\n break;\n default:\n kind = ts.ScriptElementKind.unknown;\n break;\n }\n\n return {\n name: item.label,\n kindModifiers: \"\",\n kind,\n sortText: \"\",\n };\n }),\n };\n };\n\n proxy.getSemanticDiagnostics = (fileName) => {\n const diagnostics = languageService.getSemanticDiagnostics(fileName);\n const sourceFile = getSourceFile(fileName);\n\n if (!sourceFile) {\n return diagnostics;\n }\n\n ts.forEachChild(sourceFile, function visitor(node) {\n if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && isGraphqlTag(node.tag.getText())) {\n try {\n const { query, offset } = fix(node);\n const _diagnostics = getDiagnostics(query, schema);\n\n for (const {\n range: { start, end },\n severity,\n message,\n } of _diagnostics) {\n diagnostics.push({\n category: getDiagnosticCategory(severity),\n code: 9999,\n messageText: message,\n file: sourceFile,\n start: start.character + offset,\n length: end.character - start.character,\n });\n }\n } catch (error) {\n if (error instanceof GraphQLError) {\n diagnostics.push({\n category: ts.DiagnosticCategory.Error,\n code: 9999,\n messageText: error.message,\n file: sourceFile,\n start: node.template.getStart() + 1,\n length: node.template.getWidth() - 2,\n });\n }\n }\n }\n\n ts.forEachChild(node, visitor);\n });\n\n return diagnostics;\n };\n\n return proxy;\n },\n };\n};\n\nexport default init;\n"],"names":[],"mappings":";;;;;;;;MAcM,IAAI,GAA+B,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE;IAC1D,OAAO;QACL,MAAM,CAAC,IAAI;YACT,MAAM,eAAe,GAAG,IAAI,CAAC,eAAe,CAAC;YAC7C,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;YAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,mBAAmB,EAAE,CAAC;YAC/C,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,IAAI,OAA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CAAC;oCACf,QAAQ,EAAE,qBAAqB,CAAC,QAAQ,CAAC;oCACzC,IAAI,EAAE,IAAI;oCACV,WAAW,EAAE,OAAO;oCACpB,IAAI,EAAE,UAAU;oCAChB,KAAK,EAAE,KAAK,CAAC,SAAS,GAAG,MAAM;oCAC/B,MAAM,EAAE,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC,SAAS;iCACxC,CAAC,CAAC;6BACJ;yBACF;wBAAC,OAAO,KAAK,EAAE;4BACd,IAAI,KAAK,YAAY,YAAY,EAAE;gCACjC,WAAW,CAAC,IAAI,CAAC;oCACf,QAAQ,EAAE,EAAE,CAAC,kBAAkB,CAAC,KAAK;oCACrC,IAAI,EAAE,IAAI;oCACV,WAAW,EAAE,KAAK,CAAC,OAAO;oCAC1B,IAAI,EAAE,UAAU;oCAChB,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC;oCACnC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC;iCACrC,CAAC,CAAC;6BACJ;yBACF;qBACF;oBAED,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;iBAChC,CAAC,CAAC;gBAEH,OAAO,WAAW,CAAC;aACpB,CAAC;YAEF,OAAO,KAAK,CAAC;SACd;KACF,CAAC;AACJ;;;;"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@mo36924/graphql-plugin",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.6.1",
|
4
4
|
"description": "graphql-plugin",
|
5
5
|
"keywords": [],
|
6
6
|
"homepage": "https://github.com/mo36924/monorepo#readme",
|
@@ -25,6 +25,7 @@
|
|
25
25
|
},
|
26
26
|
"main": "./dist/index.cjs",
|
27
27
|
"module": "./dist/index.mjs",
|
28
|
+
"types": "./dist/index.d.ts",
|
28
29
|
"typesVersions": {
|
29
30
|
"*": {
|
30
31
|
"*": [
|
@@ -32,16 +33,19 @@
|
|
32
33
|
]
|
33
34
|
}
|
34
35
|
},
|
36
|
+
"files": [
|
37
|
+
"dist"
|
38
|
+
],
|
35
39
|
"dependencies": {
|
36
|
-
"@mo36924/graphql-schema": "^1.
|
37
|
-
"graphql": "^
|
38
|
-
"graphql-language-service-interface": "^2.
|
39
|
-
"graphql-language-service-utils": "^2.
|
40
|
-
"typescript": "^4.3
|
40
|
+
"@mo36924/graphql-schema": "^1.6.1",
|
41
|
+
"graphql": "^16.3.0",
|
42
|
+
"graphql-language-service-interface": "^2.10.2",
|
43
|
+
"graphql-language-service-utils": "^2.7.1",
|
44
|
+
"typescript": "^4.6.3",
|
41
45
|
"vscode-languageserver-types": "^3.16.0"
|
42
46
|
},
|
43
47
|
"publishConfig": {
|
44
48
|
"access": "public"
|
45
49
|
},
|
46
|
-
"gitHead": "
|
50
|
+
"gitHead": "6db82b81fcee91132bd8d1fae25b0fd429e31d41"
|
47
51
|
}
|
package/src/index.ts
DELETED
@@ -1,357 +0,0 @@
|
|
1
|
-
import { readFileSync, unwatchFile, watch, watchFile } from "fs";
|
2
|
-
import { resolve } from "path";
|
3
|
-
import { printSchemaModel } from "@mo36924/graphql-schema";
|
4
|
-
import { buildSchema, GraphQLError, parse, validate } from "graphql";
|
5
|
-
import {
|
6
|
-
getAutocompleteSuggestions,
|
7
|
-
getDiagnostics,
|
8
|
-
getHoverInformation,
|
9
|
-
getTokenAtPosition,
|
10
|
-
} from "graphql-language-service-interface";
|
11
|
-
import { Position } from "graphql-language-service-utils";
|
12
|
-
import type { LanguageService, server, SourceFile, TaggedTemplateExpression } from "typescript/lib/tsserverlibrary";
|
13
|
-
import { CompletionItemKind, DiagnosticSeverity } from "vscode-languageserver-types";
|
14
|
-
|
15
|
-
const init: server.PluginModuleFactory = ({ typescript: ts }) => {
|
16
|
-
return {
|
17
|
-
create(info) {
|
18
|
-
const languageService = info.languageService;
|
19
|
-
const config = info.config;
|
20
|
-
const cwd = info.project.getCurrentDirectory();
|
21
|
-
const modelPath = config.model && resolve(cwd, config.model);
|
22
|
-
const schemaPath = config.schema && resolve(cwd, config.schema);
|
23
|
-
const watchPath = modelPath || schemaPath;
|
24
|
-
let schema = buildSchema("scalar Unknown");
|
25
|
-
|
26
|
-
const addScalarUnknownType = (schemaCode: string) =>
|
27
|
-
schemaCode.includes("scalar Unknown") ? schemaCode : `${schemaCode}\nscalar Unknown`;
|
28
|
-
|
29
|
-
const changeModel = () => {
|
30
|
-
schema = buildSchema(addScalarUnknownType(printSchemaModel(readFileSync(modelPath, "utf8"))));
|
31
|
-
};
|
32
|
-
|
33
|
-
const changeSchema = () => {
|
34
|
-
schema = buildSchema(addScalarUnknownType(readFileSync(schemaPath, "utf8")));
|
35
|
-
};
|
36
|
-
|
37
|
-
const update = modelPath ? changeModel : changeSchema;
|
38
|
-
|
39
|
-
const listener = () => {
|
40
|
-
try {
|
41
|
-
update();
|
42
|
-
} catch {}
|
43
|
-
};
|
44
|
-
|
45
|
-
try {
|
46
|
-
update();
|
47
|
-
watch(watchPath, listener);
|
48
|
-
} catch {
|
49
|
-
watchFile(watchPath, () => {
|
50
|
-
try {
|
51
|
-
update();
|
52
|
-
watch(watchPath, listener);
|
53
|
-
unwatchFile(watchPath);
|
54
|
-
} catch {}
|
55
|
-
});
|
56
|
-
}
|
57
|
-
|
58
|
-
const getSourceFile = (fileName: string) => languageService.getProgram()?.getSourceFile(fileName);
|
59
|
-
|
60
|
-
const isGraphqlTag = (tag: string): tag is "query" | "mutation" | "subscription" => {
|
61
|
-
switch (tag) {
|
62
|
-
case "query":
|
63
|
-
case "mutation":
|
64
|
-
case "subscription":
|
65
|
-
return true;
|
66
|
-
default:
|
67
|
-
return false;
|
68
|
-
}
|
69
|
-
};
|
70
|
-
|
71
|
-
const getDiagnosticCategory = (severity?: number) => {
|
72
|
-
switch (severity) {
|
73
|
-
case DiagnosticSeverity.Error:
|
74
|
-
return ts.DiagnosticCategory.Error;
|
75
|
-
case DiagnosticSeverity.Warning:
|
76
|
-
return ts.DiagnosticCategory.Warning;
|
77
|
-
case DiagnosticSeverity.Information:
|
78
|
-
return ts.DiagnosticCategory.Message;
|
79
|
-
case DiagnosticSeverity.Hint:
|
80
|
-
return ts.DiagnosticCategory.Suggestion;
|
81
|
-
default:
|
82
|
-
return ts.DiagnosticCategory.Error;
|
83
|
-
}
|
84
|
-
};
|
85
|
-
|
86
|
-
const hover = (sourceFile: SourceFile, position: number) => {
|
87
|
-
const tag = ts.forEachChild(sourceFile, function visitor(node): true | undefined | TaggedTemplateExpression {
|
88
|
-
if (position < node.pos) {
|
89
|
-
return true;
|
90
|
-
}
|
91
|
-
|
92
|
-
if (position >= node.end) {
|
93
|
-
return;
|
94
|
-
}
|
95
|
-
|
96
|
-
if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && isGraphqlTag(node.tag.getText())) {
|
97
|
-
const template = node.template;
|
98
|
-
|
99
|
-
if (ts.isNoSubstitutionTemplateLiteral(template)) {
|
100
|
-
if (position >= template.getStart() + 1 && position < template.getEnd() - 1) {
|
101
|
-
return node;
|
102
|
-
}
|
103
|
-
} else {
|
104
|
-
const head = template.head;
|
105
|
-
|
106
|
-
if (position >= head.getStart() + 1 && position < head.getEnd() - 2) {
|
107
|
-
return node;
|
108
|
-
}
|
109
|
-
|
110
|
-
for (const { literal } of template.templateSpans) {
|
111
|
-
if (
|
112
|
-
position >= literal.getStart() + 1 &&
|
113
|
-
position < literal.getEnd() - (ts.isTemplateMiddle(literal) ? 2 : 1)
|
114
|
-
) {
|
115
|
-
return node;
|
116
|
-
}
|
117
|
-
}
|
118
|
-
}
|
119
|
-
}
|
120
|
-
|
121
|
-
return ts.forEachChild(node, visitor);
|
122
|
-
});
|
123
|
-
|
124
|
-
if (tag === true) {
|
125
|
-
return;
|
126
|
-
}
|
127
|
-
|
128
|
-
return tag;
|
129
|
-
};
|
130
|
-
|
131
|
-
const fix = (node: TaggedTemplateExpression) => {
|
132
|
-
const template = node.template;
|
133
|
-
let query = "";
|
134
|
-
let variables = "";
|
135
|
-
|
136
|
-
if (ts.isNoSubstitutionTemplateLiteral(template)) {
|
137
|
-
// 2 ``
|
138
|
-
const templateWidth = template.getWidth() - 2;
|
139
|
-
query = template.text.padStart(templateWidth);
|
140
|
-
} else {
|
141
|
-
const head = template.head;
|
142
|
-
const templateSpans = template.templateSpans;
|
143
|
-
|
144
|
-
// 3 `...${
|
145
|
-
const templateWidth = head.getWidth() - 3;
|
146
|
-
query = head.text.padStart(templateWidth);
|
147
|
-
|
148
|
-
templateSpans.forEach((span, i) => {
|
149
|
-
const spanWidth = span.getFullWidth();
|
150
|
-
const literal = span.literal;
|
151
|
-
const literalWidth = literal.getWidth();
|
152
|
-
const expressionWidth = spanWidth - literalWidth;
|
153
|
-
const variableName = `$_${i}`;
|
154
|
-
const variable = variableName.padStart(expressionWidth + 2).padEnd(expressionWidth + 3);
|
155
|
-
const templateWidth = literalWidth - (ts.isTemplateTail(literal) ? 2 : 3);
|
156
|
-
const template = literal.text.padStart(templateWidth);
|
157
|
-
query += variable + template;
|
158
|
-
variables += variableName + ":Unknown";
|
159
|
-
});
|
160
|
-
}
|
161
|
-
|
162
|
-
const tag = node.tag.getText();
|
163
|
-
let offset = template.getStart() + 1;
|
164
|
-
query = query.replace(/\n|\r/g, " ");
|
165
|
-
|
166
|
-
if (variables) {
|
167
|
-
query = `${tag}(${variables}){${query}}`;
|
168
|
-
offset -= tag.length + variables.length + 3;
|
169
|
-
} else if (tag === "query") {
|
170
|
-
query = `{${query}}`;
|
171
|
-
offset -= 1;
|
172
|
-
} else {
|
173
|
-
query = `${tag}{${query}}`;
|
174
|
-
offset -= tag.length + 1;
|
175
|
-
}
|
176
|
-
|
177
|
-
const documentNode = parse(query);
|
178
|
-
const errors = validate(schema, documentNode);
|
179
|
-
|
180
|
-
for (const error of errors) {
|
181
|
-
const match = error.message.match(
|
182
|
-
/^Variable ".*?" of type "Unknown" used in position expecting type "(.*?)"\.$/,
|
183
|
-
);
|
184
|
-
|
185
|
-
if (match) {
|
186
|
-
query = query.replace("Unknown", match[1]);
|
187
|
-
offset += 7 - match[1].length;
|
188
|
-
}
|
189
|
-
}
|
190
|
-
|
191
|
-
return {
|
192
|
-
query,
|
193
|
-
offset,
|
194
|
-
};
|
195
|
-
};
|
196
|
-
|
197
|
-
const proxy: LanguageService = Object.create(null);
|
198
|
-
|
199
|
-
for (const [key, value] of Object.entries(languageService)) {
|
200
|
-
(proxy as any)[key] = value.bind(languageService);
|
201
|
-
}
|
202
|
-
|
203
|
-
proxy.getQuickInfoAtPosition = (fileName, position) => {
|
204
|
-
const sourceFile = getSourceFile(fileName);
|
205
|
-
|
206
|
-
if (!sourceFile) {
|
207
|
-
return undefined;
|
208
|
-
}
|
209
|
-
|
210
|
-
const tag = hover(sourceFile, position);
|
211
|
-
|
212
|
-
if (!tag) {
|
213
|
-
return languageService.getQuickInfoAtPosition(fileName, position);
|
214
|
-
}
|
215
|
-
|
216
|
-
let result: { query: string; offset: number };
|
217
|
-
|
218
|
-
try {
|
219
|
-
result = fix(tag);
|
220
|
-
} catch {
|
221
|
-
return languageService.getQuickInfoAtPosition(fileName, position);
|
222
|
-
}
|
223
|
-
|
224
|
-
const { query, offset } = result;
|
225
|
-
const cursor = new Position(0, position - offset + 1);
|
226
|
-
const token = getTokenAtPosition(query, cursor);
|
227
|
-
const marked = getHoverInformation(schema, query, cursor, token);
|
228
|
-
|
229
|
-
if (marked === "" || typeof marked !== "string") {
|
230
|
-
return;
|
231
|
-
}
|
232
|
-
|
233
|
-
return {
|
234
|
-
kind: ts.ScriptElementKind.string,
|
235
|
-
textSpan: {
|
236
|
-
start: offset + token.start,
|
237
|
-
length: token.end - token.start,
|
238
|
-
},
|
239
|
-
kindModifiers: "",
|
240
|
-
displayParts: [{ text: marked, kind: "" }],
|
241
|
-
};
|
242
|
-
};
|
243
|
-
|
244
|
-
proxy.getCompletionsAtPosition = (fileName, position, options) => {
|
245
|
-
const sourceFile = getSourceFile(fileName);
|
246
|
-
|
247
|
-
if (!sourceFile) {
|
248
|
-
return undefined;
|
249
|
-
}
|
250
|
-
|
251
|
-
const tag = hover(sourceFile, position);
|
252
|
-
|
253
|
-
if (!tag) {
|
254
|
-
return languageService.getCompletionsAtPosition(fileName, position, options);
|
255
|
-
}
|
256
|
-
|
257
|
-
let result: { query: string; offset: number };
|
258
|
-
|
259
|
-
try {
|
260
|
-
result = fix(tag);
|
261
|
-
} catch {
|
262
|
-
return languageService.getCompletionsAtPosition(fileName, position, options);
|
263
|
-
}
|
264
|
-
|
265
|
-
const { query, offset } = result;
|
266
|
-
const cursor = new Position(0, position - offset);
|
267
|
-
const items = getAutocompleteSuggestions(schema, query, cursor);
|
268
|
-
|
269
|
-
if (!items.length) {
|
270
|
-
return;
|
271
|
-
}
|
272
|
-
|
273
|
-
return {
|
274
|
-
isGlobalCompletion: false,
|
275
|
-
isMemberCompletion: false,
|
276
|
-
isNewIdentifierLocation: false,
|
277
|
-
entries: items.map((item) => {
|
278
|
-
let kind: ts.ScriptElementKind;
|
279
|
-
|
280
|
-
switch (item.kind) {
|
281
|
-
case CompletionItemKind.Function:
|
282
|
-
case CompletionItemKind.Constructor:
|
283
|
-
kind = ts.ScriptElementKind.functionElement;
|
284
|
-
break;
|
285
|
-
case CompletionItemKind.Field:
|
286
|
-
case CompletionItemKind.Variable:
|
287
|
-
kind = ts.ScriptElementKind.memberVariableElement;
|
288
|
-
break;
|
289
|
-
default:
|
290
|
-
kind = ts.ScriptElementKind.unknown;
|
291
|
-
break;
|
292
|
-
}
|
293
|
-
|
294
|
-
return {
|
295
|
-
name: item.label,
|
296
|
-
kindModifiers: "",
|
297
|
-
kind,
|
298
|
-
sortText: "",
|
299
|
-
};
|
300
|
-
}),
|
301
|
-
};
|
302
|
-
};
|
303
|
-
|
304
|
-
proxy.getSemanticDiagnostics = (fileName) => {
|
305
|
-
const diagnostics = languageService.getSemanticDiagnostics(fileName);
|
306
|
-
const sourceFile = getSourceFile(fileName);
|
307
|
-
|
308
|
-
if (!sourceFile) {
|
309
|
-
return diagnostics;
|
310
|
-
}
|
311
|
-
|
312
|
-
ts.forEachChild(sourceFile, function visitor(node) {
|
313
|
-
if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && isGraphqlTag(node.tag.getText())) {
|
314
|
-
try {
|
315
|
-
const { query, offset } = fix(node);
|
316
|
-
const _diagnostics = getDiagnostics(query, schema);
|
317
|
-
|
318
|
-
for (const {
|
319
|
-
range: { start, end },
|
320
|
-
severity,
|
321
|
-
message,
|
322
|
-
} of _diagnostics) {
|
323
|
-
diagnostics.push({
|
324
|
-
category: getDiagnosticCategory(severity),
|
325
|
-
code: 9999,
|
326
|
-
messageText: message,
|
327
|
-
file: sourceFile,
|
328
|
-
start: start.character + offset,
|
329
|
-
length: end.character - start.character,
|
330
|
-
});
|
331
|
-
}
|
332
|
-
} catch (error) {
|
333
|
-
if (error instanceof GraphQLError) {
|
334
|
-
diagnostics.push({
|
335
|
-
category: ts.DiagnosticCategory.Error,
|
336
|
-
code: 9999,
|
337
|
-
messageText: error.message,
|
338
|
-
file: sourceFile,
|
339
|
-
start: node.template.getStart() + 1,
|
340
|
-
length: node.template.getWidth() - 2,
|
341
|
-
});
|
342
|
-
}
|
343
|
-
}
|
344
|
-
}
|
345
|
-
|
346
|
-
ts.forEachChild(node, visitor);
|
347
|
-
});
|
348
|
-
|
349
|
-
return diagnostics;
|
350
|
-
};
|
351
|
-
|
352
|
-
return proxy;
|
353
|
-
},
|
354
|
-
};
|
355
|
-
};
|
356
|
-
|
357
|
-
export default init;
|