@mdsvex/language-core 0.1.0-next.0 → 0.1.0-next.2

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.
@@ -1,183 +0,0 @@
1
- /**
2
- * Minimal Svelte LanguagePlugin.
3
- *
4
- * Runs svelte2tsx on .svelte files to produce TypeScript VirtualCode,
5
- * enabling component prop types to resolve when imported from .pfm files.
6
- *
7
- * This is NOT a full Svelte language server, it only provides enough
8
- * for cross-file type resolution.
9
- */
10
-
11
- import type {
12
- LanguagePlugin,
13
- VirtualCode,
14
- IScriptSnapshot,
15
- CodeMapping,
16
- CodegenContext,
17
- } from "@volar/language-core";
18
- import { forEachEmbeddedCode } from "@volar/language-core";
19
- import type { TypeScriptExtraServiceScript } from "@volar/typescript";
20
-
21
- import { svelte2tsx } from "svelte2tsx";
22
- import { v3ToVolarMappings } from "@mdsvex/source-map/v3-to-volar";
23
-
24
- const SVELTE_EXTENSION = ".svelte";
25
-
26
- import { SVELTE_SHIMS } from "./_svelte_shims";
27
-
28
- /** Create a simple IScriptSnapshot from a string. */
29
- function createSnapshot(text: string): IScriptSnapshot {
30
- return {
31
- getText: (start, end) => text.slice(start, end),
32
- getLength: () => text.length,
33
- getChangeRange: () => undefined,
34
- };
35
- }
36
-
37
- function createSvelteVirtualCode(
38
- source: string,
39
- scriptId: string,
40
- ): VirtualCode | undefined {
41
- let tsx;
42
- try {
43
- tsx = svelte2tsx(source, {
44
- filename: scriptId,
45
- isTsFile: false,
46
- mode: "ts",
47
- });
48
- } catch {
49
- return undefined;
50
- }
51
-
52
- const v3Map = tsx.map.toJSON ? tsx.map.toJSON() : tsx.map;
53
-
54
- // Prepend svelte2tsx shims so TypeScript can resolve helper types
55
- const prefix = SVELTE_SHIMS ? SVELTE_SHIMS + "\n" : "";
56
- const tsCode = prefix + tsx.code;
57
- const prefixLen = prefix.length;
58
-
59
- // Build mappings from original tsx output, then shift generated offsets
60
- const rawMappings = v3ToVolarMappings(v3Map as any, tsx.code, source);
61
- const mappings = rawMappings.map((m) => ({
62
- ...m,
63
- generatedOffsets: m.generatedOffsets.map((o) => o + prefixLen),
64
- }));
65
-
66
- const tsVirtualCode: VirtualCode = {
67
- id: "ts",
68
- languageId: "typescript",
69
- snapshot: createSnapshot(tsCode),
70
- mappings: mappings as CodeMapping[],
71
- embeddedCodes: [],
72
- };
73
-
74
- return {
75
- id: "root",
76
- languageId: "svelte",
77
- snapshot: createSnapshot(source),
78
- mappings: [{
79
- sourceOffsets: [0],
80
- generatedOffsets: [0],
81
- lengths: [source.length],
82
- data: { structure: true },
83
- }],
84
- embeddedCodes: [tsVirtualCode],
85
- };
86
- }
87
-
88
- /**
89
- * Create a minimal Svelte LanguagePlugin for cross-file type resolution.
90
- */
91
- export function create_svelte_language_plugin(): LanguagePlugin<string> {
92
- const cache = new Map<string, { hash: number; result: VirtualCode }>();
93
-
94
- function hashString(s: string): number {
95
- let h = 0;
96
- for (let i = 0; i < s.length; i++) {
97
- h = ((h << 5) - h + s.charCodeAt(i)) | 0;
98
- }
99
- return h;
100
- }
101
-
102
- return {
103
- getLanguageId(scriptId: string): string | undefined {
104
- if (scriptId.endsWith(SVELTE_EXTENSION)) {
105
- return "svelte";
106
- }
107
- return undefined;
108
- },
109
-
110
- createVirtualCode(
111
- scriptId: string,
112
- languageId: string,
113
- snapshot: IScriptSnapshot,
114
- ): VirtualCode | undefined {
115
- if (languageId !== "svelte") return undefined;
116
-
117
- const source = snapshot.getText(0, snapshot.getLength());
118
- const hash = hashString(source);
119
- const cached = cache.get(scriptId);
120
- if (cached && cached.hash === hash) return cached.result;
121
-
122
- const result = createSvelteVirtualCode(source, scriptId);
123
- if (result) cache.set(scriptId, { hash, result });
124
- return result;
125
- },
126
-
127
- updateVirtualCode(
128
- scriptId: string,
129
- _virtualCode: VirtualCode,
130
- newSnapshot: IScriptSnapshot,
131
- ): VirtualCode | undefined {
132
- const source = newSnapshot.getText(0, newSnapshot.getLength());
133
- const hash = hashString(source);
134
- const cached = cache.get(scriptId);
135
- if (cached && cached.hash === hash) return cached.result;
136
-
137
- const result = createSvelteVirtualCode(source, scriptId);
138
- if (result) cache.set(scriptId, { hash, result });
139
- return result;
140
- },
141
-
142
- disposeVirtualCode(scriptId: string) {
143
- cache.delete(scriptId);
144
- },
145
-
146
- typescript: {
147
- extraFileExtensions: [
148
- {
149
- extension: "svelte",
150
- isMixedContent: true,
151
- scriptKind: 7 as any, // ts.ScriptKind.Deferred
152
- },
153
- ],
154
- getServiceScript(root: VirtualCode) {
155
- const tsCode = root.embeddedCodes?.find(
156
- (c) => c.languageId === "typescript",
157
- );
158
- if (tsCode) {
159
- return {
160
- code: tsCode,
161
- extension: ".ts" as any,
162
- scriptKind: 3 as any, // ts.ScriptKind.TS
163
- };
164
- }
165
- return undefined;
166
- },
167
- getExtraServiceScripts(fileName: string, root: VirtualCode) {
168
- const scripts: TypeScriptExtraServiceScript[] = [];
169
- for (const code of forEachEmbeddedCode(root)) {
170
- if (code.languageId === "typescript") {
171
- scripts.push({
172
- fileName: fileName + "." + code.id + ".ts",
173
- code,
174
- extension: ".ts",
175
- scriptKind: 3 as any,
176
- });
177
- }
178
- }
179
- return scripts;
180
- },
181
- },
182
- };
183
- }
@@ -1,177 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import { create_pfm_language_plugin } from "../src/language_plugin";
3
- import type { IScriptSnapshot, CodegenContext, VirtualCode, CodeMapping } from "@volar/language-core";
4
-
5
- function snap(text: string): IScriptSnapshot {
6
- return {
7
- getText: (s, e) => text.slice(s, e),
8
- getLength: () => text.length,
9
- getChangeRange: () => undefined,
10
- };
11
- }
12
-
13
- const dummyCtx: CodegenContext<string> = {
14
- getAssociatedScript: () => undefined,
15
- };
16
-
17
- describe("create_pfm_language_plugin", () => {
18
- const plugin = create_pfm_language_plugin();
19
-
20
- it("getLanguageId returns 'pfm' for .pfm files", () => {
21
- expect(plugin.getLanguageId("file.pfm")).toBe("pfm");
22
- expect(plugin.getLanguageId("/path/to/doc.pfm")).toBe("pfm");
23
- });
24
-
25
- it("getLanguageId returns undefined for non-.pfm files", () => {
26
- expect(plugin.getLanguageId("file.md")).toBeUndefined();
27
- expect(plugin.getLanguageId("file.svelte")).toBeUndefined();
28
- expect(plugin.getLanguageId("file.ts")).toBeUndefined();
29
- });
30
-
31
- it("createVirtualCode returns undefined for non-pfm languageId", () => {
32
- const result = plugin.createVirtualCode!(
33
- "test.pfm",
34
- "markdown",
35
- snap("# Hello"),
36
- dummyCtx,
37
- );
38
- expect(result).toBeUndefined();
39
- });
40
-
41
- it("createVirtualCode produces valid VirtualCode for simple markdown", () => {
42
- const vc = plugin.createVirtualCode!(
43
- "test.pfm",
44
- "pfm",
45
- snap("# Hello\n\nWorld\n"),
46
- dummyCtx,
47
- );
48
-
49
- expect(vc).toBeDefined();
50
- expect(vc!.id).toBe("root");
51
- expect(vc!.languageId).toBe("pfm");
52
- expect(vc!.mappings.length).toBeGreaterThan(0);
53
- expect(vc!.embeddedCodes).toBeDefined();
54
- });
55
-
56
- it("produces TypeScript embedded VirtualCode", () => {
57
- const vc = plugin.createVirtualCode!(
58
- "test.pfm",
59
- "pfm",
60
- snap("# Hello\n\nWorld\n"),
61
- dummyCtx,
62
- );
63
-
64
- const tsCode = vc!.embeddedCodes!.find((c) => c.languageId === "typescript");
65
- expect(tsCode).toBeDefined();
66
- expect(tsCode!.id).toBe("ts");
67
- expect(tsCode!.snapshot.getLength()).toBeGreaterThan(0);
68
- expect(tsCode!.mappings.length).toBeGreaterThanOrEqual(0);
69
- });
70
-
71
- it("TS VirtualCode contains svelte2tsx output", () => {
72
- const vc = plugin.createVirtualCode!(
73
- "test.pfm",
74
- "pfm",
75
- snap("---\ntitle: hello\n---\n\n# {title}\n"),
76
- dummyCtx,
77
- );
78
-
79
- const tsCode = vc!.embeddedCodes!.find((c) => c.languageId === "typescript");
80
- const tsText = tsCode!.snapshot.getText(0, tsCode!.snapshot.getLength());
81
-
82
- // svelte2tsx produces a $$render function
83
- expect(tsText).toContain("$$render");
84
- // the title variable should appear
85
- expect(tsText).toContain("title");
86
- });
87
-
88
- it("mappings survive into the TS VirtualCode", () => {
89
- const pfm = "---\ntitle: hello\ncount: 42\n---\n\n# {title}\n\n{count + 1}\n";
90
- const vc = plugin.createVirtualCode!(
91
- "test.pfm",
92
- "pfm",
93
- snap(pfm),
94
- dummyCtx,
95
- );
96
-
97
- const tsCode = vc!.embeddedCodes!.find((c) => c.languageId === "typescript");
98
- const tsText = tsCode!.snapshot.getText(0, tsCode!.snapshot.getLength());
99
-
100
- // should have composed PFM→TS mappings
101
- expect(tsCode!.mappings.length).toBeGreaterThan(0);
102
-
103
- // verify at least one mapping points back to PFM source correctly
104
- let foundTitle = false;
105
- for (const m of tsCode!.mappings) {
106
- for (let i = 0; i < m.sourceOffsets.length; i++) {
107
- const srcText = pfm.slice(
108
- m.sourceOffsets[i],
109
- m.sourceOffsets[i] + m.lengths[i],
110
- );
111
- if (srcText === "title") foundTitle = true;
112
- }
113
- }
114
- expect(foundTitle).toBe(true);
115
- });
116
-
117
- it("updateVirtualCode produces fresh VirtualCode", () => {
118
- const vc1 = plugin.createVirtualCode!(
119
- "test.pfm",
120
- "pfm",
121
- snap("---\nfoo: 1\n---\n\n{foo}\n"),
122
- dummyCtx,
123
- );
124
-
125
- const vc2 = plugin.updateVirtualCode!(
126
- "test.pfm",
127
- vc1!,
128
- snap("---\nbar: 2\n---\n\n{bar}\n"),
129
- dummyCtx,
130
- );
131
-
132
- expect(vc2).toBeDefined();
133
- const tsCode = vc2!.embeddedCodes!.find((c) => c.languageId === "typescript");
134
- const tsText = tsCode!.snapshot.getText(0, tsCode!.snapshot.getLength());
135
- // svelte2tsx preserves expressions — "bar" should appear, "foo" should not
136
- expect(tsText).toContain("bar");
137
- expect(tsText).not.toContain("foo");
138
- });
139
-
140
- it("handles complex PFM with imports and expressions", () => {
141
- const pfm = `---
142
- title: Hello World
143
- count: 42
144
- ---
145
- import Counter from "./Counter.svelte"
146
-
147
- # {title}
148
-
149
- <Counter value={count} />
150
-
151
- The count is {count + 1}.
152
- `;
153
- const vc = plugin.createVirtualCode!(
154
- "test.pfm",
155
- "pfm",
156
- snap(pfm),
157
- dummyCtx,
158
- );
159
-
160
- expect(vc).toBeDefined();
161
- const tsCode = vc!.embeddedCodes!.find((c) => c.languageId === "typescript");
162
- const tsText = tsCode!.snapshot.getText(0, tsCode!.snapshot.getLength());
163
-
164
- expect(tsText).toContain("title");
165
- expect(tsText).toContain("count");
166
- expect(tsText).toContain("Counter");
167
- expect(tsText).toContain("count + 1");
168
-
169
- // all mappings should have valid offsets
170
- for (const m of tsCode!.mappings) {
171
- for (let i = 0; i < m.sourceOffsets.length; i++) {
172
- expect(m.sourceOffsets[i]).toBeGreaterThanOrEqual(0);
173
- expect(m.sourceOffsets[i] + m.lengths[i]).toBeLessThanOrEqual(pfm.length);
174
- }
175
- }
176
- });
177
- });
package/tsconfig.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "include": ["src/**/*", "test/**/*"],
3
- "compilerOptions": {
4
- "target": "es2020",
5
- "module": "esnext",
6
- "moduleResolution": "bundler",
7
- "forceConsistentCasingInFileNames": true,
8
- "esModuleInterop": true,
9
- "strict": true
10
- }
11
- }