@golar/volar 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 auvred <https://github.com/auvred>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,18 @@
1
+ import { Promisable, ServiceCodeError } from "@golar/plugin";
2
+ import { LanguagePlugin, VirtualCode } from "@volar/language-core";
3
+
4
+ //#region src/index.d.ts
5
+ type VolarLanguagePlugin = LanguagePlugin<string> & {
6
+ getVirtualCodeErrors?(root: VirtualCode): ServiceCodeError[];
7
+ };
8
+ type CreateVolarPluginOptions = {
9
+ filename: string;
10
+ /**
11
+ * @example ['.vue']
12
+ */
13
+ extraFileExtensions: string[];
14
+ languagePlugins: VolarLanguagePlugin[] | ((cwd: string, configFileName: string | null) => Promisable<VolarLanguagePlugin[]>);
15
+ };
16
+ declare function createVolarPlugin(opts: CreateVolarPluginOptions): void;
17
+ //#endregion
18
+ export { CreateVolarPluginOptions, VolarLanguagePlugin, createVolarPlugin };
package/dist/index.js ADDED
@@ -0,0 +1,125 @@
1
+ import { createPlugin } from "@golar/plugin";
2
+
3
+ //#region src/index.ts
4
+ function createVolarPlugin(opts) {
5
+ const languagePluginsByProject = /* @__PURE__ */ new Map();
6
+ createPlugin({
7
+ filename: opts.filename,
8
+ extraExtensions: opts.extraFileExtensions,
9
+ async createServiceCode(cwd, configFileName, fileName, sourceText) {
10
+ let languagePlugins;
11
+ if (Array.isArray(opts.languagePlugins)) languagePlugins = opts.languagePlugins;
12
+ else {
13
+ const project = `${cwd}::${configFileName}`;
14
+ let plugins = languagePluginsByProject.get(project);
15
+ if (plugins == null) languagePluginsByProject.set(project, plugins = opts.languagePlugins(cwd, configFileName));
16
+ languagePlugins = await plugins;
17
+ }
18
+ for (const plugin of languagePlugins) {
19
+ if (plugin.createVirtualCode == null) continue;
20
+ const languageId = plugin.getLanguageId(fileName);
21
+ if (languageId == null) continue;
22
+ const virtualCode = plugin.createVirtualCode(fileName, languageId, {
23
+ getLength() {
24
+ return sourceText.length;
25
+ },
26
+ getText(start, end) {
27
+ return sourceText.slice(start, end);
28
+ },
29
+ dispose() {},
30
+ getChangeRange() {}
31
+ }, { getAssociatedScript(scriptId) {} });
32
+ if (virtualCode == null) continue;
33
+ {
34
+ const errors = plugin.getVirtualCodeErrors?.(virtualCode);
35
+ if (errors?.length) return { errors };
36
+ }
37
+ const serviceScript = plugin.typescript.getServiceScript(virtualCode);
38
+ const serviceText = serviceScript.code.snapshot.getText(0, serviceScript.code.snapshot.getLength());
39
+ const verificationMappings = serviceScript.code.mappings.filter((m) => m.data.verification);
40
+ const sourceOffsets = /* @__PURE__ */ new Set();
41
+ const serviceOffsets = /* @__PURE__ */ new Set();
42
+ for (const m of verificationMappings) {
43
+ for (const [i, offset] of m.sourceOffsets.entries()) {
44
+ sourceOffsets.add(offset);
45
+ sourceOffsets.add(offset + m.lengths[i]);
46
+ }
47
+ for (const [i, offset] of m.generatedOffsets.entries()) {
48
+ serviceOffsets.add(offset);
49
+ serviceOffsets.add(offset + (m.generatedLengths ?? m.lengths)[i]);
50
+ }
51
+ if ("__expectErrorCommentLoc" in m.data) {
52
+ const [start, end] = m.data.__expectErrorCommentLoc;
53
+ sourceOffsets.add(start).add(end);
54
+ }
55
+ }
56
+ const serviceCovered = [];
57
+ const expectErrorMappings = [];
58
+ const mappings = verificationMappings.flatMap((m) => {
59
+ return m.sourceOffsets.map((sourceOffset, i) => {
60
+ const generatedOffset = m.generatedOffsets[i];
61
+ const sourceLength = m.lengths[i];
62
+ const generatedLength = m.generatedLengths?.[i] ?? sourceLength;
63
+ if (generatedLength > 0) serviceCovered.push([generatedOffset, generatedOffset + generatedLength]);
64
+ if ("__expectErrorCommentLoc" in m.data) {
65
+ const [start, end] = m.data.__expectErrorCommentLoc;
66
+ expectErrorMappings.push({
67
+ sourceOffset: start,
68
+ serviceOffset: generatedOffset,
69
+ sourceLength: end - start,
70
+ serviceLength: generatedLength
71
+ });
72
+ }
73
+ return {
74
+ sourceOffset,
75
+ serviceOffset: generatedOffset,
76
+ sourceLength,
77
+ serviceLength: generatedLength
78
+ };
79
+ });
80
+ });
81
+ serviceCovered.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
82
+ const merged = [];
83
+ for (const [s, e] of serviceCovered) {
84
+ const last = merged.at(-1);
85
+ if (!last) merged.push([s, e]);
86
+ else if (s <= last[1]) last[1] = Math.max(last[1], e);
87
+ else merged.push([s, e]);
88
+ }
89
+ const ignoreMappings = [];
90
+ let cursor = 0;
91
+ for (const [s, e] of merged) {
92
+ if (s > cursor) ignoreMappings.push({
93
+ serviceOffset: cursor,
94
+ serviceLength: s - cursor
95
+ });
96
+ cursor = Math.max(cursor, e);
97
+ }
98
+ ignoreMappings.push({
99
+ serviceOffset: cursor,
100
+ serviceLength: serviceText.length - cursor
101
+ });
102
+ return {
103
+ serviceText,
104
+ scriptKind: tsScriptKindToGolar(serviceScript?.scriptKind),
105
+ mappings,
106
+ ignoreMappings,
107
+ expectErrorMappings
108
+ };
109
+ }
110
+ throw new Error("Unknown language");
111
+ }
112
+ });
113
+ }
114
+ function tsScriptKindToGolar(scriptKind) {
115
+ switch (scriptKind) {
116
+ case 1: return "js";
117
+ case 2: return "jsx";
118
+ case 3: return "ts";
119
+ case 4: return "tsx";
120
+ default: return "ts";
121
+ }
122
+ }
123
+
124
+ //#endregion
125
+ export { createVolarPlugin };
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@golar/volar",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": "./dist/index.js"
7
+ },
8
+ "files": [
9
+ "./dist"
10
+ ],
11
+ "license": "MIT",
12
+ "author": "auvred <aauvred@gmail.com> (https://github.com/auvred)",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/auvred/golar.git",
16
+ "directory": "packages/volar"
17
+ },
18
+ "bugs": "https://github.com/auvred/golar/issues",
19
+ "homepage": "https://github.com/auvred/golar/tree/main/packages/volar#readme",
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "dependencies": {
24
+ "@volar/language-core": "^2.4.28",
25
+ "@golar/plugin": "0.0.1"
26
+ },
27
+ "devDependencies": {
28
+ "@volar/typescript": "^2.4.28"
29
+ }
30
+ }