typescript-svelte-plugin 0.3.1 → 0.3.4

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.
@@ -25,3 +25,4 @@ class ConfigManager {
25
25
  }
26
26
  }
27
27
  exports.ConfigManager = ConfigManager;
28
+ //# sourceMappingURL=config-manager.js.map
package/dist/src/index.js CHANGED
@@ -95,3 +95,4 @@ function init(modules) {
95
95
  return { create, getExternalFiles, onConfigurationChanged };
96
96
  }
97
97
  module.exports = init;
98
+ //# sourceMappingURL=index.js.map
@@ -51,3 +51,4 @@ function decorateCompletions(ls, logger) {
51
51
  };
52
52
  }
53
53
  exports.decorateCompletions = decorateCompletions;
54
+ //# sourceMappingURL=completions.js.map
@@ -38,3 +38,4 @@ function decorateGetDefinition(ls, snapshotManager, logger) {
38
38
  };
39
39
  }
40
40
  exports.decorateGetDefinition = decorateGetDefinition;
41
+ //# sourceMappingURL=definition.js.map
@@ -41,3 +41,4 @@ function decorateSuggestionDiagnostics(ls) {
41
41
  return getSuggestionDiagnostics(fileName);
42
42
  };
43
43
  }
44
+ //# sourceMappingURL=diagnostics.js.map
@@ -9,12 +9,19 @@ function decorateFindReferences(ls, snapshotManager, logger) {
9
9
  exports.decorateFindReferences = decorateFindReferences;
10
10
  function _decorateFindReferences(ls, snapshotManager, logger) {
11
11
  const findReferences = ls.findReferences;
12
+ const getReferences = (fileName, position) => {
13
+ var _a;
14
+ return (_a = findReferences(fileName, position)) === null || _a === void 0 ? void 0 : _a.reduce((acc, curr) => acc.concat(curr.references), []);
15
+ };
12
16
  ls.findReferences = (fileName, position) => {
13
17
  const references = findReferences(fileName, position);
14
18
  return references === null || references === void 0 ? void 0 : references.map((reference) => {
15
19
  const snapshot = snapshotManager.get(reference.definition.fileName);
16
20
  if (!(0, utils_1.isSvelteFilePath)(reference.definition.fileName) || !snapshot) {
17
- return reference;
21
+ return {
22
+ ...reference,
23
+ references: mapReferences(reference.references, snapshotManager, logger, getReferences)
24
+ };
18
25
  }
19
26
  const textSpan = snapshot.getOriginalTextSpan(reference.definition.textSpan);
20
27
  if (!textSpan) {
@@ -27,7 +34,7 @@ function _decorateFindReferences(ls, snapshotManager, logger) {
27
34
  // Spare the work for now
28
35
  originalTextSpan: undefined
29
36
  },
30
- references: mapReferences(reference.references, snapshotManager, logger)
37
+ references: mapReferences(reference.references, snapshotManager, logger, getReferences)
31
38
  };
32
39
  }).filter(utils_1.isNotNullOrUndefined);
33
40
  };
@@ -36,20 +43,40 @@ function decorateGetReferencesAtPosition(ls, snapshotManager, logger) {
36
43
  const getReferencesAtPosition = ls.getReferencesAtPosition;
37
44
  ls.getReferencesAtPosition = (fileName, position) => {
38
45
  const references = getReferencesAtPosition(fileName, position);
39
- return references && mapReferences(references, snapshotManager, logger);
46
+ return (references &&
47
+ mapReferences(references, snapshotManager, logger, getReferencesAtPosition));
40
48
  };
41
49
  }
42
- function mapReferences(references, snapshotManager, logger) {
43
- return references
44
- .map((reference) => {
50
+ function mapReferences(references, snapshotManager, logger, getReferences) {
51
+ const additionalStoreReferences = [];
52
+ const mappedReferences = [];
53
+ for (const reference of references) {
45
54
  const snapshot = snapshotManager.get(reference.fileName);
46
55
  if (!(0, utils_1.isSvelteFilePath)(reference.fileName) || !snapshot) {
47
- return reference;
56
+ mappedReferences.push(reference);
57
+ continue;
58
+ }
59
+ const textSpan = snapshot.getOriginalTextSpan(reference.textSpan);
60
+ if (textSpan) {
61
+ mappedReferences.push(mapReference(reference, textSpan));
48
62
  }
63
+ else {
64
+ if ((0, utils_1.isStoreVariableIn$storeDeclaration)(snapshot.getText(), reference.textSpan.start)) {
65
+ additionalStoreReferences.push(...(getReferences(reference.fileName, (0, utils_1.get$storeOffsetOf$storeDeclaration)(snapshot.getText(), reference.textSpan.start)) || []));
66
+ }
67
+ }
68
+ }
69
+ for (const reference of additionalStoreReferences) {
70
+ // We know these are Svelte files
71
+ const snapshot = snapshotManager.get(reference.fileName);
49
72
  const textSpan = snapshot.getOriginalTextSpan(reference.textSpan);
50
73
  if (!textSpan) {
51
- return null;
74
+ continue;
52
75
  }
76
+ mappedReferences.push(mapReference(reference, textSpan));
77
+ }
78
+ return mappedReferences;
79
+ function mapReference(reference, textSpan) {
53
80
  logger.debug('Find references; map textSpan: changed', reference.textSpan, 'to', textSpan);
54
81
  return {
55
82
  ...reference,
@@ -59,6 +86,6 @@ function mapReferences(references, snapshotManager, logger) {
59
86
  originalTextSpan: undefined,
60
87
  originalContextSpan: undefined
61
88
  };
62
- })
63
- .filter(utils_1.isNotNullOrUndefined);
89
+ }
64
90
  }
91
+ //# sourceMappingURL=find-references.js.map
@@ -28,3 +28,4 @@ function decorateGetImplementation(ls, snapshotManager, logger) {
28
28
  };
29
29
  }
30
30
  exports.decorateGetImplementation = decorateGetImplementation;
31
+ //# sourceMappingURL=implementation.js.map
@@ -8,6 +8,7 @@ const diagnostics_1 = require("./diagnostics");
8
8
  const find_references_1 = require("./find-references");
9
9
  const implementation_1 = require("./implementation");
10
10
  const rename_1 = require("./rename");
11
+ const update_imports_1 = require("./update-imports");
11
12
  const sveltePluginPatchSymbol = Symbol('sveltePluginPatchSymbol');
12
13
  function isPatched(ls) {
13
14
  return ls[sveltePluginPatchSymbol] === true;
@@ -29,6 +30,7 @@ function decorateLanguageServiceInner(ls, snapshotManager, logger) {
29
30
  (0, completions_1.decorateCompletions)(ls, logger);
30
31
  (0, definition_1.decorateGetDefinition)(ls, snapshotManager, logger);
31
32
  (0, implementation_1.decorateGetImplementation)(ls, snapshotManager, logger);
33
+ (0, update_imports_1.decorateUpdateImports)(ls, snapshotManager, logger);
32
34
  return ls;
33
35
  }
34
36
  function createProxyHandler(configManager) {
@@ -67,3 +69,4 @@ function patchLineColumnOffset(ls, snapshotManager) {
67
69
  return toLineColumnOffset(fileName, position);
68
70
  };
69
71
  }
72
+ //# sourceMappingURL=index.js.map
@@ -6,27 +6,52 @@ function decorateRename(ls, snapshotManager, logger) {
6
6
  const findRenameLocations = ls.findRenameLocations;
7
7
  ls.findRenameLocations = (fileName, position, findInStrings, findInComments, providePrefixAndSuffixTextForRename) => {
8
8
  const renameLocations = findRenameLocations(fileName, position, findInStrings, findInComments, providePrefixAndSuffixTextForRename);
9
- return renameLocations === null || renameLocations === void 0 ? void 0 : renameLocations.map((renameLocation) => {
9
+ if (!renameLocations) {
10
+ return undefined;
11
+ }
12
+ const convertedRenameLocations = [];
13
+ const additionalStoreRenameLocations = [];
14
+ for (const renameLocation of renameLocations) {
10
15
  const snapshot = snapshotManager.get(renameLocation.fileName);
11
16
  if (!(0, utils_1.isSvelteFilePath)(renameLocation.fileName) || !snapshot) {
12
- return renameLocation;
17
+ convertedRenameLocations.push(renameLocation);
18
+ continue;
13
19
  }
14
20
  // TODO more needed to filter invalid locations, see RenameProvider
15
21
  const textSpan = snapshot.getOriginalTextSpan(renameLocation.textSpan);
16
22
  if (!textSpan) {
17
- return null;
23
+ if ((0, utils_1.isStoreVariableIn$storeDeclaration)(snapshot.getText(), renameLocation.textSpan.start)) {
24
+ additionalStoreRenameLocations.push(...findRenameLocations(renameLocation.fileName, (0, utils_1.get$storeOffsetOf$storeDeclaration)(snapshot.getText(), renameLocation.textSpan.start), false, false, false));
25
+ }
26
+ continue;
18
27
  }
19
- const converted = {
20
- ...renameLocation,
21
- textSpan
22
- };
23
- if (converted.contextSpan) {
24
- // Not important, spare the work
25
- converted.contextSpan = undefined;
28
+ convertedRenameLocations.push(convert(renameLocation, textSpan));
29
+ }
30
+ for (const renameLocation of additionalStoreRenameLocations) {
31
+ // We know these are Svelte files
32
+ const snapshot = snapshotManager.get(renameLocation.fileName);
33
+ const textSpan = snapshot.getOriginalTextSpan(renameLocation.textSpan);
34
+ if (!textSpan) {
35
+ continue;
26
36
  }
27
- logger.debug('Converted rename location ', converted);
28
- return converted;
29
- }).filter(utils_1.isNotNullOrUndefined);
37
+ // |$store| would be renamed, make it $|store|
38
+ textSpan.start += 1;
39
+ convertedRenameLocations.push(convert(renameLocation, textSpan));
40
+ }
41
+ return convertedRenameLocations;
30
42
  };
43
+ function convert(renameLocation, textSpan) {
44
+ const converted = {
45
+ ...renameLocation,
46
+ textSpan
47
+ };
48
+ if (converted.contextSpan) {
49
+ // Not important, spare the work
50
+ converted.contextSpan = undefined;
51
+ }
52
+ logger.debug('Converted rename location ', converted);
53
+ return converted;
54
+ }
31
55
  }
32
56
  exports.decorateRename = decorateRename;
57
+ //# sourceMappingURL=rename.js.map
@@ -0,0 +1,4 @@
1
+ import type ts from 'typescript/lib/tsserverlibrary';
2
+ import { Logger } from '../logger';
3
+ import { SvelteSnapshotManager } from '../svelte-snapshots';
4
+ export declare function decorateUpdateImports(ls: ts.LanguageService, snapshotManager: SvelteSnapshotManager, logger: Logger): void;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decorateUpdateImports = void 0;
4
+ const utils_1 = require("../utils");
5
+ function decorateUpdateImports(ls, snapshotManager, logger) {
6
+ const getEditsForFileRename = ls.getEditsForFileRename;
7
+ ls.getEditsForFileRename = (oldFilePath, newFilePath, formatOptions, preferences) => {
8
+ const renameLocations = getEditsForFileRename(oldFilePath, newFilePath, formatOptions, preferences);
9
+ // If a file move/rename of a TS/JS file results a Svelte file change,
10
+ // the Svelte extension will notice that, too, and adjusts the same imports.
11
+ // This results in duplicate adjustments or race conditions with conflicting text spans
12
+ // which can break imports in some cases.
13
+ // Therefore don't do any updates of Svelte files and and also no updates of mixed TS files
14
+ // and let the Svelte extension handle that.
15
+ return renameLocations === null || renameLocations === void 0 ? void 0 : renameLocations.filter((renameLocation) => {
16
+ return (!(0, utils_1.isSvelteFilePath)(renameLocation.fileName) &&
17
+ !renameLocation.textChanges.some((change) => change.newText.endsWith('.svelte')));
18
+ });
19
+ };
20
+ }
21
+ exports.decorateUpdateImports = decorateUpdateImports;
22
+ //# sourceMappingURL=update-imports.js.map
@@ -38,3 +38,4 @@ class Logger {
38
38
  }
39
39
  }
40
40
  exports.Logger = Logger;
41
+ //# sourceMappingURL=logger.js.map
@@ -67,13 +67,13 @@ function patchModuleLoader(logger, snapshotManager, typescript, lsHost, project,
67
67
  configManager.onConfigurationChanged(() => {
68
68
  moduleCache.clear();
69
69
  });
70
- function resolveModuleNames(moduleNames, containingFile, reusedNames, redirectedReference, compilerOptions) {
70
+ function resolveModuleNames(moduleNames, containingFile, reusedNames, redirectedReference, compilerOptions, containingSourceFile) {
71
71
  logger.log('Resolving modules names for ' + containingFile);
72
72
  // Try resolving all module names with the original method first.
73
73
  // The ones that are undefined will be re-checked if they are a
74
74
  // Svelte file and if so, are resolved, too. This way we can defer
75
75
  // all module resolving logic except for Svelte files to TypeScript.
76
- const resolved = (origResolveModuleNames === null || origResolveModuleNames === void 0 ? void 0 : origResolveModuleNames(moduleNames, containingFile, reusedNames, redirectedReference, compilerOptions)) || Array.from(Array(moduleNames.length));
76
+ const resolved = (origResolveModuleNames === null || origResolveModuleNames === void 0 ? void 0 : origResolveModuleNames(moduleNames, containingFile, reusedNames, redirectedReference, compilerOptions, containingSourceFile)) || Array.from(Array(moduleNames.length));
77
77
  if (!configManager.getConfig().enable) {
78
78
  return resolved;
79
79
  }
@@ -86,13 +86,15 @@ function patchModuleLoader(logger, snapshotManager, typescript, lsHost, project,
86
86
  if (cachedModule) {
87
87
  return cachedModule;
88
88
  }
89
- const resolvedModule = resolveModuleName(fileName, containingFile, compilerOptions);
89
+ const resolvedModule = resolveSvelteModuleName(fileName, containingFile, compilerOptions);
90
90
  moduleCache.set(fileName, containingFile, resolvedModule);
91
91
  return resolvedModule;
92
92
  });
93
93
  }
94
- function resolveModuleName(name, containingFile, compilerOptions) {
95
- const svelteResolvedModule = typescript.resolveModuleName(name, containingFile, compilerOptions, svelteSys).resolvedModule;
94
+ function resolveSvelteModuleName(name, containingFile, compilerOptions) {
95
+ const svelteResolvedModule = typescript.resolveModuleName(name, containingFile, compilerOptions, svelteSys
96
+ // don't set mode or else .svelte imports couldn't be resolved
97
+ ).resolvedModule;
96
98
  if (!svelteResolvedModule ||
97
99
  !(0, utils_1.isVirtualSvelteFilePath)(svelteResolvedModule.resolvedFileName)) {
98
100
  return svelteResolvedModule;
@@ -111,3 +113,4 @@ function patchModuleLoader(logger, snapshotManager, typescript, lsHost, project,
111
113
  }
112
114
  }
113
115
  exports.patchModuleLoader = patchModuleLoader;
116
+ //# sourceMappingURL=module-loader.js.map
@@ -121,3 +121,4 @@ class ProjectSvelteFilesManager {
121
121
  }
122
122
  exports.ProjectSvelteFilesManager = ProjectSvelteFilesManager;
123
123
  ProjectSvelteFilesManager.instances = new Map();
124
+ //# sourceMappingURL=project-svelte-files.js.map
@@ -99,3 +99,4 @@ class SourceMapper {
99
99
  }
100
100
  }
101
101
  exports.SourceMapper = SourceMapper;
102
+ //# sourceMappingURL=source-mapper.js.map
@@ -32,7 +32,7 @@ export declare class SvelteSnapshot {
32
32
  private log;
33
33
  private debug;
34
34
  private toggleMappingMode;
35
- private getText;
35
+ getText(): string;
36
36
  }
37
37
  export declare class SvelteSnapshotManager {
38
38
  private typescript;
@@ -229,7 +229,27 @@ class SvelteSnapshotManager {
229
229
  patchProjectServiceReadFile() {
230
230
  const readFile = this.projectService.host.readFile;
231
231
  this.projectService.host.readFile = (path) => {
232
- if ((0, utils_1.isSvelteFilePath)(path) && this.configManager.getConfig().enable) {
232
+ // The following (very hacky) first two checks make sure that the ambient module definitions
233
+ // that tell TS "every import ending with .svelte is a valid module" are removed.
234
+ // They exist in svelte2tsx and svelte to make sure that people don't
235
+ // get errors in their TS files when importing Svelte files and not using our TS plugin.
236
+ // If someone wants to get back the behavior they can add an ambient module definition
237
+ // on their own.
238
+ const normalizedPath = path.replace(/\\/g, '/');
239
+ if (normalizedPath.endsWith('node_modules/svelte/types/runtime/ambient.d.ts')) {
240
+ return '';
241
+ }
242
+ else if (normalizedPath.endsWith('svelte2tsx/svelte-shims.d.ts')) {
243
+ let originalText = readFile(path) || '';
244
+ if (!originalText.includes('// -- start svelte-ls-remove --')) {
245
+ return originalText; // uses an older version of svelte2tsx
246
+ }
247
+ originalText =
248
+ originalText.substring(0, originalText.indexOf('// -- start svelte-ls-remove --')) +
249
+ originalText.substring(originalText.indexOf('// -- end svelte-ls-remove --'));
250
+ return originalText;
251
+ }
252
+ else if ((0, utils_1.isSvelteFilePath)(path) && this.configManager.getConfig().enable) {
233
253
  this.logger.debug('Read Svelte file:', path);
234
254
  const svelteCode = readFile(path) || '';
235
255
  try {
@@ -265,3 +285,4 @@ class SvelteSnapshotManager {
265
285
  }
266
286
  }
267
287
  exports.SvelteSnapshotManager = SvelteSnapshotManager;
288
+ //# sourceMappingURL=svelte-snapshots.js.map
@@ -32,3 +32,4 @@ function createSvelteSys(logger) {
32
32
  return svelteSys;
33
33
  }
34
34
  exports.createSvelteSys = createSvelteSys;
35
+ //# sourceMappingURL=svelte-sys.js.map
@@ -19,3 +19,5 @@ export declare function isNoTextSpanInGeneratedCode(text: string, span: ts.TextS
19
19
  */
20
20
  export declare function replaceDeep<T extends Record<string, any>>(obj: T, searchStr: string | RegExp, replacementStr: string): T;
21
21
  export declare function getConfigPathForProject(project: ts.server.Project): ts.server.NormalizedPath;
22
+ export declare function isStoreVariableIn$storeDeclaration(text: string, varStart: number): boolean;
23
+ export declare function get$storeOffsetOf$storeDeclaration(text: string, storePosition: number): number;
package/dist/src/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getConfigPathForProject = exports.replaceDeep = exports.isNoTextSpanInGeneratedCode = exports.isInGeneratedCode = exports.isNotNullOrUndefined = exports.ensureRealSvelteFilePath = exports.toRealSvelteFilePath = exports.isVirtualSvelteFilePath = exports.isSvelteFilePath = void 0;
3
+ 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
4
  function isSvelteFilePath(filePath) {
5
5
  return filePath.endsWith('.svelte');
6
6
  }
@@ -68,3 +68,13 @@ function getConfigPathForProject(project) {
68
68
  return ((_a = project.canonicalConfigFilePath) !== null && _a !== void 0 ? _a : project.getCompilerOptions().configFilePath);
69
69
  }
70
70
  exports.getConfigPathForProject = getConfigPathForProject;
71
+ function isStoreVariableIn$storeDeclaration(text, varStart) {
72
+ return (text.lastIndexOf('__sveltets_1_store_get(', varStart) ===
73
+ varStart - '__sveltets_1_store_get('.length);
74
+ }
75
+ exports.isStoreVariableIn$storeDeclaration = isStoreVariableIn$storeDeclaration;
76
+ function get$storeOffsetOf$storeDeclaration(text, storePosition) {
77
+ return text.lastIndexOf(' =', storePosition) - 1;
78
+ }
79
+ exports.get$storeOffsetOf$storeDeclaration = get$storeOffsetOf$storeDeclaration;
80
+ //# sourceMappingURL=utils.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-svelte-plugin",
3
- "version": "0.3.1",
3
+ "version": "0.3.4",
4
4
  "description": "A TypeScript Plugin providing Svelte intellisense",
5
5
  "main": "dist/src/index.js",
6
6
  "scripts": {