typescript-svelte-plugin 0.3.4 → 0.3.7

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.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.decorateCompletions = void 0;
4
+ const path_1 = require("path");
4
5
  const utils_1 = require("../utils");
5
6
  const componentPostfix = '__SvelteComponent_';
6
7
  function decorateCompletions(ls, logger) {
@@ -26,9 +27,49 @@ function decorateCompletions(ls, logger) {
26
27
  };
27
28
  const getCompletionEntryDetails = ls.getCompletionEntryDetails;
28
29
  ls.getCompletionEntryDetails = (fileName, position, entryName, formatOptions, source, preferences, data) => {
29
- const details = getCompletionEntryDetails(fileName, position, entryName, formatOptions, source, preferences, data);
30
+ var _a, _b, _c, _d;
31
+ let is$typeImport = false;
32
+ const originalSource = source;
33
+ const originalData = data ? { ...data } : undefined;
34
+ if ((0, path_1.basename)(fileName).startsWith('+') && (source === null || source === void 0 ? void 0 : source.includes('.svelte-kit/types'))) {
35
+ // resolve path from FileName to svelte-kit/types
36
+ // src/routes/foo/+page.svelte -> .svelte-kit/types/foo/$types.d.ts
37
+ const routesFolder = 'src/routes'; // TODO somehow get access to kit.files.routes in here
38
+ const relativeFileName = (_a = fileName.split(routesFolder)[1]) === null || _a === void 0 ? void 0 : _a.slice(1);
39
+ if (relativeFileName) {
40
+ is$typeImport = true;
41
+ source =
42
+ source.split('.svelte-kit/types')[0] +
43
+ // note the missing .d.ts at the end - TS wants it that way for some reason
44
+ `.svelte-kit/types/${routesFolder}/${(0, path_1.dirname)(relativeFileName)}/$types`;
45
+ if (data) {
46
+ data.fileName = (_b = data.fileName) === null || _b === void 0 ? void 0 : _b.replace(originalSource, source);
47
+ data.moduleSpecifier = (_c = data.moduleSpecifier) === null || _c === void 0 ? void 0 : _c.replace(originalSource, source);
48
+ }
49
+ }
50
+ }
51
+ let details = getCompletionEntryDetails(fileName, position, entryName, formatOptions, source, preferences, data);
52
+ if (!details && is$typeImport) {
53
+ // Try again
54
+ is$typeImport = false;
55
+ details = getCompletionEntryDetails(fileName, position, entryName, formatOptions, originalSource, preferences, originalData);
56
+ }
30
57
  if (details) {
31
- if ((0, utils_1.isSvelteFilePath)(source || '')) {
58
+ if (is$typeImport) {
59
+ details.codeActions = (_d = details.codeActions) === null || _d === void 0 ? void 0 : _d.map((codeAction) => {
60
+ codeAction.description = adjustPath(codeAction.description);
61
+ codeAction.changes = codeAction.changes.map((change) => {
62
+ change.textChanges = change.textChanges.map((textChange) => {
63
+ textChange.newText = adjustPath(textChange.newText);
64
+ return textChange;
65
+ });
66
+ return change;
67
+ });
68
+ return codeAction;
69
+ });
70
+ return details;
71
+ }
72
+ else if ((0, utils_1.isSvelteFilePath)(source || '')) {
32
73
  logger.debug('TS found Svelte Component import completion details');
33
74
  return (0, utils_1.replaceDeep)(details, componentPostfix, '');
34
75
  }
@@ -51,4 +92,9 @@ function decorateCompletions(ls, logger) {
51
92
  };
52
93
  }
53
94
  exports.decorateCompletions = decorateCompletions;
95
+ function adjustPath(path) {
96
+ return path.replace(/(['"])(.+?)['"]/,
97
+ // .js logic for node16 module resolution
98
+ (_match, quote, path) => `${quote}./$types${path.endsWith('.js') ? '.js' : ''}${quote}`);
99
+ }
54
100
  //# sourceMappingURL=completions.js.map
@@ -36,6 +36,7 @@ function decorateRename(ls, snapshotManager, logger) {
36
36
  }
37
37
  // |$store| would be renamed, make it $|store|
38
38
  textSpan.start += 1;
39
+ textSpan.length -= 1;
39
40
  convertedRenameLocations.push(convert(renameLocation, textSpan));
40
41
  }
41
42
  return convertedRenameLocations;
@@ -228,7 +228,10 @@ class SvelteSnapshotManager {
228
228
  }
229
229
  patchProjectServiceReadFile() {
230
230
  const readFile = this.projectService.host.readFile;
231
- this.projectService.host.readFile = (path) => {
231
+ this.projectService.host.readFile = (path, encoding) => {
232
+ if (!this.configManager.getConfig().enable) {
233
+ return readFile(path, encoding);
234
+ }
232
235
  // The following (very hacky) first two checks make sure that the ambient module definitions
233
236
  // that tell TS "every import ending with .svelte is a valid module" are removed.
234
237
  // They exist in svelte2tsx and svelte to make sure that people don't
@@ -249,7 +252,7 @@ class SvelteSnapshotManager {
249
252
  originalText.substring(originalText.indexOf('// -- end svelte-ls-remove --'));
250
253
  return originalText;
251
254
  }
252
- else if ((0, utils_1.isSvelteFilePath)(path) && this.configManager.getConfig().enable) {
255
+ else if ((0, utils_1.isSvelteFilePath)(path)) {
253
256
  this.logger.debug('Read Svelte file:', path);
254
257
  const svelteCode = readFile(path) || '';
255
258
  try {
@@ -279,7 +282,7 @@ class SvelteSnapshotManager {
279
282
  }
280
283
  }
281
284
  else {
282
- return readFile(path);
285
+ return readFile(path, encoding);
283
286
  }
284
287
  };
285
288
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-svelte-plugin",
3
- "version": "0.3.4",
3
+ "version": "0.3.7",
4
4
  "description": "A TypeScript Plugin providing Svelte intellisense",
5
5
  "main": "dist/src/index.js",
6
6
  "scripts": {