@mo36924/graphql-plugin 5.4.0 → 5.4.2

Sign up to get free protection for your applications and to get access to all the features.
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,7 +68,7 @@ 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
73
  if (!schema) {
71
74
  return languageService;
@@ -198,7 +201,7 @@ const init = ({ typescript: ts })=>{
198
201
  });
199
202
  }
200
203
  } catch (error) {
201
- if (error instanceof graphql$1.GraphQLError) {
204
+ if (error instanceof graphql$2.GraphQLError) {
202
205
  diagnostics.push({
203
206
  category: ts.DiagnosticCategory.Error,
204
207
  code: 9999,
@@ -220,4 +223,25 @@ const init = ({ typescript: ts })=>{
220
223
  };
221
224
  };
222
225
 
223
- module.exports = init;
226
+ const pluginModuleFactory = (mod)=>{
227
+ const pluginModules = [
228
+ graphql(mod),
229
+ packages_graphqlPlugin_dist_volar.volar(mod)
230
+ ];
231
+ return {
232
+ create (createInfo) {
233
+ return pluginModules.reduce((languageService, module)=>module.create({
234
+ ...createInfo,
235
+ languageService
236
+ }), createInfo.languageService);
237
+ },
238
+ getExternalFiles (proj, updateLevel) {
239
+ return pluginModules.flatMap((module)=>module.getExternalFiles?.(proj, updateLevel) ?? []);
240
+ },
241
+ onConfigurationChanged (config) {
242
+ pluginModules.forEach((module)=>module.onConfigurationChanged?.(config));
243
+ }
244
+ };
245
+ };
246
+
247
+ 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){
@@ -218,4 +221,25 @@ const init = ({ typescript: ts })=>{
218
221
  };
219
222
  };
220
223
 
221
- export { init as default };
224
+ const pluginModuleFactory = (mod)=>{
225
+ const pluginModules = [
226
+ graphql(mod),
227
+ volar(mod)
228
+ ];
229
+ return {
230
+ create (createInfo) {
231
+ return pluginModules.reduce((languageService, module)=>module.create({
232
+ ...createInfo,
233
+ languageService
234
+ }), createInfo.languageService);
235
+ },
236
+ getExternalFiles (proj, updateLevel) {
237
+ return pluginModules.flatMap((module)=>module.getExternalFiles?.(proj, updateLevel) ?? []);
238
+ },
239
+ onConfigurationChanged (config) {
240
+ pluginModules.forEach((module)=>module.onConfigurationChanged?.(config));
241
+ }
242
+ };
243
+ };
244
+
245
+ export { pluginModuleFactory as default };
package/dist/volar.cjs ADDED
@@ -0,0 +1,154 @@
1
+ 'use strict';
2
+
3
+ var graphql = require('@mo36924/graphql');
4
+ var createLanguageServicePlugin = require('@volar/typescript/lib/quickstart/createLanguageServicePlugin');
5
+ var graphql$1 = require('graphql');
6
+ var ts = require('typescript');
7
+
8
+ /// <reference types="@volar/typescript" />
9
+ const tag = "gql";
10
+ const data = {
11
+ completion: true,
12
+ format: false,
13
+ navigation: true,
14
+ semantic: true,
15
+ structure: true,
16
+ verification: true
17
+ };
18
+ const getText = (snapshot)=>snapshot.getText(0, snapshot.getLength());
19
+ const getCodeMappings = (text)=>[
20
+ {
21
+ sourceOffsets: [
22
+ 0
23
+ ],
24
+ generatedOffsets: [
25
+ 0
26
+ ],
27
+ lengths: [
28
+ text.length
29
+ ],
30
+ data
31
+ }
32
+ ];
33
+ const getLanguagePlugin = (cwd)=>{
34
+ let { schema } = graphql.getSchema(cwd);
35
+ schema ?? (schema = graphql$1.buildSchema("scalar _"));
36
+ const transform = (sourceFile)=>{
37
+ const code = sourceFile.text;
38
+ const sourceOffsets = [
39
+ 0
40
+ ];
41
+ const generatedOffsets = [
42
+ 0
43
+ ];
44
+ const lengths = [];
45
+ let offset = 0;
46
+ let generatedCode = "";
47
+ ts.forEachChild(sourceFile, function cb(node) {
48
+ if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && node.tag.text === tag && !node.typeArguments) {
49
+ const typeArguments = graphql.getGqlTypeArguments(schema, node);
50
+ if (typeArguments) {
51
+ const end = node.tag.end;
52
+ generatedCode += code.substring(offset, end) + typeArguments;
53
+ sourceOffsets.push(end);
54
+ generatedOffsets.push(generatedCode.length);
55
+ lengths.push(end - offset);
56
+ offset = end;
57
+ }
58
+ }
59
+ ts.forEachChild(node, cb);
60
+ });
61
+ generatedCode += code.substring(offset);
62
+ lengths.push(code.length - offset);
63
+ const snapshot = ts.ScriptSnapshot.fromString(generatedCode);
64
+ return {
65
+ snapshot,
66
+ mappings: [
67
+ {
68
+ sourceOffsets,
69
+ generatedOffsets,
70
+ lengths,
71
+ data
72
+ }
73
+ ]
74
+ };
75
+ };
76
+ const languagePlugin = {
77
+ getLanguageId () {
78
+ return undefined;
79
+ },
80
+ createVirtualCode (scriptId, languageId, snapshot) {
81
+ if (languageId !== "typescript" && languageId !== "typescriptreact" || scriptId.includes("node_modules") || scriptId.endsWith(".d.ts")) {
82
+ return;
83
+ }
84
+ const text = getText(snapshot);
85
+ if (!text.includes("gql")) {
86
+ return {
87
+ id: scriptId,
88
+ languageId,
89
+ snapshot,
90
+ mappings: getCodeMappings(text)
91
+ };
92
+ }
93
+ const sourceFile = ts.createSourceFile(scriptId, text, 99);
94
+ const result = transform(sourceFile);
95
+ return {
96
+ id: scriptId,
97
+ languageId,
98
+ ...result,
99
+ source: {
100
+ sourceFile,
101
+ snapshot
102
+ }
103
+ };
104
+ },
105
+ updateVirtualCode (scriptId, virtualCode, snapshot) {
106
+ const text = getText(snapshot);
107
+ if (!text.includes("gql")) {
108
+ return {
109
+ ...virtualCode,
110
+ snapshot,
111
+ mappings: getCodeMappings(text),
112
+ source: undefined
113
+ };
114
+ }
115
+ let sourceFile;
116
+ if (virtualCode.source) {
117
+ const change = snapshot.getChangeRange(virtualCode.source.snapshot);
118
+ if (change) {
119
+ sourceFile = virtualCode.source.sourceFile.update(text, change);
120
+ }
121
+ }
122
+ sourceFile ?? (sourceFile = ts.createSourceFile(scriptId, text, 99));
123
+ const result = transform(sourceFile);
124
+ return {
125
+ ...virtualCode,
126
+ ...result,
127
+ source: {
128
+ sourceFile,
129
+ snapshot
130
+ }
131
+ };
132
+ },
133
+ typescript: {
134
+ extraFileExtensions: [],
135
+ getServiceScript (code) {
136
+ const isTs = code.languageId === "typescript";
137
+ return {
138
+ code,
139
+ extension: isTs ? ".ts" : ".tsx",
140
+ scriptKind: isTs ? ts.ScriptKind.TS : ts.ScriptKind.TSX
141
+ };
142
+ }
143
+ }
144
+ };
145
+ return languagePlugin;
146
+ };
147
+ const volar = createLanguageServicePlugin.createLanguageServicePlugin((_ts, info)=>({
148
+ languagePlugins: [
149
+ getLanguagePlugin(info.project.getCurrentDirectory())
150
+ ]
151
+ }));
152
+
153
+ exports.getLanguagePlugin = getLanguagePlugin;
154
+ 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,151 @@
1
+ import { getSchema, getGqlTypeArguments } from '@mo36924/graphql';
2
+ import { createLanguageServicePlugin } from '@volar/typescript/lib/quickstart/createLanguageServicePlugin';
3
+ import { buildSchema } from 'graphql';
4
+ import ts from 'typescript';
5
+
6
+ /// <reference types="@volar/typescript" />
7
+ const tag = "gql";
8
+ const data = {
9
+ completion: true,
10
+ format: false,
11
+ navigation: true,
12
+ semantic: true,
13
+ structure: true,
14
+ verification: true
15
+ };
16
+ const getText = (snapshot)=>snapshot.getText(0, snapshot.getLength());
17
+ const getCodeMappings = (text)=>[
18
+ {
19
+ sourceOffsets: [
20
+ 0
21
+ ],
22
+ generatedOffsets: [
23
+ 0
24
+ ],
25
+ lengths: [
26
+ text.length
27
+ ],
28
+ data
29
+ }
30
+ ];
31
+ const getLanguagePlugin = (cwd)=>{
32
+ let { schema } = getSchema(cwd);
33
+ schema ?? (schema = buildSchema("scalar _"));
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 = 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((_ts, info)=>({
146
+ languagePlugins: [
147
+ getLanguagePlugin(info.project.getCurrentDirectory())
148
+ ]
149
+ }));
150
+
151
+ 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.2",
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.2",
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": "57e62171edc5313ea100277a08cca72130ebca42"
51
58
  }