typescript-svelte-plugin 0.3.36 → 0.3.38
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/src/config-manager.d.ts +2 -0
- package/dist/src/config-manager.js +7 -0
- package/dist/src/index.js +22 -11
- package/dist/src/language-service/completions.js +6 -0
- package/dist/src/language-service/diagnostics.js +2 -1
- package/dist/src/language-service/hover.js +2 -4
- package/dist/src/language-service/index.js +2 -0
- package/dist/src/language-service/move-to-file.d.ts +2 -0
- package/dist/src/language-service/move-to-file.js +24 -0
- package/dist/src/language-service/sveltekit.js +56 -56
- package/dist/src/module-loader.d.ts +3 -1
- package/dist/src/module-loader.js +9 -2
- package/dist/src/project-svelte-files.d.ts +1 -0
- package/dist/src/project-svelte-files.js +11 -9
- package/dist/src/svelte-snapshots.d.ts +5 -1
- package/dist/src/svelte-snapshots.js +8 -2
- package/dist/src/svelte-sys.d.ts +1 -1
- package/dist/src/utils.d.ts +4 -1
- package/dist/src/utils.js +49 -4
- package/package.json +4 -3
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export interface Configuration {
|
|
2
|
+
global?: boolean;
|
|
2
3
|
enable: boolean;
|
|
3
4
|
/** Skip the Svelte detection and assume this is a Svelte project */
|
|
4
5
|
assumeIsSvelteProject: boolean;
|
|
@@ -7,6 +8,7 @@ export declare class ConfigManager {
|
|
|
7
8
|
private emitter;
|
|
8
9
|
private config;
|
|
9
10
|
onConfigurationChanged(listener: (config: Configuration) => void): void;
|
|
11
|
+
removeConfigurationChangeListener(listener: (config: Configuration) => void): void;
|
|
10
12
|
isConfigChanged(config: Configuration): boolean;
|
|
11
13
|
updateConfigFromPluginConfig(config: Configuration): void;
|
|
12
14
|
getConfig(): Configuration;
|
|
@@ -14,11 +14,18 @@ class ConfigManager {
|
|
|
14
14
|
onConfigurationChanged(listener) {
|
|
15
15
|
this.emitter.on(configurationEventName, listener);
|
|
16
16
|
}
|
|
17
|
+
removeConfigurationChangeListener(listener) {
|
|
18
|
+
this.emitter.off(configurationEventName, listener);
|
|
19
|
+
}
|
|
17
20
|
isConfigChanged(config) {
|
|
18
21
|
// right now we only care about enable
|
|
19
22
|
return config.enable !== this.config.enable;
|
|
20
23
|
}
|
|
21
24
|
updateConfigFromPluginConfig(config) {
|
|
25
|
+
// TODO this doesn't work because TS will resolve/load files already before we get the config request,
|
|
26
|
+
// which leads to TS files that use Svelte files getting all kinds of type errors
|
|
27
|
+
// const shouldWaitForConfigRequest = config.global == true;
|
|
28
|
+
// const enable = config.enable ?? !shouldWaitForConfigRequest;
|
|
22
29
|
this.config = {
|
|
23
30
|
...this.config,
|
|
24
31
|
...config
|
package/dist/src/index.js
CHANGED
|
@@ -10,10 +10,11 @@ const utils_1 = require("./utils");
|
|
|
10
10
|
function init(modules) {
|
|
11
11
|
const configManager = new config_manager_1.ConfigManager();
|
|
12
12
|
let resolvedSvelteTsxFiles;
|
|
13
|
+
const isSvelteProjectCache = new Map();
|
|
13
14
|
function create(info) {
|
|
14
15
|
const logger = new logger_1.Logger(info.project.projectService.logger);
|
|
15
16
|
if (!info.config?.assumeIsSvelteProject &&
|
|
16
|
-
!
|
|
17
|
+
!isSvelteProjectWithCache(info.project)) {
|
|
17
18
|
logger.log('Detected that this is not a Svelte project, abort patching TypeScript');
|
|
18
19
|
return info.languageService;
|
|
19
20
|
}
|
|
@@ -87,12 +88,12 @@ function init(modules) {
|
|
|
87
88
|
const svelteOptions = parsedCommandLine?.raw?.svelteOptions || { namespace: 'svelteHTML' };
|
|
88
89
|
logger.log('svelteOptions:', svelteOptions);
|
|
89
90
|
logger.debug(parsedCommandLine?.wildcardDirectories);
|
|
90
|
-
const snapshotManager = new svelte_snapshots_1.SvelteSnapshotManager(modules.typescript, info.project.projectService, svelteOptions, logger, configManager);
|
|
91
|
+
const snapshotManager = new svelte_snapshots_1.SvelteSnapshotManager(modules.typescript, info.project.projectService, svelteOptions, logger, configManager, (0, utils_1.importSvelteCompiler)((0, utils_1.getProjectDirectory)(info.project)));
|
|
91
92
|
const projectSvelteFilesManager = parsedCommandLine
|
|
92
93
|
? new project_svelte_files_1.ProjectSvelteFilesManager(modules.typescript, info.project, info.serverHost, snapshotManager, logger, parsedCommandLine, configManager)
|
|
93
94
|
: undefined;
|
|
94
|
-
(0, module_loader_1.patchModuleLoader)(logger, snapshotManager, modules.typescript, info.languageServiceHost, info.project, configManager);
|
|
95
|
-
|
|
95
|
+
const moduleLoaderDisposable = (0, module_loader_1.patchModuleLoader)(logger, snapshotManager, modules.typescript, info.languageServiceHost, info.project, configManager);
|
|
96
|
+
const updateProjectWhenConfigChanges = () => {
|
|
96
97
|
// enabling/disabling the plugin means TS has to recompute stuff
|
|
97
98
|
// don't clear semantic cache here
|
|
98
99
|
// typescript now expected the program updates to be completely in their control
|
|
@@ -103,16 +104,21 @@ function init(modules) {
|
|
|
103
104
|
if (projectSvelteFilesManager) {
|
|
104
105
|
info.project.updateGraph();
|
|
105
106
|
}
|
|
107
|
+
};
|
|
108
|
+
configManager.onConfigurationChanged(updateProjectWhenConfigChanges);
|
|
109
|
+
return (0, language_service_1.decorateLanguageService)(info.languageService, snapshotManager, logger, configManager, info, modules.typescript, () => {
|
|
110
|
+
projectSvelteFilesManager?.dispose();
|
|
111
|
+
configManager.removeConfigurationChangeListener(updateProjectWhenConfigChanges);
|
|
112
|
+
moduleLoaderDisposable.dispose();
|
|
106
113
|
});
|
|
107
|
-
return (0, language_service_1.decorateLanguageService)(info.languageService, snapshotManager, logger, configManager, info, modules.typescript, () => projectSvelteFilesManager?.dispose());
|
|
108
114
|
}
|
|
109
115
|
function getExternalFiles(project) {
|
|
110
|
-
if (!
|
|
116
|
+
if (!isSvelteProjectWithCache(project) || !configManager.getConfig().enable) {
|
|
111
117
|
return [];
|
|
112
118
|
}
|
|
113
|
-
const configFilePath =
|
|
119
|
+
const configFilePath = (0, utils_1.getProjectDirectory)(project);
|
|
114
120
|
// Needed so the ambient definitions are known inside the tsx files
|
|
115
|
-
const svelteTsxFiles = resolveSvelteTsxFiles(
|
|
121
|
+
const svelteTsxFiles = resolveSvelteTsxFiles(configFilePath);
|
|
116
122
|
if (!configFilePath) {
|
|
117
123
|
svelteTsxFiles.forEach((file) => {
|
|
118
124
|
openSvelteTsxFileForInferredProject(project, file);
|
|
@@ -145,9 +151,14 @@ function init(modules) {
|
|
|
145
151
|
resolvedSvelteTsxFiles = svelteTsxFiles;
|
|
146
152
|
return svelteTsxFiles;
|
|
147
153
|
}
|
|
148
|
-
function
|
|
149
|
-
|
|
150
|
-
|
|
154
|
+
function isSvelteProjectWithCache(project) {
|
|
155
|
+
const cached = isSvelteProjectCache.get(project.getProjectName());
|
|
156
|
+
if (cached !== undefined) {
|
|
157
|
+
return cached;
|
|
158
|
+
}
|
|
159
|
+
const result = !!(0, utils_1.isSvelteProject)(project);
|
|
160
|
+
isSvelteProjectCache.set(project.getProjectName(), result);
|
|
161
|
+
return result;
|
|
151
162
|
}
|
|
152
163
|
function onConfigurationChanged(config) {
|
|
153
164
|
if (configManager.isConfigChanged(config)) {
|
|
@@ -26,6 +26,12 @@ function decorateCompletions(ls, info, ts, logger) {
|
|
|
26
26
|
}
|
|
27
27
|
return c;
|
|
28
28
|
});
|
|
29
|
+
if (completions.optionalReplacementSpan) {
|
|
30
|
+
completions.optionalReplacementSpan = {
|
|
31
|
+
...completions.optionalReplacementSpan,
|
|
32
|
+
start: toOriginalPos(completions.optionalReplacementSpan.start).pos
|
|
33
|
+
};
|
|
34
|
+
}
|
|
29
35
|
}
|
|
30
36
|
}
|
|
31
37
|
completions =
|
|
@@ -123,7 +123,8 @@ function getKitDiagnostics(methodName, fileName, info, ts, logger) {
|
|
|
123
123
|
start: identifier.getStart(),
|
|
124
124
|
length: identifier.getEnd() - identifier.getStart(),
|
|
125
125
|
messageText: `Invalid export '${exportName}' (valid exports are ${validExports.join(', ')}, or anything with a '_' prefix)`,
|
|
126
|
-
|
|
126
|
+
// make it a warning in case people are stuck on old versions and new exports are added to SvelteKit
|
|
127
|
+
category: ts.DiagnosticCategory.Warning,
|
|
127
128
|
code: 71001 // arbitrary
|
|
128
129
|
});
|
|
129
130
|
}
|
|
@@ -18,10 +18,8 @@ function decorateHover(ls, info, ts, logger) {
|
|
|
18
18
|
const node = source && (0, utils_1.findNodeAtPosition)(source, virtualPos);
|
|
19
19
|
if (node && (0, utils_1.isTopLevelExport)(ts, node, source) && ts.isIdentifier(node)) {
|
|
20
20
|
const name = node.text;
|
|
21
|
-
if (name in sveltekit_1.kitExports) {
|
|
22
|
-
quickInfo.documentation =
|
|
23
|
-
? sveltekit_1.kitExports[name].documentation
|
|
24
|
-
: quickInfo.documentation;
|
|
21
|
+
if (name in sveltekit_1.kitExports && !quickInfo.documentation?.length) {
|
|
22
|
+
quickInfo.documentation = sveltekit_1.kitExports[name].documentation;
|
|
25
23
|
}
|
|
26
24
|
}
|
|
27
25
|
return {
|
|
@@ -15,6 +15,7 @@ const update_imports_1 = require("./update-imports");
|
|
|
15
15
|
const host_1 = require("./host");
|
|
16
16
|
const navigate_to_items_1 = require("./navigate-to-items");
|
|
17
17
|
const file_references_1 = require("./file-references");
|
|
18
|
+
const move_to_file_1 = require("./move-to-file");
|
|
18
19
|
const patchedProject = new Set();
|
|
19
20
|
function isPatched(project) {
|
|
20
21
|
return patchedProject.has(project.getProjectName());
|
|
@@ -44,6 +45,7 @@ function decorateLanguageServiceInner(ls, snapshotManager, logger, info, typescr
|
|
|
44
45
|
(0, inlay_hints_1.decorateInlayHints)(ls, info, typescript, logger);
|
|
45
46
|
(0, navigate_to_items_1.decorateNavigateToItems)(ls, snapshotManager);
|
|
46
47
|
(0, file_references_1.decorateFileReferences)(ls, snapshotManager);
|
|
48
|
+
(0, move_to_file_1.decorateMoveToRefactoringFileSuggestions)(ls);
|
|
47
49
|
decorateDispose(ls, info.project, onDispose);
|
|
48
50
|
return ls;
|
|
49
51
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decorateMoveToRefactoringFileSuggestions = void 0;
|
|
4
|
+
function decorateMoveToRefactoringFileSuggestions(ls) {
|
|
5
|
+
const getMoveToRefactoringFileSuggestions = ls.getMoveToRefactoringFileSuggestions;
|
|
6
|
+
ls.getMoveToRefactoringFileSuggestions = (fileName, positionOrRange, preferences, triggerReason, kind) => {
|
|
7
|
+
const program = ls.getProgram();
|
|
8
|
+
if (!program) {
|
|
9
|
+
return getMoveToRefactoringFileSuggestions(fileName, positionOrRange, preferences, triggerReason, kind);
|
|
10
|
+
}
|
|
11
|
+
const getSourceFiles = program.getSourceFiles;
|
|
12
|
+
try {
|
|
13
|
+
// typescript currently only allows js/ts files to be moved to.
|
|
14
|
+
// Once there isn't a restriction anymore, we can remove this.
|
|
15
|
+
program.getSourceFiles = () => getSourceFiles().filter((file) => !file.fileName.endsWith('.svelte'));
|
|
16
|
+
return getMoveToRefactoringFileSuggestions(fileName, positionOrRange, preferences, triggerReason, kind);
|
|
17
|
+
}
|
|
18
|
+
finally {
|
|
19
|
+
program.getSourceFiles = getSourceFiles;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
exports.decorateMoveToRefactoringFileSuggestions = decorateMoveToRefactoringFileSuggestions;
|
|
24
|
+
//# sourceMappingURL=move-to-file.js.map
|
|
@@ -1,27 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
3
|
exports.getVirtualLS = exports.isKitRouteExportAllowedIn = exports.kitExports = void 0;
|
|
27
4
|
const utils_1 = require("../utils");
|
|
@@ -470,6 +447,18 @@ exports.kitExports = {
|
|
|
470
447
|
kind: 'text'
|
|
471
448
|
}
|
|
472
449
|
]
|
|
450
|
+
},
|
|
451
|
+
reroute: {
|
|
452
|
+
allowedIn: [],
|
|
453
|
+
displayParts: [],
|
|
454
|
+
documentation: [
|
|
455
|
+
{
|
|
456
|
+
text: `This function allows you to change how URLs are translated into routes. ` +
|
|
457
|
+
`The returned pathname (which defaults to url.pathname) is used to select the route and its parameters. ` +
|
|
458
|
+
`More info: https://kit.svelte.dev/docs/hooks#universal-hooks-reroute`,
|
|
459
|
+
kind: 'text'
|
|
460
|
+
}
|
|
461
|
+
]
|
|
473
462
|
}
|
|
474
463
|
};
|
|
475
464
|
const FORCE_UPDATE_VERSION = 'FORCE_UPDATE_VERSION';
|
|
@@ -488,12 +477,19 @@ function isKitRouteExportAllowedIn(basename, kitExport) {
|
|
|
488
477
|
: allowedIn.includes('universal')));
|
|
489
478
|
}
|
|
490
479
|
exports.isKitRouteExportAllowedIn = isKitRouteExportAllowedIn;
|
|
480
|
+
const kitFilesSettings = {
|
|
481
|
+
paramsPath: 'src/params',
|
|
482
|
+
clientHooksPath: 'src/hooks.client',
|
|
483
|
+
serverHooksPath: 'src/hooks.server',
|
|
484
|
+
universalHooksPath: 'src/hooks'
|
|
485
|
+
};
|
|
491
486
|
function getProxiedLanguageService(info, ts, logger) {
|
|
492
487
|
const cachedProxiedLanguageService = cache.get(info);
|
|
493
488
|
if (cachedProxiedLanguageService !== undefined) {
|
|
494
489
|
return cachedProxiedLanguageService ?? undefined;
|
|
495
490
|
}
|
|
496
|
-
|
|
491
|
+
const projectDirectory = (0, utils_1.getProjectDirectory)(info.project);
|
|
492
|
+
if (projectDirectory && !(0, utils_1.hasNodeModule)(projectDirectory, '@sveltejs/kit')) {
|
|
497
493
|
// Not a SvelteKit project, do nothing
|
|
498
494
|
cache.set(info, null);
|
|
499
495
|
return;
|
|
@@ -502,9 +498,6 @@ function getProxiedLanguageService(info, ts, logger) {
|
|
|
502
498
|
class ProxiedLanguageServiceHost {
|
|
503
499
|
constructor() {
|
|
504
500
|
this.files = {};
|
|
505
|
-
this.paramsPath = 'src/params';
|
|
506
|
-
this.serverHooksPath = 'src/hooks.server';
|
|
507
|
-
this.clientHooksPath = 'src/hooks.client';
|
|
508
501
|
this.resolveModuleNames = originalLanguageServiceHost.resolveModuleNames
|
|
509
502
|
? (...args) => {
|
|
510
503
|
return originalLanguageServiceHost.resolveModuleNames(
|
|
@@ -542,29 +535,40 @@ function getProxiedLanguageService(info, ts, logger) {
|
|
|
542
535
|
this.realpath = originalLanguageServiceHost.realpath
|
|
543
536
|
? (...args) => originalLanguageServiceHost.realpath(...args)
|
|
544
537
|
: undefined;
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
538
|
+
// This never worked due to configPath being the wrong format and due to https://github.com/microsoft/TypeScript/issues/43329 .
|
|
539
|
+
// Noone has complained about this not working, so this is commented out for now, revisit if it ever comes up.
|
|
540
|
+
// const configPath = info.project.getCurrentDirectory() + '/svelte.config.js';
|
|
541
|
+
// (import(configPath)) as Promise<any>)
|
|
542
|
+
// .then((module) => {
|
|
543
|
+
// const config = module.default;
|
|
544
|
+
// if (config.kit && config.kit.files) {
|
|
545
|
+
// if (config.kit.files.params) {
|
|
546
|
+
// this.paramsPath = config.kit.files.params;
|
|
547
|
+
// }
|
|
548
|
+
// if (config.kit.files.hooks) {
|
|
549
|
+
// this.serverHooksPath ||= config.kit.files.hooks.server;
|
|
550
|
+
// this.clientHooksPath ||= config.kit.files.hooks.client;
|
|
551
|
+
// this.universalHooksPath ||= config.kit.files.hooks.universal;
|
|
552
|
+
// }
|
|
553
|
+
// logger?.log(
|
|
554
|
+
// `Using SvelteKit files config: ${JSON.stringify(
|
|
555
|
+
// config.kit.files.hooks
|
|
556
|
+
// )}`
|
|
557
|
+
// );
|
|
558
|
+
// // We could be more sophisticated with only removing the files that are actually
|
|
559
|
+
// // wrong but this is good enough given how rare it is that this setting is used
|
|
560
|
+
// Object.keys(this.files)
|
|
561
|
+
// .filter((name) => {
|
|
562
|
+
// return !name.includes('src/hooks') && !name.includes('src/params');
|
|
563
|
+
// })
|
|
564
|
+
// .forEach((name) => {
|
|
565
|
+
// delete this.files[name];
|
|
566
|
+
// });
|
|
567
|
+
// }
|
|
568
|
+
// })
|
|
569
|
+
// .catch((e) => {
|
|
570
|
+
// logger?.log('error loading SvelteKit file', e);
|
|
571
|
+
// });
|
|
568
572
|
}
|
|
569
573
|
log() { }
|
|
570
574
|
trace() { }
|
|
@@ -608,11 +612,7 @@ function getProxiedLanguageService(info, ts, logger) {
|
|
|
608
612
|
return this.files[fileName];
|
|
609
613
|
}
|
|
610
614
|
upsertKitFile(fileName) {
|
|
611
|
-
const result = svelte2tsx_1.internalHelpers.upsertKitFile(ts, fileName,
|
|
612
|
-
clientHooksPath: this.clientHooksPath,
|
|
613
|
-
paramsPath: this.paramsPath,
|
|
614
|
-
serverHooksPath: this.serverHooksPath
|
|
615
|
-
}, () => info.languageService.getProgram()?.getSourceFile(fileName));
|
|
615
|
+
const result = svelte2tsx_1.internalHelpers.upsertKitFile(ts, fileName, kitFilesSettings, () => info.languageService.getProgram()?.getSourceFile(fileName));
|
|
616
616
|
if (!result) {
|
|
617
617
|
return;
|
|
618
618
|
}
|
|
@@ -646,7 +646,7 @@ function getProxiedLanguageService(info, ts, logger) {
|
|
|
646
646
|
// Ideally we'd create a full Proxy of the language service, but that seems to have cache issues
|
|
647
647
|
// with diagnostics, which makes positions go out of sync.
|
|
648
648
|
const languageServiceHost = new ProxiedLanguageServiceHost();
|
|
649
|
-
const languageService = ts.createLanguageService(languageServiceHost, createProxyRegistry(ts, originalLanguageServiceHost,
|
|
649
|
+
const languageService = ts.createLanguageService(languageServiceHost, createProxyRegistry(ts, originalLanguageServiceHost, kitFilesSettings));
|
|
650
650
|
cache.set(info, { languageService, languageServiceHost });
|
|
651
651
|
return {
|
|
652
652
|
languageService,
|
|
@@ -18,4 +18,6 @@ declare module 'typescript/lib/tsserverlibrary' {
|
|
|
18
18
|
* so it assumes it's a normal typescript file and searches for files like `../Component.svelte.ts`, which is wrong.
|
|
19
19
|
* In order to fix this, we need to wrap typescript's module resolution and reroute all `.svelte.ts` file lookups to .svelte.
|
|
20
20
|
*/
|
|
21
|
-
export declare function patchModuleLoader(logger: Logger, snapshotManager: SvelteSnapshotManager, typescript: typeof ts, lsHost: ts.LanguageServiceHost, project: ts.server.Project, configManager: ConfigManager):
|
|
21
|
+
export declare function patchModuleLoader(logger: Logger, snapshotManager: SvelteSnapshotManager, typescript: typeof ts, lsHost: ts.LanguageServiceHost, project: ts.server.Project, configManager: ConfigManager): {
|
|
22
|
+
dispose: () => void;
|
|
23
|
+
};
|
|
@@ -73,9 +73,16 @@ function patchModuleLoader(logger, snapshotManager, typescript, lsHost, project,
|
|
|
73
73
|
moduleCache.delete(info.fileName);
|
|
74
74
|
return origRemoveFile(info, fileExists, detachFromProject);
|
|
75
75
|
};
|
|
76
|
-
|
|
76
|
+
const onConfigChanged = () => {
|
|
77
77
|
moduleCache.clear();
|
|
78
|
-
}
|
|
78
|
+
};
|
|
79
|
+
configManager.onConfigurationChanged(onConfigChanged);
|
|
80
|
+
return {
|
|
81
|
+
dispose() {
|
|
82
|
+
configManager.removeConfigurationChangeListener(onConfigChanged);
|
|
83
|
+
moduleCache.clear();
|
|
84
|
+
}
|
|
85
|
+
};
|
|
79
86
|
function resolveModuleNames(moduleNames, containingFile, reusedNames, redirectedReference, compilerOptions, containingSourceFile) {
|
|
80
87
|
logger.debug('Resolving modules names for ' + containingFile);
|
|
81
88
|
// Try resolving all module names with the original method first.
|
|
@@ -13,6 +13,7 @@ export declare class ProjectSvelteFilesManager {
|
|
|
13
13
|
private readonly snapshotManager;
|
|
14
14
|
private readonly logger;
|
|
15
15
|
private parsedCommandLine;
|
|
16
|
+
private readonly configManager;
|
|
16
17
|
private projectFileToOriginalCasing;
|
|
17
18
|
private directoryWatchers;
|
|
18
19
|
private static instances;
|
|
@@ -13,13 +13,22 @@ class ProjectSvelteFilesManager {
|
|
|
13
13
|
this.snapshotManager = snapshotManager;
|
|
14
14
|
this.logger = logger;
|
|
15
15
|
this.parsedCommandLine = parsedCommandLine;
|
|
16
|
+
this.configManager = configManager;
|
|
16
17
|
this.projectFileToOriginalCasing = new Map();
|
|
17
18
|
this.directoryWatchers = new Set();
|
|
19
|
+
this.onConfigChanged = (config) => {
|
|
20
|
+
this.disposeWatchers();
|
|
21
|
+
this.clearProjectFile();
|
|
22
|
+
if (config.enable) {
|
|
23
|
+
this.setupWatchers();
|
|
24
|
+
this.updateProjectSvelteFiles();
|
|
25
|
+
}
|
|
26
|
+
};
|
|
18
27
|
if (configManager.getConfig().enable) {
|
|
19
28
|
this.setupWatchers();
|
|
20
29
|
this.updateProjectSvelteFiles();
|
|
21
30
|
}
|
|
22
|
-
configManager.onConfigurationChanged(this.onConfigChanged
|
|
31
|
+
configManager.onConfigurationChanged(this.onConfigChanged);
|
|
23
32
|
ProjectSvelteFilesManager.instances.set(project.getProjectName(), this);
|
|
24
33
|
}
|
|
25
34
|
updateProjectConfig(serviceHost) {
|
|
@@ -109,14 +118,6 @@ class ProjectSvelteFilesManager {
|
|
|
109
118
|
.readDirectory(this.project.getCurrentDirectory() || process.cwd(), ['.svelte'], exclude, include)
|
|
110
119
|
.map(this.typescript.server.toNormalizedPath);
|
|
111
120
|
}
|
|
112
|
-
onConfigChanged(config) {
|
|
113
|
-
this.disposeWatchers();
|
|
114
|
-
this.clearProjectFile();
|
|
115
|
-
if (config.enable) {
|
|
116
|
-
this.setupWatchers();
|
|
117
|
-
this.updateProjectSvelteFiles();
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
121
|
removeFileFromProject(file, exists = true) {
|
|
121
122
|
const info = this.project.getScriptInfo(file);
|
|
122
123
|
if (info) {
|
|
@@ -137,6 +138,7 @@ class ProjectSvelteFilesManager {
|
|
|
137
138
|
// because TypeScript already does that when the project is closed
|
|
138
139
|
// - and because the project is closed, `project.removeFile` will result in an error
|
|
139
140
|
this.projectFileToOriginalCasing.clear();
|
|
141
|
+
this.configManager.removeConfigurationChangeListener(this.onConfigChanged);
|
|
140
142
|
ProjectSvelteFilesManager.instances.delete(this.project.getProjectName());
|
|
141
143
|
}
|
|
142
144
|
}
|
|
@@ -43,10 +43,14 @@ export declare class SvelteSnapshotManager {
|
|
|
43
43
|
private svelteOptions;
|
|
44
44
|
private logger;
|
|
45
45
|
private configManager;
|
|
46
|
+
/** undefined if no node_modules with Svelte next to tsconfig.json */
|
|
47
|
+
private svelteCompiler;
|
|
46
48
|
private snapshots;
|
|
47
49
|
constructor(typescript: typeof ts, projectService: ts.server.ProjectService, svelteOptions: {
|
|
48
50
|
namespace: string;
|
|
49
|
-
}, logger: Logger, configManager: ConfigManager
|
|
51
|
+
}, logger: Logger, configManager: ConfigManager,
|
|
52
|
+
/** undefined if no node_modules with Svelte next to tsconfig.json */
|
|
53
|
+
svelteCompiler: typeof import('svelte/compiler') | undefined);
|
|
50
54
|
get(fileName: string): SvelteSnapshot | undefined;
|
|
51
55
|
create(fileName: string): SvelteSnapshot | undefined;
|
|
52
56
|
private patchProjectServiceReadFile;
|
|
@@ -220,12 +220,15 @@ class SvelteSnapshot {
|
|
|
220
220
|
}
|
|
221
221
|
exports.SvelteSnapshot = SvelteSnapshot;
|
|
222
222
|
class SvelteSnapshotManager {
|
|
223
|
-
constructor(typescript, projectService, svelteOptions, logger, configManager
|
|
223
|
+
constructor(typescript, projectService, svelteOptions, logger, configManager,
|
|
224
|
+
/** undefined if no node_modules with Svelte next to tsconfig.json */
|
|
225
|
+
svelteCompiler) {
|
|
224
226
|
this.typescript = typescript;
|
|
225
227
|
this.projectService = projectService;
|
|
226
228
|
this.svelteOptions = svelteOptions;
|
|
227
229
|
this.logger = logger;
|
|
228
230
|
this.configManager = configManager;
|
|
231
|
+
this.svelteCompiler = svelteCompiler;
|
|
229
232
|
this.snapshots = new Map();
|
|
230
233
|
this.patchProjectServiceReadFile();
|
|
231
234
|
}
|
|
@@ -311,7 +314,10 @@ class SvelteSnapshotManager {
|
|
|
311
314
|
filename: path.split('/').pop(),
|
|
312
315
|
isTsFile,
|
|
313
316
|
mode: 'ts',
|
|
314
|
-
typingsNamespace: this.svelteOptions.namespace
|
|
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
|
|
315
321
|
});
|
|
316
322
|
code = result.code;
|
|
317
323
|
mapper = new source_mapper_1.SourceMapper(result.map.mappings);
|
package/dist/src/svelte-sys.d.ts
CHANGED
package/dist/src/utils.d.ts
CHANGED
|
@@ -43,5 +43,8 @@ export declare function isGeneratedSvelteComponentName(className: string): boole
|
|
|
43
43
|
export declare function offsetOfGeneratedComponentExport(snapshot: SvelteSnapshot): number;
|
|
44
44
|
export declare function gatherDescendants<T extends ts.Node>(node: ts.Node, predicate: NodeTypePredicate<T>, dest?: T[]): T[];
|
|
45
45
|
export declare function findIdentifier(ts: _ts, node: ts.Node): ts.Identifier | undefined;
|
|
46
|
-
export declare function
|
|
46
|
+
export declare function getProjectDirectory(project: ts.server.Project): string | undefined;
|
|
47
|
+
export declare function hasNodeModule(startPath: string, module: string): string | boolean;
|
|
48
|
+
export declare function isSvelteProject(project: ts.server.Project): string | boolean;
|
|
49
|
+
export declare function importSvelteCompiler(fromPath: string | undefined): typeof import('svelte/compiler') | undefined;
|
|
47
50
|
export {};
|
package/dist/src/utils.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.hasNodeModule = exports.findIdentifier = exports.gatherDescendants = exports.offsetOfGeneratedComponentExport = exports.isGeneratedSvelteComponentName = exports.isTopLevelExport = exports.findNodeAtPosition = exports.findNodeAtSpan = exports.get$storeOffsetOf$storeDeclaration = exports.isStoreVariableIn$storeDeclaration = exports.getConfigPathForProject = exports.replaceDeep = exports.isNoTextSpanInGeneratedCode = exports.isInGeneratedCode = exports.isNotNullOrUndefined = exports.ensureRealSvelteFilePath = exports.toRealSvelteFilePath = exports.isVirtualSvelteFilePath = exports.isSvelteFilePath = void 0;
|
|
3
|
+
exports.importSvelteCompiler = exports.isSvelteProject = exports.hasNodeModule = exports.getProjectDirectory = exports.findIdentifier = exports.gatherDescendants = exports.offsetOfGeneratedComponentExport = exports.isGeneratedSvelteComponentName = exports.isTopLevelExport = exports.findNodeAtPosition = exports.findNodeAtSpan = exports.get$storeOffsetOf$storeDeclaration = exports.isStoreVariableIn$storeDeclaration = exports.getConfigPathForProject = exports.replaceDeep = exports.isNoTextSpanInGeneratedCode = exports.isInGeneratedCode = exports.isNotNullOrUndefined = exports.ensureRealSvelteFilePath = exports.toRealSvelteFilePath = exports.isVirtualSvelteFilePath = exports.isSvelteFilePath = void 0;
|
|
4
|
+
const path_1 = require("path");
|
|
4
5
|
function isSvelteFilePath(filePath) {
|
|
5
6
|
return filePath.endsWith('.svelte');
|
|
6
7
|
}
|
|
@@ -187,10 +188,18 @@ function findIdentifier(ts, node) {
|
|
|
187
188
|
}
|
|
188
189
|
}
|
|
189
190
|
exports.findIdentifier = findIdentifier;
|
|
190
|
-
function
|
|
191
|
+
function getProjectDirectory(project) {
|
|
192
|
+
const compilerOptions = project.getCompilerOptions();
|
|
193
|
+
if (typeof compilerOptions.configFilePath === 'string') {
|
|
194
|
+
return (0, path_1.dirname)(compilerOptions.configFilePath);
|
|
195
|
+
}
|
|
196
|
+
const packageJsonPath = (0, path_1.join)(project.getCurrentDirectory(), 'package.json');
|
|
197
|
+
return project.fileExists(packageJsonPath) ? project.getCurrentDirectory() : undefined;
|
|
198
|
+
}
|
|
199
|
+
exports.getProjectDirectory = getProjectDirectory;
|
|
200
|
+
function hasNodeModule(startPath, module) {
|
|
191
201
|
try {
|
|
192
|
-
const hasModule =
|
|
193
|
-
require.resolve(module, { paths: [compilerOptions.configFilePath] });
|
|
202
|
+
const hasModule = require.resolve(module, { paths: [startPath] });
|
|
194
203
|
return hasModule;
|
|
195
204
|
}
|
|
196
205
|
catch (e) {
|
|
@@ -200,4 +209,40 @@ function hasNodeModule(compilerOptions, module) {
|
|
|
200
209
|
}
|
|
201
210
|
}
|
|
202
211
|
exports.hasNodeModule = hasNodeModule;
|
|
212
|
+
function isSvelteProject(project) {
|
|
213
|
+
const projectDirectory = getProjectDirectory(project);
|
|
214
|
+
if (projectDirectory) {
|
|
215
|
+
return hasNodeModule(projectDirectory, 'svelte');
|
|
216
|
+
}
|
|
217
|
+
const packageJsons = project
|
|
218
|
+
.readDirectory(project.getCurrentDirectory(), ['.json'], ['node_modules', 'dist', 'build'], ['**/package.json'],
|
|
219
|
+
// assuming structure like packages/projectName
|
|
220
|
+
3)
|
|
221
|
+
// in case some other plugin patched readDirectory in a weird way
|
|
222
|
+
.filter((file) => file.endsWith('package.json') && !hasConfigInConjunction(file, project));
|
|
223
|
+
return packageJsons.some((packageJsonPath) => hasNodeModule((0, path_1.dirname)(packageJsonPath), 'svelte'));
|
|
224
|
+
}
|
|
225
|
+
exports.isSvelteProject = isSvelteProject;
|
|
226
|
+
function hasConfigInConjunction(packageJsonPath, project) {
|
|
227
|
+
const dir = (0, path_1.dirname)(packageJsonPath);
|
|
228
|
+
return (project.fileExists((0, path_1.join)(dir, 'tsconfig.json')) ||
|
|
229
|
+
project.fileExists((0, path_1.join)(dir, 'jsconfig.json')));
|
|
230
|
+
}
|
|
231
|
+
function importSvelteCompiler(fromPath) {
|
|
232
|
+
if (!fromPath)
|
|
233
|
+
return undefined;
|
|
234
|
+
try {
|
|
235
|
+
const sveltePath = require.resolve('svelte/compiler', { paths: [fromPath] });
|
|
236
|
+
const compiler = require(sveltePath);
|
|
237
|
+
if (compiler.VERSION.split('.')[0] === '3') {
|
|
238
|
+
// use built-in version for Svelte 3
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
return compiler;
|
|
242
|
+
}
|
|
243
|
+
catch (e) {
|
|
244
|
+
// ignore
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
exports.importSvelteCompiler = importSvelteCompiler;
|
|
203
248
|
//# sourceMappingURL=utils.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-svelte-plugin",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.38",
|
|
4
4
|
"description": "A TypeScript Plugin providing Svelte intellisense",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"keywords": [
|
|
@@ -13,11 +13,12 @@
|
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@types/node": "^16.0.0",
|
|
16
|
-
"typescript": "^5.
|
|
16
|
+
"typescript": "^5.4.5",
|
|
17
|
+
"svelte": "^3.57.0"
|
|
17
18
|
},
|
|
18
19
|
"dependencies": {
|
|
19
20
|
"@jridgewell/sourcemap-codec": "^1.4.14",
|
|
20
|
-
"svelte2tsx": "~0.
|
|
21
|
+
"svelte2tsx": "~0.7.0"
|
|
21
22
|
},
|
|
22
23
|
"scripts": {
|
|
23
24
|
"build": "tsc -p ./",
|