@mo36924/graphql-plugin 5.4.0 → 5.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,10 +1,13 @@
1
1
  'use strict';
2
2
 
3
- var graphql = require('@mo36924/graphql');
4
- var graphql$1 = require('graphql');
3
+ var graphql$1 = require('@mo36924/graphql');
4
+ var graphql$2 = require('graphql');
5
5
  var graphqlLanguageService = require('graphql-language-service');
6
+ var packages_graphqlPlugin_dist_volar = require('./volar.cjs');
7
+ require('@volar/typescript/lib/quickstart/createLanguageServicePlugin');
8
+ require('typescript');
6
9
 
7
- const init = ({ typescript: ts })=>{
10
+ const graphql = ({ typescript: ts })=>{
8
11
  const isQueryTag = (node)=>ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && node.tag.escapedText === "gql";
9
12
  const getScriptElementKind = (completionItemKind)=>{
10
13
  switch(completionItemKind){
@@ -65,11 +68,8 @@ const init = ({ typescript: ts })=>{
65
68
  };
66
69
  return {
67
70
  create (info) {
68
- const { schema } = graphql.getSchema(info.project.getCurrentDirectory());
71
+ const { schema } = graphql$1.getSchema(info.project.getCurrentDirectory());
69
72
  const languageService = info.languageService;
70
- if (!schema) {
71
- return languageService;
72
- }
73
73
  const getSourceFile = (fileName)=>languageService.getProgram()?.getSourceFile(fileName);
74
74
  const normalizeQuery = (node)=>{
75
75
  const template = node.template;
@@ -198,7 +198,7 @@ const init = ({ typescript: ts })=>{
198
198
  });
199
199
  }
200
200
  } catch (error) {
201
- if (error instanceof graphql$1.GraphQLError) {
201
+ if (error instanceof graphql$2.GraphQLError) {
202
202
  diagnostics.push({
203
203
  category: ts.DiagnosticCategory.Error,
204
204
  code: 9999,
@@ -220,4 +220,25 @@ const init = ({ typescript: ts })=>{
220
220
  };
221
221
  };
222
222
 
223
- module.exports = init;
223
+ const pluginModuleFactory = (mod)=>{
224
+ const pluginModules = [
225
+ graphql(mod),
226
+ packages_graphqlPlugin_dist_volar.volar(mod)
227
+ ];
228
+ return {
229
+ create (createInfo) {
230
+ return pluginModules.reduce((languageService, module)=>module.create({
231
+ ...createInfo,
232
+ languageService
233
+ }), createInfo.languageService);
234
+ },
235
+ getExternalFiles (proj, updateLevel) {
236
+ return pluginModules.flatMap((module)=>module.getExternalFiles?.(proj, updateLevel) ?? []);
237
+ },
238
+ onConfigurationChanged (config) {
239
+ pluginModules.forEach((module)=>module.onConfigurationChanged?.(config));
240
+ }
241
+ };
242
+ };
243
+
244
+ module.exports = pluginModuleFactory;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import ts__default from 'typescript';
1
+ import ts from 'typescript';
2
2
 
3
- declare const init: ts__default.server.PluginModuleFactory;
3
+ declare const pluginModuleFactory: ts.server.PluginModuleFactory;
4
4
 
5
- export { init as default };
5
+ export { pluginModuleFactory as default };
package/dist/index.js CHANGED
@@ -1,8 +1,11 @@
1
1
  import { getSchema } from '@mo36924/graphql';
2
2
  import { GraphQLError } from 'graphql';
3
3
  import { getDiagnostics, DIAGNOSTIC_SEVERITY, Position, getAutocompleteSuggestions, CompletionItemKind, getTokenAtPosition, getHoverInformation } from 'graphql-language-service';
4
+ import { volar } from './volar.js';
5
+ import '@volar/typescript/lib/quickstart/createLanguageServicePlugin';
6
+ import 'typescript';
4
7
 
5
- const init = ({ typescript: ts })=>{
8
+ const graphql = ({ typescript: ts })=>{
6
9
  const isQueryTag = (node)=>ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && node.tag.escapedText === "gql";
7
10
  const getScriptElementKind = (completionItemKind)=>{
8
11
  switch(completionItemKind){
@@ -65,9 +68,6 @@ const init = ({ typescript: ts })=>{
65
68
  create (info) {
66
69
  const { schema } = getSchema(info.project.getCurrentDirectory());
67
70
  const languageService = info.languageService;
68
- if (!schema) {
69
- return languageService;
70
- }
71
71
  const getSourceFile = (fileName)=>languageService.getProgram()?.getSourceFile(fileName);
72
72
  const normalizeQuery = (node)=>{
73
73
  const template = node.template;
@@ -218,4 +218,25 @@ const init = ({ typescript: ts })=>{
218
218
  };
219
219
  };
220
220
 
221
- export { init as default };
221
+ const pluginModuleFactory = (mod)=>{
222
+ const pluginModules = [
223
+ graphql(mod),
224
+ volar(mod)
225
+ ];
226
+ return {
227
+ create (createInfo) {
228
+ return pluginModules.reduce((languageService, module)=>module.create({
229
+ ...createInfo,
230
+ languageService
231
+ }), createInfo.languageService);
232
+ },
233
+ getExternalFiles (proj, updateLevel) {
234
+ return pluginModules.flatMap((module)=>module.getExternalFiles?.(proj, updateLevel) ?? []);
235
+ },
236
+ onConfigurationChanged (config) {
237
+ pluginModules.forEach((module)=>module.onConfigurationChanged?.(config));
238
+ }
239
+ };
240
+ };
241
+
242
+ export { pluginModuleFactory as default };
package/dist/volar.cjs ADDED
@@ -0,0 +1,152 @@
1
+ 'use strict';
2
+
3
+ var graphql = require('@mo36924/graphql');
4
+ var createLanguageServicePlugin = require('@volar/typescript/lib/quickstart/createLanguageServicePlugin');
5
+ var ts = require('typescript');
6
+
7
+ /// <reference types="@volar/typescript" />
8
+ const tag = "gql";
9
+ const data = {
10
+ completion: true,
11
+ format: false,
12
+ navigation: true,
13
+ semantic: true,
14
+ structure: true,
15
+ verification: true
16
+ };
17
+ const getText = (snapshot)=>snapshot.getText(0, snapshot.getLength());
18
+ const getCodeMappings = (text)=>[
19
+ {
20
+ sourceOffsets: [
21
+ 0
22
+ ],
23
+ generatedOffsets: [
24
+ 0
25
+ ],
26
+ lengths: [
27
+ text.length
28
+ ],
29
+ data
30
+ }
31
+ ];
32
+ const getLanguagePlugin = (cwd)=>{
33
+ const { schema } = graphql.getSchema(cwd);
34
+ const transform = (sourceFile)=>{
35
+ const code = sourceFile.text;
36
+ const sourceOffsets = [
37
+ 0
38
+ ];
39
+ const generatedOffsets = [
40
+ 0
41
+ ];
42
+ const lengths = [];
43
+ let offset = 0;
44
+ let generatedCode = "";
45
+ ts.forEachChild(sourceFile, function cb(node) {
46
+ if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && node.tag.text === tag && !node.typeArguments) {
47
+ const typeArguments = graphql.getGqlTypeArguments(schema, node);
48
+ if (typeArguments) {
49
+ const end = node.tag.end;
50
+ generatedCode += code.substring(offset, end) + typeArguments;
51
+ sourceOffsets.push(end);
52
+ generatedOffsets.push(generatedCode.length);
53
+ lengths.push(end - offset);
54
+ offset = end;
55
+ }
56
+ }
57
+ ts.forEachChild(node, cb);
58
+ });
59
+ generatedCode += code.substring(offset);
60
+ lengths.push(code.length - offset);
61
+ const snapshot = ts.ScriptSnapshot.fromString(generatedCode);
62
+ return {
63
+ snapshot,
64
+ mappings: [
65
+ {
66
+ sourceOffsets,
67
+ generatedOffsets,
68
+ lengths,
69
+ data
70
+ }
71
+ ]
72
+ };
73
+ };
74
+ const languagePlugin = {
75
+ getLanguageId () {
76
+ return undefined;
77
+ },
78
+ createVirtualCode (scriptId, languageId, snapshot) {
79
+ if (languageId !== "typescript" && languageId !== "typescriptreact" || scriptId.includes("node_modules") || scriptId.endsWith(".d.ts")) {
80
+ return;
81
+ }
82
+ const text = getText(snapshot);
83
+ if (!text.includes("gql")) {
84
+ return {
85
+ id: scriptId,
86
+ languageId,
87
+ snapshot,
88
+ mappings: getCodeMappings(text)
89
+ };
90
+ }
91
+ const sourceFile = ts.createSourceFile(scriptId, text, 99);
92
+ const result = transform(sourceFile);
93
+ return {
94
+ id: scriptId,
95
+ languageId,
96
+ ...result,
97
+ source: {
98
+ sourceFile,
99
+ snapshot
100
+ }
101
+ };
102
+ },
103
+ updateVirtualCode (scriptId, virtualCode, snapshot) {
104
+ const text = getText(snapshot);
105
+ if (!text.includes("gql")) {
106
+ return {
107
+ ...virtualCode,
108
+ snapshot,
109
+ mappings: getCodeMappings(text),
110
+ source: undefined
111
+ };
112
+ }
113
+ let sourceFile;
114
+ if (virtualCode.source) {
115
+ const change = snapshot.getChangeRange(virtualCode.source.snapshot);
116
+ if (change) {
117
+ sourceFile = virtualCode.source.sourceFile.update(text, change);
118
+ }
119
+ }
120
+ sourceFile ?? (sourceFile = ts.createSourceFile(scriptId, text, 99));
121
+ const result = transform(sourceFile);
122
+ return {
123
+ ...virtualCode,
124
+ ...result,
125
+ source: {
126
+ sourceFile,
127
+ snapshot
128
+ }
129
+ };
130
+ },
131
+ typescript: {
132
+ extraFileExtensions: [],
133
+ getServiceScript (code) {
134
+ const isTs = code.languageId === "typescript";
135
+ return {
136
+ code,
137
+ extension: isTs ? ".ts" : ".tsx",
138
+ scriptKind: isTs ? ts.ScriptKind.TS : ts.ScriptKind.TSX
139
+ };
140
+ }
141
+ }
142
+ };
143
+ return languagePlugin;
144
+ };
145
+ const volar = createLanguageServicePlugin.createLanguageServicePlugin((_ts, info)=>({
146
+ languagePlugins: [
147
+ getLanguagePlugin(info.project.getCurrentDirectory())
148
+ ]
149
+ }));
150
+
151
+ exports.getLanguagePlugin = getLanguagePlugin;
152
+ exports.volar = volar;
@@ -0,0 +1,12 @@
1
+ import { LanguagePlugin, VirtualCode } from '@volar/language-core';
2
+ import ts from 'typescript';
3
+
4
+ declare const getLanguagePlugin: (cwd?: string) => LanguagePlugin<string, VirtualCode & {
5
+ source?: {
6
+ sourceFile: ts.SourceFile;
7
+ snapshot: ts.IScriptSnapshot;
8
+ };
9
+ }>;
10
+ declare const volar: ts.server.PluginModuleFactory;
11
+
12
+ export { getLanguagePlugin, volar };
package/dist/volar.js ADDED
@@ -0,0 +1,149 @@
1
+ import { getSchema, getGqlTypeArguments } from '@mo36924/graphql';
2
+ import { createLanguageServicePlugin } from '@volar/typescript/lib/quickstart/createLanguageServicePlugin';
3
+ import ts from 'typescript';
4
+
5
+ /// <reference types="@volar/typescript" />
6
+ const tag = "gql";
7
+ const data = {
8
+ completion: true,
9
+ format: false,
10
+ navigation: true,
11
+ semantic: true,
12
+ structure: true,
13
+ verification: true
14
+ };
15
+ const getText = (snapshot)=>snapshot.getText(0, snapshot.getLength());
16
+ const getCodeMappings = (text)=>[
17
+ {
18
+ sourceOffsets: [
19
+ 0
20
+ ],
21
+ generatedOffsets: [
22
+ 0
23
+ ],
24
+ lengths: [
25
+ text.length
26
+ ],
27
+ data
28
+ }
29
+ ];
30
+ const getLanguagePlugin = (cwd)=>{
31
+ const { schema } = getSchema(cwd);
32
+ const transform = (sourceFile)=>{
33
+ const code = sourceFile.text;
34
+ const sourceOffsets = [
35
+ 0
36
+ ];
37
+ const generatedOffsets = [
38
+ 0
39
+ ];
40
+ const lengths = [];
41
+ let offset = 0;
42
+ let generatedCode = "";
43
+ ts.forEachChild(sourceFile, function cb(node) {
44
+ if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && node.tag.text === tag && !node.typeArguments) {
45
+ const typeArguments = getGqlTypeArguments(schema, node);
46
+ if (typeArguments) {
47
+ const end = node.tag.end;
48
+ generatedCode += code.substring(offset, end) + typeArguments;
49
+ sourceOffsets.push(end);
50
+ generatedOffsets.push(generatedCode.length);
51
+ lengths.push(end - offset);
52
+ offset = end;
53
+ }
54
+ }
55
+ ts.forEachChild(node, cb);
56
+ });
57
+ generatedCode += code.substring(offset);
58
+ lengths.push(code.length - offset);
59
+ const snapshot = ts.ScriptSnapshot.fromString(generatedCode);
60
+ return {
61
+ snapshot,
62
+ mappings: [
63
+ {
64
+ sourceOffsets,
65
+ generatedOffsets,
66
+ lengths,
67
+ data
68
+ }
69
+ ]
70
+ };
71
+ };
72
+ const languagePlugin = {
73
+ getLanguageId () {
74
+ return undefined;
75
+ },
76
+ createVirtualCode (scriptId, languageId, snapshot) {
77
+ if (languageId !== "typescript" && languageId !== "typescriptreact" || scriptId.includes("node_modules") || scriptId.endsWith(".d.ts")) {
78
+ return;
79
+ }
80
+ const text = getText(snapshot);
81
+ if (!text.includes("gql")) {
82
+ return {
83
+ id: scriptId,
84
+ languageId,
85
+ snapshot,
86
+ mappings: getCodeMappings(text)
87
+ };
88
+ }
89
+ const sourceFile = ts.createSourceFile(scriptId, text, 99);
90
+ const result = transform(sourceFile);
91
+ return {
92
+ id: scriptId,
93
+ languageId,
94
+ ...result,
95
+ source: {
96
+ sourceFile,
97
+ snapshot
98
+ }
99
+ };
100
+ },
101
+ updateVirtualCode (scriptId, virtualCode, snapshot) {
102
+ const text = getText(snapshot);
103
+ if (!text.includes("gql")) {
104
+ return {
105
+ ...virtualCode,
106
+ snapshot,
107
+ mappings: getCodeMappings(text),
108
+ source: undefined
109
+ };
110
+ }
111
+ let sourceFile;
112
+ if (virtualCode.source) {
113
+ const change = snapshot.getChangeRange(virtualCode.source.snapshot);
114
+ if (change) {
115
+ sourceFile = virtualCode.source.sourceFile.update(text, change);
116
+ }
117
+ }
118
+ sourceFile ?? (sourceFile = ts.createSourceFile(scriptId, text, 99));
119
+ const result = transform(sourceFile);
120
+ return {
121
+ ...virtualCode,
122
+ ...result,
123
+ source: {
124
+ sourceFile,
125
+ snapshot
126
+ }
127
+ };
128
+ },
129
+ typescript: {
130
+ extraFileExtensions: [],
131
+ getServiceScript (code) {
132
+ const isTs = code.languageId === "typescript";
133
+ return {
134
+ code,
135
+ extension: isTs ? ".ts" : ".tsx",
136
+ scriptKind: isTs ? ts.ScriptKind.TS : ts.ScriptKind.TSX
137
+ };
138
+ }
139
+ }
140
+ };
141
+ return languagePlugin;
142
+ };
143
+ const volar = createLanguageServicePlugin((_ts, info)=>({
144
+ languagePlugins: [
145
+ getLanguagePlugin(info.project.getCurrentDirectory())
146
+ ]
147
+ }));
148
+
149
+ export { getLanguagePlugin, volar };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mo36924/graphql-plugin",
3
3
  "type": "module",
4
- "version": "5.4.0",
4
+ "version": "5.4.4",
5
5
  "description": "graphql-plugin",
6
6
  "author": "mo36924",
7
7
  "license": "MIT",
@@ -21,6 +21,12 @@
21
21
  "import": "./dist/index.js",
22
22
  "require": "./dist/index.cjs",
23
23
  "default": "./dist/index.js"
24
+ },
25
+ "./volar": {
26
+ "types": "./dist/volar.d.ts",
27
+ "import": "./dist/volar.js",
28
+ "require": "./dist/volar.cjs",
29
+ "default": "./dist/volar.js"
24
30
  }
25
31
  },
26
32
  "main": "./dist/index.cjs",
@@ -41,11 +47,12 @@
41
47
  "dist"
42
48
  ],
43
49
  "dependencies": {
44
- "@mo36924/graphql": "^5.4.0",
50
+ "@mo36924/graphql": "^5.4.4",
51
+ "@volar/typescript": "^2.4.10",
45
52
  "graphql": "^16.9.0",
46
53
  "graphql-language-service": "^5.3.0",
47
54
  "typescript": "^5.6.3",
48
55
  "vscode-languageserver-types": "^3.17.5"
49
56
  },
50
- "gitHead": "f1f8c8063a21b7aed3f6c29f6d574420abc25324"
57
+ "gitHead": "d441e61abb10c23ade3abb7217a9044e15fe767c"
51
58
  }