Skip to content

Commit 88e4c1a

Browse files
committed
chore: tidy svelte example
1 parent 35479f0 commit 88e4c1a

File tree

5 files changed

+24
-57
lines changed

5 files changed

+24
-57
lines changed

examples/svelte-language-core/src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ export const languageModule: LanguageModule = {
1313
fileName,
1414
snapshot,
1515
kind: FileKind.TextFile,
16-
embeddedFiles: getEmbeddeds(fileName, snapshot.getText(0, snapshot.getLength())),
16+
embeddedFiles: getEmbeddedFiles(fileName, snapshot.getText(0, snapshot.getLength())),
1717
capabilities: FileCapabilities.full,
1818
mappings: [],
1919
};
2020
}
2121
},
2222
updateFile(sourceFile, snapshot) {
2323
sourceFile.snapshot = snapshot;
24-
sourceFile.embeddedFiles = getEmbeddeds(sourceFile.fileName, sourceFile.snapshot.getText(0, sourceFile.snapshot.getLength()));
24+
sourceFile.embeddedFiles = getEmbeddedFiles(sourceFile.fileName, sourceFile.snapshot.getText(0, sourceFile.snapshot.getLength()));
2525
},
2626
};
2727

28-
function getEmbeddeds(fileName: string, text: string) {
28+
function getEmbeddedFiles(fileName: string, text: string) {
2929

3030
try {
3131
const tsx = svelte2tsx(text, {
@@ -88,9 +88,9 @@ function getEmbeddeds(fileName: string, text: string) {
8888
}
8989
}
9090

91-
const embeddeds: VirtualFile[] = [];
91+
const embeddedFiles: VirtualFile[] = [];
9292

93-
embeddeds.push({
93+
embeddedFiles.push({
9494
fileName: fileName + '.ts',
9595
snapshot: {
9696
getText(start, end) {
@@ -114,7 +114,7 @@ function getEmbeddeds(fileName: string, text: string) {
114114
embeddedFiles: [],
115115
});
116116

117-
return embeddeds;
117+
return embeddedFiles;
118118
}
119119
catch {
120120
return [];
Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
11
import { languageModule } from '@volar-examples/svelte-language-core';
2-
import useTsPlugin from '@volar-plugins/typescript';
3-
import { createConnection, startLanguageServer, LanguageServerPlugin } from '@volar/language-server/node';
2+
import createTsPlugin from '@volar-plugins/typescript';
3+
import { createConnection, startLanguageServer } from '@volar/language-server/node';
44

5-
const plugin: LanguageServerPlugin = () => ({
6-
extraFileExtensions: [{ extension: 'svelte', isMixedContent: true, scriptKind: 7 }],
7-
semanticService: {
5+
startLanguageServer(
6+
createConnection(),
7+
() => ({
8+
extraFileExtensions: [{ extension: 'svelte', isMixedContent: true, scriptKind: 7 }],
89
getLanguageModules() {
910
return [languageModule];
1011
},
1112
getLanguageServicePlugins() {
12-
return [
13-
useTsPlugin(),
14-
];
13+
return [createTsPlugin()];
1514
},
16-
},
17-
syntacticService: {
18-
getLanguageModules() {
19-
return [languageModule];
20-
},
21-
getLanguageServicePlugins() {
22-
return [
23-
useTsPlugin(),
24-
];
25-
}
26-
},
27-
});
28-
29-
startLanguageServer(createConnection(), plugin);
15+
}),
16+
);

examples/svelte-tsc/bin/svelte-tsc.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/usr/bin/env node
22
const fs = require('fs');
3-
43
const readFileSync = fs.readFileSync;
54
const tscPath = require.resolve('typescript/lib/tsc');
6-
const proxyPath = require.resolve('../out/proxy');
5+
const proxyPath = require.resolve('../out/index.js');
76

87
fs.readFileSync = (...args) => {
98
if (args[0] === tscPath) {
@@ -14,11 +13,8 @@ fs.readFileSync = (...args) => {
1413
tryReplace(/supportedJSExtensions = .*(?=;)/, s => s + '.concat([[".svelte"]])');
1514
tryReplace(/allSupportedExtensions = .*(?=;)/, s => s + '.concat([[".svelte"]])');
1615

17-
// proxy startTracing, dumpTracingLegend
18-
tryReplace(/ = tracingEnabled\./g, ` = require(${JSON.stringify(proxyPath)}).loadTsLib().`);
19-
2016
// proxy createProgram apis
21-
tryReplace(/function createProgram\(.+\) {/, s => s + ` return require(${JSON.stringify(proxyPath)}).createProgramProxy(...arguments);`);
17+
tryReplace(/function createProgram\(.+\) {/, s => s + ` return require(${JSON.stringify(proxyPath)}).createProgram(...arguments);`);
2218

2319
return tsc;
2420

examples/svelte-tsc/src/proxy.ts renamed to examples/svelte-tsc/src/index.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@ import * as ts from 'typescript/lib/tsserverlibrary';
22
import * as svelte from '@volar-examples/svelte-language-core';
33
import * as svelteTs from '@volar-examples/svelte-typescript';
44

5-
export function createProgramProxy(
6-
options: ts.CreateProgramOptions, // rootNamesOrOptions: readonly string[] | CreateProgramOptions,
7-
_options?: ts.CompilerOptions,
8-
_host?: ts.CompilerHost,
9-
_oldProgram?: ts.Program,
10-
_configFileParsingDiagnostics?: readonly ts.Diagnostic[],
11-
) {
5+
export function createProgram(options: ts.CreateProgramOptions) {
126

137
if (!options.options.noEmit && !options.options.emitDeclarationOnly)
14-
return doThrow('js emit is not supported');
8+
throw toThrow('js emit is not supported');
159

1610
if (!options.options.noEmit && options.options.noEmitOnError)
17-
return doThrow('noEmitOnError is not supported');
11+
throw toThrow('noEmitOnError is not supported');
1812

1913
if (!options.host)
20-
return doThrow('!options.host');
14+
throw toThrow('!options.host');
2115

2216
let program = options.oldProgram as any;
2317

@@ -107,11 +101,7 @@ export function createProgramProxy(
107101
return program;
108102
}
109103

110-
export function loadTsLib() {
111-
return ts;
112-
}
113-
114-
function doThrow(msg: string) {
104+
function toThrow(msg: string) {
115105
console.error(msg);
116-
throw msg;
106+
return msg;
117107
}

vue-language-tools/vue-tsc/src/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@ interface ProgramContext {
1414
languageService: ReturnType<typeof vueTs.createLanguageService>,
1515
}
1616

17-
export function createProgram(
18-
options: ts.CreateProgramOptions, // rootNamesOrOptions: readonly string[] | CreateProgramOptions,
19-
_options?: ts.CompilerOptions,
20-
_host?: ts.CompilerHost,
21-
_oldProgram?: ts.Program,
22-
_configFileParsingDiagnostics?: readonly ts.Diagnostic[],
23-
) {
17+
export function createProgram(options: ts.CreateProgramOptions) {
2418

2519
if (!options.options.noEmit && !options.options.emitDeclarationOnly)
2620
throw toThrow('js emit is not supported');

0 commit comments

Comments
 (0)