typescript-svelte-plugin 0.3.43 → 0.3.45
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.
|
@@ -53,5 +53,5 @@ export declare class SvelteSnapshotManager {
|
|
|
53
53
|
svelteCompiler: typeof import('svelte/compiler') | undefined);
|
|
54
54
|
get(fileName: string): SvelteSnapshot | undefined;
|
|
55
55
|
create(fileName: string): SvelteSnapshot | undefined;
|
|
56
|
-
private
|
|
56
|
+
private patchProjectService;
|
|
57
57
|
}
|
|
@@ -229,8 +229,9 @@ class SvelteSnapshotManager {
|
|
|
229
229
|
this.logger = logger;
|
|
230
230
|
this.configManager = configManager;
|
|
231
231
|
this.svelteCompiler = svelteCompiler;
|
|
232
|
-
this.
|
|
233
|
-
|
|
232
|
+
this.patchProjectService();
|
|
233
|
+
// @ts-expect-error
|
|
234
|
+
this.snapshots = this.projectService[snapshots];
|
|
234
235
|
}
|
|
235
236
|
get(fileName) {
|
|
236
237
|
return this.snapshots.get(this.projectService.toCanonicalFileName(fileName));
|
|
@@ -261,98 +262,93 @@ class SvelteSnapshotManager {
|
|
|
261
262
|
this.snapshots.set(canonicalFilePath, snapshot);
|
|
262
263
|
return snapshot;
|
|
263
264
|
}
|
|
264
|
-
|
|
265
|
-
// @ts-
|
|
266
|
-
if (
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
265
|
+
patchProjectService() {
|
|
266
|
+
// @ts-expect-error The projectService is shared across some instances, make sure we patch only once
|
|
267
|
+
if (this.projectService[snapshots])
|
|
268
|
+
return;
|
|
269
|
+
this.logger.log('patching projectService');
|
|
270
|
+
// @ts-expect-error Snapshots are stored on the projectService, so they are shared across all instances
|
|
271
|
+
this.snapshots = this.projectService[snapshots] = new Map();
|
|
272
|
+
const readFile = this.projectService.host.readFile;
|
|
273
|
+
this.projectService.host.readFile = (path, encoding) => {
|
|
274
|
+
if (!this.configManager.getConfig().enable) {
|
|
275
|
+
return readFile(path, encoding);
|
|
276
|
+
}
|
|
277
|
+
// The following (very hacky) first two checks make sure that the ambient module definitions
|
|
278
|
+
// that tell TS "every import ending with .svelte is a valid module" are removed.
|
|
279
|
+
// They exist in svelte2tsx and svelte to make sure that people don't
|
|
280
|
+
// get errors in their TS files when importing Svelte files and not using our TS plugin.
|
|
281
|
+
// If someone wants to get back the behavior they can add an ambient module definition
|
|
282
|
+
// on their own.
|
|
283
|
+
const normalizedPath = path.replace(/\\/g, '/');
|
|
284
|
+
if (normalizedPath.endsWith('node_modules/svelte/types/runtime/ambient.d.ts')) {
|
|
285
|
+
return '';
|
|
286
|
+
}
|
|
287
|
+
else if (normalizedPath.endsWith('svelte2tsx/svelte-jsx.d.ts')) {
|
|
288
|
+
// Remove the dom lib reference to not load these ambient types in case
|
|
289
|
+
// the user has a tsconfig.json with different lib settings like in
|
|
290
|
+
// https://github.com/sveltejs/language-tools/issues/1733
|
|
291
|
+
const originalText = readFile(path) || '';
|
|
292
|
+
const toReplace = '/// <reference lib="dom" />';
|
|
293
|
+
return originalText.replace(toReplace, ' '.repeat(toReplace.length));
|
|
294
|
+
}
|
|
295
|
+
else if (normalizedPath.endsWith('svelte2tsx/svelte-shims.d.ts')) {
|
|
296
|
+
let originalText = readFile(path) || '';
|
|
297
|
+
if (!originalText.includes('// -- start svelte-ls-remove --')) {
|
|
298
|
+
return originalText; // uses an older version of svelte2tsx or is already patched
|
|
284
299
|
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
300
|
+
const startIdx = originalText.indexOf('// -- start svelte-ls-remove --');
|
|
301
|
+
const endIdx = originalText.indexOf('// -- end svelte-ls-remove --');
|
|
302
|
+
originalText =
|
|
303
|
+
originalText.substring(0, startIdx) +
|
|
304
|
+
' '.repeat(endIdx - startIdx) +
|
|
305
|
+
originalText.substring(endIdx);
|
|
306
|
+
return originalText;
|
|
307
|
+
}
|
|
308
|
+
else if ((0, utils_1.isSvelteFilePath)(path)) {
|
|
309
|
+
this.logger.debug('Read Svelte file:', path);
|
|
310
|
+
const svelteCode = readFile(path) || '';
|
|
311
|
+
const isTsFile = true; // TODO check file contents? TS might be okay with importing ts into js.
|
|
312
|
+
let code;
|
|
313
|
+
let mapper;
|
|
314
|
+
try {
|
|
315
|
+
const result = (0, svelte2tsx_1.svelte2tsx)(svelteCode, {
|
|
316
|
+
filename: path.split('/').pop(),
|
|
317
|
+
isTsFile,
|
|
318
|
+
mode: 'ts',
|
|
319
|
+
typingsNamespace: this.svelteOptions.namespace,
|
|
320
|
+
// Don't search for compiler from current path - could be a different one from which we have loaded the svelte2tsx globals
|
|
321
|
+
parse: this.svelteCompiler?.parse,
|
|
322
|
+
version: this.svelteCompiler?.VERSION
|
|
323
|
+
});
|
|
324
|
+
code = result.code;
|
|
325
|
+
mapper = new source_mapper_1.SourceMapper(result.map.mappings);
|
|
326
|
+
this.logger.log('Successfully read Svelte file contents of', path);
|
|
292
327
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
originalText =
|
|
301
|
-
originalText.substring(0, startIdx) +
|
|
302
|
-
' '.repeat(endIdx - startIdx) +
|
|
303
|
-
originalText.substring(endIdx);
|
|
304
|
-
return originalText;
|
|
328
|
+
catch (e) {
|
|
329
|
+
this.logger.log('Error loading Svelte file:', path, ' Using fallback.');
|
|
330
|
+
this.logger.debug('Error:', e);
|
|
331
|
+
// Return something either way, else "X is not a module" errors will appear
|
|
332
|
+
// in the TS files that use this file.
|
|
333
|
+
code = 'export default class extends Svelte2TsxComponent<any,any,any> {}';
|
|
334
|
+
mapper = new source_mapper_1.SourceMapper('');
|
|
305
335
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
let code;
|
|
311
|
-
let mapper;
|
|
312
|
-
try {
|
|
313
|
-
const result = (0, svelte2tsx_1.svelte2tsx)(svelteCode, {
|
|
314
|
-
filename: path.split('/').pop(),
|
|
315
|
-
isTsFile,
|
|
316
|
-
mode: 'ts',
|
|
317
|
-
typingsNamespace: this.svelteOptions.namespace,
|
|
318
|
-
// Don't search for compiler from current path - could be a different one from which we have loaded the svelte2tsx globals
|
|
319
|
-
parse: this.svelteCompiler?.parse,
|
|
320
|
-
version: this.svelteCompiler?.VERSION
|
|
321
|
-
});
|
|
322
|
-
code = result.code;
|
|
323
|
-
mapper = new source_mapper_1.SourceMapper(result.map.mappings);
|
|
324
|
-
this.logger.log('Successfully read Svelte file contents of', path);
|
|
325
|
-
}
|
|
326
|
-
catch (e) {
|
|
327
|
-
this.logger.log('Error loading Svelte file:', path, ' Using fallback.');
|
|
328
|
-
this.logger.debug('Error:', e);
|
|
329
|
-
// Return something either way, else "X is not a module" errors will appear
|
|
330
|
-
// in the TS files that use this file.
|
|
331
|
-
code = 'export default class extends Svelte2TsxComponent<any,any,any> {}';
|
|
332
|
-
mapper = new source_mapper_1.SourceMapper('');
|
|
333
|
-
}
|
|
334
|
-
// @ts-ignore
|
|
335
|
-
this.projectService.host[onReadSvelteFile].forEach((listener) => listener(path, svelteCode, isTsFile, mapper));
|
|
336
|
-
return code;
|
|
336
|
+
const canonicalFilePath = this.projectService.toCanonicalFileName(path);
|
|
337
|
+
const existingSnapshot = this.snapshots.get(canonicalFilePath);
|
|
338
|
+
if (existingSnapshot) {
|
|
339
|
+
existingSnapshot.update(svelteCode, mapper);
|
|
337
340
|
}
|
|
338
341
|
else {
|
|
339
|
-
|
|
342
|
+
this.snapshots.set(canonicalFilePath, new SvelteSnapshot(this.typescript, path, svelteCode, mapper, this.logger, isTsFile));
|
|
340
343
|
}
|
|
341
|
-
|
|
342
|
-
}
|
|
343
|
-
// @ts-ignore
|
|
344
|
-
this.projectService.host[onReadSvelteFile].push((path, svelteCode, isTsFile, mapper) => {
|
|
345
|
-
const canonicalFilePath = this.projectService.toCanonicalFileName(path);
|
|
346
|
-
const existingSnapshot = this.snapshots.get(canonicalFilePath);
|
|
347
|
-
if (existingSnapshot) {
|
|
348
|
-
existingSnapshot.update(svelteCode, mapper);
|
|
344
|
+
return code;
|
|
349
345
|
}
|
|
350
346
|
else {
|
|
351
|
-
|
|
347
|
+
return readFile(path, encoding);
|
|
352
348
|
}
|
|
353
|
-
}
|
|
349
|
+
};
|
|
354
350
|
}
|
|
355
351
|
}
|
|
356
352
|
exports.SvelteSnapshotManager = SvelteSnapshotManager;
|
|
357
|
-
const
|
|
353
|
+
const snapshots = Symbol('sveltePluginPatchSymbol');
|
|
358
354
|
//# sourceMappingURL=svelte-snapshots.js.map
|
package/dist/src/utils.js
CHANGED
|
@@ -217,6 +217,13 @@ function hasNodeModule(startPath, module) {
|
|
|
217
217
|
}
|
|
218
218
|
}
|
|
219
219
|
function isSvelteProject(project) {
|
|
220
|
+
// internal api, the way to check requires checking the files config in tsconfig.json
|
|
221
|
+
// so we can't reimplement it without reading the tsconfig.json again
|
|
222
|
+
// The solution project is mostly just a container we don't need to patch it
|
|
223
|
+
// and having any files in this project cause TSServer to send config error while it originally won't
|
|
224
|
+
if (project.isSolution?.()) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
220
227
|
const projectDirectory = getProjectDirectory(project);
|
|
221
228
|
if (projectDirectory) {
|
|
222
229
|
return hasNodeModule(projectDirectory, 'svelte');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-svelte-plugin",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.45",
|
|
4
4
|
"description": "A TypeScript Plugin providing Svelte intellisense",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"keywords": [
|
|
@@ -18,12 +18,12 @@
|
|
|
18
18
|
"license": "MIT",
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"@types/node": "^18.0.0",
|
|
21
|
-
"typescript": "
|
|
21
|
+
"typescript": "~5.6.3",
|
|
22
22
|
"svelte": "^4.2.19"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"@jridgewell/sourcemap-codec": "^1.5.0",
|
|
26
|
-
"svelte2tsx": "~0.7.
|
|
26
|
+
"svelte2tsx": "~0.7.25"
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
29
|
"build": "tsc -p ./",
|