@flint.fyi/svelte-language 0.0.4 → 0.1.0
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.d.mts +12 -0
- package/{lib/volarLanguagePlugin.js → dist/index.mjs} +91 -10
- package/package.json +12 -12
- package/lib/extractDirectives.d.ts +0 -5
- package/lib/extractDirectives.js +0 -27
- package/lib/index.d.ts +0 -2
- package/lib/index.js +0 -2
- package/lib/language.d.ts +0 -15
- package/lib/language.js +0 -65
- package/lib/volarLanguagePlugin.d.ts +0 -7
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AST } from "svelte/compiler";
|
|
2
|
+
|
|
3
|
+
//#region src/language.d.ts
|
|
4
|
+
interface SvelteServices {
|
|
5
|
+
svelte: {
|
|
6
|
+
ast: AST.Root;
|
|
7
|
+
sourceText: string;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
declare const svelteLanguage: import("@flint.fyi/core").Language<import("@flint.fyi/typescript-language").TypeScriptNodesByName, Partial<SvelteServices> & import("@flint.fyi/typescript-language").TypeScriptFileServices>;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { svelteLanguage };
|
|
@@ -1,20 +1,46 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parse } from "svelte/compiler";
|
|
2
|
+
import { setTSExtraSupportedExtensions } from "@flint.fyi/ts-patch";
|
|
3
|
+
import { createVolarBasedLanguage } from "@flint.fyi/volar-language";
|
|
4
|
+
import { getColumnAndLineOfPosition, getPositionOfColumnAndLine } from "@flint.fyi/core";
|
|
5
|
+
import { nullThrows } from "@flint.fyi/utils";
|
|
2
6
|
import path from "node:path";
|
|
3
7
|
import url from "node:url";
|
|
4
8
|
import { decode } from "@jridgewell/sourcemap-codec";
|
|
5
9
|
import { forEachEmbeddedCode } from "@volar/language-core";
|
|
6
10
|
import { internalHelpers, svelte2tsx } from "svelte2tsx";
|
|
11
|
+
//#region src/extractDirectives.ts
|
|
12
|
+
function extractDirectives(ast, source) {
|
|
13
|
+
const directives = [];
|
|
14
|
+
function visit(node) {
|
|
15
|
+
if ("fragment" in node) for (const child of node.fragment.nodes) visit(child);
|
|
16
|
+
if (node.type !== "Comment") return;
|
|
17
|
+
const match = /\s*flint-(\S+)(?:\s+(.+))?/.exec(node.data);
|
|
18
|
+
if (match == null) return;
|
|
19
|
+
const [, type, selection] = match;
|
|
20
|
+
directives.push({
|
|
21
|
+
range: {
|
|
22
|
+
begin: getColumnAndLineOfPosition(source, node.start),
|
|
23
|
+
end: getColumnAndLineOfPosition(source, node.end)
|
|
24
|
+
},
|
|
25
|
+
selection: nullThrows(selection, "Expected RegExp to provide second capturing group"),
|
|
26
|
+
type: nullThrows(type, "Expected RegExp to provide first capturing group")
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
for (const child of ast.fragment.nodes) visit(child);
|
|
30
|
+
return directives;
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
7
33
|
//#region src/volarLanguagePlugin.ts
|
|
8
34
|
const sveltePath = path.dirname(url.fileURLToPath(import.meta.resolve("svelte/package.json")));
|
|
9
35
|
const svelte2tsxPath = path.dirname(url.fileURLToPath(import.meta.resolve("svelte2tsx/package.json")));
|
|
10
|
-
function volarLanguagePlugin(
|
|
11
|
-
const cwd = typeof options.options.configFilePath === "string" ? path.dirname(options.options.configFilePath) : (options.host ??
|
|
36
|
+
function volarLanguagePlugin(typescript, options) {
|
|
37
|
+
const cwd = typeof options.options.configFilePath === "string" ? path.dirname(options.options.configFilePath) : (options.host ?? typescript.sys).getCurrentDirectory();
|
|
12
38
|
return {
|
|
13
39
|
createVirtualCode(fileName, languageId, snapshot) {
|
|
14
40
|
if (languageId !== "svelte") return;
|
|
15
41
|
return {
|
|
16
42
|
codegenStacks: [],
|
|
17
|
-
embeddedCodes: [getEmbeddedTsCode(
|
|
43
|
+
embeddedCodes: [getEmbeddedTsCode(typescript, cwd, fileName, snapshot.getText(0, snapshot.getLength()))],
|
|
18
44
|
id: "root",
|
|
19
45
|
languageId,
|
|
20
46
|
mappings: [],
|
|
@@ -40,7 +66,7 @@ function volarLanguagePlugin(ts, options) {
|
|
|
40
66
|
},
|
|
41
67
|
updateVirtualCode(fileName, virtualCode, snapshot) {
|
|
42
68
|
virtualCode.snapshot = snapshot;
|
|
43
|
-
virtualCode.embeddedCodes = [getEmbeddedTsCode(
|
|
69
|
+
virtualCode.embeddedCodes = [getEmbeddedTsCode(typescript, cwd, fileName, snapshot.getText(0, snapshot.getLength()))];
|
|
44
70
|
return virtualCode;
|
|
45
71
|
}
|
|
46
72
|
};
|
|
@@ -66,8 +92,8 @@ function errorToLanguageReport(fileName, error) {
|
|
|
66
92
|
function isSvelteCompileError(error) {
|
|
67
93
|
return "start" in error && typeof error.start === "object" && error.start !== null && "character" in error.start && typeof error.start.character === "number";
|
|
68
94
|
}
|
|
69
|
-
function getEmbeddedTsCode(
|
|
70
|
-
const svelteTsxFiles = internalHelpers.get_global_types(
|
|
95
|
+
function getEmbeddedTsCode(typescript, cwd, fileName, text) {
|
|
96
|
+
const svelteTsxFiles = internalHelpers.get_global_types(typescript.sys, false, sveltePath, svelte2tsxPath, cwd);
|
|
71
97
|
try {
|
|
72
98
|
const tsx = svelte2tsx(text, {
|
|
73
99
|
isTsFile: true,
|
|
@@ -158,6 +184,61 @@ function getEmbeddedTsCode(ts, cwd, fileName, text) {
|
|
|
158
184
|
}
|
|
159
185
|
}
|
|
160
186
|
//#endregion
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
187
|
+
//#region src/language.ts
|
|
188
|
+
setTSExtraSupportedExtensions([".svelte"]);
|
|
189
|
+
const svelteLanguage = createVolarBasedLanguage((ts, options) => {
|
|
190
|
+
return {
|
|
191
|
+
createFile({ sourceFile, sourceScript }) {
|
|
192
|
+
const sourceText = sourceScript.snapshot.getText(0, sourceScript.snapshot.getLength());
|
|
193
|
+
const source = { text: sourceText };
|
|
194
|
+
const virtualCode = sourceScript.generated.root;
|
|
195
|
+
let ast;
|
|
196
|
+
const reports = [];
|
|
197
|
+
try {
|
|
198
|
+
ast = parse(sourceText, {
|
|
199
|
+
loose: true,
|
|
200
|
+
modern: true
|
|
201
|
+
});
|
|
202
|
+
} catch (error) {
|
|
203
|
+
reports.push(errorToLanguageReport(sourceFile.fileName, error));
|
|
204
|
+
ast = {
|
|
205
|
+
comments: [],
|
|
206
|
+
css: null,
|
|
207
|
+
end: 0,
|
|
208
|
+
fragment: {
|
|
209
|
+
nodes: [],
|
|
210
|
+
type: "Fragment"
|
|
211
|
+
},
|
|
212
|
+
instance: null,
|
|
213
|
+
module: null,
|
|
214
|
+
options: null,
|
|
215
|
+
start: 0,
|
|
216
|
+
type: "Root"
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
const codegenReport = virtualCodeReports.get(virtualCode);
|
|
220
|
+
if (codegenReport != null) reports.push(codegenReport);
|
|
221
|
+
return {
|
|
222
|
+
directives: extractDirectives(ast, source),
|
|
223
|
+
extraContext: { svelte: {
|
|
224
|
+
ast,
|
|
225
|
+
sourceText
|
|
226
|
+
} },
|
|
227
|
+
firstStatementPosition: Math.min(...[
|
|
228
|
+
ast.fragment.nodes.find((node) => node.type !== "Text" || !!node.data.trim().length)?.start,
|
|
229
|
+
ast.module?.start,
|
|
230
|
+
ast.instance?.start,
|
|
231
|
+
ast.css?.start,
|
|
232
|
+
ast.options?.start,
|
|
233
|
+
sourceText.length
|
|
234
|
+
].filter((pos) => typeof pos === "number")),
|
|
235
|
+
getLanguageReports() {
|
|
236
|
+
return reports;
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
},
|
|
240
|
+
languagePlugins: [volarLanguagePlugin(ts, options)]
|
|
241
|
+
};
|
|
242
|
+
});
|
|
243
|
+
//#endregion
|
|
244
|
+
export { svelteLanguage };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flint.fyi/svelte-language",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "[Experimental] Svelte language for Flint.",
|
|
5
5
|
"homepage": "https://flint.fyi",
|
|
6
6
|
"repository": {
|
|
@@ -16,27 +16,27 @@
|
|
|
16
16
|
"sideEffects": true,
|
|
17
17
|
"type": "module",
|
|
18
18
|
"exports": {
|
|
19
|
-
".": "./
|
|
19
|
+
".": "./dist/index.mjs"
|
|
20
20
|
},
|
|
21
21
|
"files": [
|
|
22
|
-
"
|
|
23
|
-
"!lib/**/*.map"
|
|
22
|
+
"dist/"
|
|
24
23
|
],
|
|
25
24
|
"dependencies": {
|
|
26
25
|
"@jridgewell/sourcemap-codec": "^1.5.5",
|
|
27
26
|
"@volar/language-core": "2.4.28",
|
|
28
27
|
"svelte": "^5.0.0",
|
|
29
|
-
"svelte2tsx": "^0.7.
|
|
28
|
+
"svelte2tsx": "^0.7.55",
|
|
30
29
|
"typescript": "^5.9.0 || ^6.0.0",
|
|
31
|
-
"@flint.fyi/
|
|
32
|
-
"@flint.fyi/ts-patch": "^0.
|
|
33
|
-
"@flint.fyi/
|
|
34
|
-
"@flint.fyi/
|
|
35
|
-
"@flint.fyi/
|
|
30
|
+
"@flint.fyi/core": "^0.24.0",
|
|
31
|
+
"@flint.fyi/ts-patch": "^0.15.0",
|
|
32
|
+
"@flint.fyi/typescript-language": "^0.19.0",
|
|
33
|
+
"@flint.fyi/volar-language": "^0.2.0",
|
|
34
|
+
"@flint.fyi/utils": "^0.15.0"
|
|
36
35
|
},
|
|
37
36
|
"devDependencies": {
|
|
38
|
-
"tsdown": "0.
|
|
39
|
-
"vitest": "4.1.0"
|
|
37
|
+
"tsdown": "0.22.3",
|
|
38
|
+
"vitest": "4.1.0",
|
|
39
|
+
"@flint.fyi/build": "^0.1.0"
|
|
40
40
|
},
|
|
41
41
|
"engines": {
|
|
42
42
|
"node": ">=24.0.0"
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { AST } from "svelte/compiler";
|
|
2
|
-
import { type SourceFileWithLineMap } from "@flint.fyi/core";
|
|
3
|
-
import type { ExtractedDirective } from "@flint.fyi/typescript-language";
|
|
4
|
-
export declare function extractDirectives(ast: AST.Root, source: SourceFileWithLineMap): ExtractedDirective[];
|
|
5
|
-
//# sourceMappingURL=extractDirectives.d.ts.map
|
package/lib/extractDirectives.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { getColumnAndLineOfPosition } from "@flint.fyi/core";
|
|
2
|
-
import { nullThrows } from "@flint.fyi/utils";
|
|
3
|
-
//#region src/extractDirectives.ts
|
|
4
|
-
function extractDirectives(ast, source) {
|
|
5
|
-
const directives = [];
|
|
6
|
-
function visit(node) {
|
|
7
|
-
if ("fragment" in node) for (const child of node.fragment.nodes) visit(child);
|
|
8
|
-
if (node.type !== "Comment") return;
|
|
9
|
-
const match = /\s*flint-(\S+)(?:\s+(.+))?/.exec(node.data);
|
|
10
|
-
if (match == null) return;
|
|
11
|
-
const [, type, selection] = match;
|
|
12
|
-
directives.push({
|
|
13
|
-
range: {
|
|
14
|
-
begin: getColumnAndLineOfPosition(source, node.start),
|
|
15
|
-
end: getColumnAndLineOfPosition(source, node.end)
|
|
16
|
-
},
|
|
17
|
-
selection: nullThrows(selection, "Expected RegExp to provide second capturing group"),
|
|
18
|
-
type: nullThrows(type, "Expected RegExp to provide first capturing group")
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
for (const child of ast.fragment.nodes) visit(child);
|
|
22
|
-
return directives;
|
|
23
|
-
}
|
|
24
|
-
//#endregion
|
|
25
|
-
export { extractDirectives };
|
|
26
|
-
|
|
27
|
-
//# sourceMappingURL=extractDirectives.js.map
|
package/lib/index.d.ts
DELETED
package/lib/index.js
DELETED
package/lib/language.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { AST } from "svelte/compiler";
|
|
2
|
-
import * as _flint_fyi_core0 from "@flint.fyi/core";
|
|
3
|
-
import * as _flint_fyi_typescript_language0 from "@flint.fyi/typescript-language";
|
|
4
|
-
|
|
5
|
-
//#region src/language.d.ts
|
|
6
|
-
interface SvelteServices {
|
|
7
|
-
svelte: {
|
|
8
|
-
ast: AST.Root;
|
|
9
|
-
sourceText: string;
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
declare const svelteLanguage: _flint_fyi_core0.Language<_flint_fyi_typescript_language0.TypeScriptNodesByName, Partial<SvelteServices> & _flint_fyi_typescript_language0.TypeScriptFileServices>;
|
|
13
|
-
//#endregion
|
|
14
|
-
export { svelteLanguage };
|
|
15
|
-
//# sourceMappingURL=language.d.ts.map
|
package/lib/language.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { extractDirectives } from "./extractDirectives.js";
|
|
2
|
-
import { errorToLanguageReport, virtualCodeReports, volarLanguagePlugin } from "./volarLanguagePlugin.js";
|
|
3
|
-
import { parse } from "svelte/compiler";
|
|
4
|
-
import { setTSExtraSupportedExtensions } from "@flint.fyi/ts-patch";
|
|
5
|
-
import { createVolarBasedLanguage } from "@flint.fyi/volar-language";
|
|
6
|
-
//#region src/language.ts
|
|
7
|
-
setTSExtraSupportedExtensions([".svelte"]);
|
|
8
|
-
const svelteLanguage = createVolarBasedLanguage((ts, options) => {
|
|
9
|
-
return {
|
|
10
|
-
createFile({ sourceFile, sourceScript }) {
|
|
11
|
-
const sourceText = sourceScript.snapshot.getText(0, sourceScript.snapshot.getLength());
|
|
12
|
-
const source = { text: sourceText };
|
|
13
|
-
const virtualCode = sourceScript.generated.root;
|
|
14
|
-
let ast;
|
|
15
|
-
const reports = [];
|
|
16
|
-
try {
|
|
17
|
-
ast = parse(sourceText, {
|
|
18
|
-
loose: true,
|
|
19
|
-
modern: true
|
|
20
|
-
});
|
|
21
|
-
} catch (error) {
|
|
22
|
-
reports.push(errorToLanguageReport(sourceFile.fileName, error));
|
|
23
|
-
ast = {
|
|
24
|
-
comments: [],
|
|
25
|
-
css: null,
|
|
26
|
-
end: 0,
|
|
27
|
-
fragment: {
|
|
28
|
-
nodes: [],
|
|
29
|
-
type: "Fragment"
|
|
30
|
-
},
|
|
31
|
-
instance: null,
|
|
32
|
-
module: null,
|
|
33
|
-
options: null,
|
|
34
|
-
start: 0,
|
|
35
|
-
type: "Root"
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
const codegenReport = virtualCodeReports.get(virtualCode);
|
|
39
|
-
if (codegenReport != null) reports.push(codegenReport);
|
|
40
|
-
return {
|
|
41
|
-
directives: extractDirectives(ast, source),
|
|
42
|
-
extraContext: { svelte: {
|
|
43
|
-
ast,
|
|
44
|
-
sourceText
|
|
45
|
-
} },
|
|
46
|
-
firstStatementPosition: Math.min(...[
|
|
47
|
-
ast.fragment.nodes.find((node) => node.type !== "Text" || !!node.data.trim().length)?.start,
|
|
48
|
-
ast.module?.start,
|
|
49
|
-
ast.instance?.start,
|
|
50
|
-
ast.css?.start,
|
|
51
|
-
ast.options?.start,
|
|
52
|
-
sourceText.length
|
|
53
|
-
].filter((pos) => typeof pos === "number")),
|
|
54
|
-
getLanguageReports() {
|
|
55
|
-
return reports;
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
},
|
|
59
|
-
languagePlugins: [volarLanguagePlugin(ts, options)]
|
|
60
|
-
};
|
|
61
|
-
});
|
|
62
|
-
//#endregion
|
|
63
|
-
export { svelteLanguage };
|
|
64
|
-
|
|
65
|
-
//# sourceMappingURL=language.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { type LanguagePlugin, type VirtualCode } from "@volar/language-core";
|
|
2
|
-
import type * as ts from "typescript";
|
|
3
|
-
import { type LanguageReport } from "@flint.fyi/core";
|
|
4
|
-
export declare function volarLanguagePlugin(ts: typeof import("typescript"), options: ts.CreateProgramOptions): LanguagePlugin<string>;
|
|
5
|
-
export declare const virtualCodeReports: WeakMap<VirtualCode, LanguageReport>;
|
|
6
|
-
export declare function errorToLanguageReport(fileName: string, error: unknown): LanguageReport;
|
|
7
|
-
//# sourceMappingURL=volarLanguagePlugin.d.ts.map
|