@flint.fyi/svelte-language 0.0.5 → 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.
@@ -9,4 +9,4 @@ interface SvelteServices {
9
9
  }
10
10
  declare const svelteLanguage: import("@flint.fyi/core").Language<import("@flint.fyi/typescript-language").TypeScriptNodesByName, Partial<SvelteServices> & import("@flint.fyi/typescript-language").TypeScriptFileServices>;
11
11
  //#endregion
12
- export { SvelteServices, svelteLanguage };
12
+ export { svelteLanguage };
@@ -1,9 +1,35 @@
1
- import { getPositionOfColumnAndLine } from "@flint.fyi/core";
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")));
@@ -158,4 +184,61 @@ function getEmbeddedTsCode(typescript, cwd, fileName, text) {
158
184
  }
159
185
  }
160
186
  //#endregion
161
- export { errorToLanguageReport, virtualCodeReports, volarLanguagePlugin };
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.5",
3
+ "version": "0.1.0",
4
4
  "description": "[Experimental] Svelte language for Flint.",
5
5
  "homepage": "https://flint.fyi",
6
6
  "repository": {
@@ -16,10 +16,10 @@
16
16
  "sideEffects": true,
17
17
  "type": "module",
18
18
  "exports": {
19
- ".": "./lib/index.js"
19
+ ".": "./dist/index.mjs"
20
20
  },
21
21
  "files": [
22
- "lib/"
22
+ "dist/"
23
23
  ],
24
24
  "dependencies": {
25
25
  "@jridgewell/sourcemap-codec": "^1.5.5",
@@ -27,15 +27,16 @@
27
27
  "svelte": "^5.0.0",
28
28
  "svelte2tsx": "^0.7.55",
29
29
  "typescript": "^5.9.0 || ^6.0.0",
30
- "@flint.fyi/core": "^0.23.3",
31
- "@flint.fyi/ts-patch": "^0.14.0",
32
- "@flint.fyi/utils": "^0.14.1",
33
- "@flint.fyi/volar-language": "^0.1.4",
34
- "@flint.fyi/typescript-language": "^0.18.4"
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"
35
35
  },
36
36
  "devDependencies": {
37
37
  "tsdown": "0.22.3",
38
- "vitest": "4.1.0"
38
+ "vitest": "4.1.0",
39
+ "@flint.fyi/build": "^0.1.0"
39
40
  },
40
41
  "engines": {
41
42
  "node": ">=24.0.0"
@@ -1,4 +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[];
@@ -1,25 +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 };
package/lib/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { svelteLanguage } from "./language.js";
2
- export { svelteLanguage };
package/lib/index.js DELETED
@@ -1,2 +0,0 @@
1
- import { svelteLanguage } from "./language.js";
2
- export { svelteLanguage };
package/lib/language.js DELETED
@@ -1,63 +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 };
@@ -1,7 +0,0 @@
1
- import { type LanguagePlugin, type VirtualCode } from "@volar/language-core";
2
- import type ts from "typescript";
3
- import type { CreateProgramOptions } from "typescript";
4
- import { type LanguageReport } from "@flint.fyi/core";
5
- export declare function volarLanguagePlugin(typescript: typeof ts, options: CreateProgramOptions): LanguagePlugin<string>;
6
- export declare const virtualCodeReports: WeakMap<VirtualCode, LanguageReport>;
7
- export declare function errorToLanguageReport(fileName: string, error: unknown): LanguageReport;