typescript-svelte-plugin 0.3.5 → 0.3.8

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,15 +1,51 @@
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) {
7
8
  const getCompletionsAtPosition = ls.getCompletionsAtPosition;
8
9
  ls.getCompletionsAtPosition = (fileName, position, options) => {
10
+ var _a, _b, _c, _d;
9
11
  const completions = getCompletionsAtPosition(fileName, position, options);
10
12
  if (!completions) {
11
13
  return completions;
12
14
  }
15
+ // Add ./$types imports for SvelteKit since TypeScript is bad at it
16
+ if ((0, path_1.basename)(fileName).startsWith('+')) {
17
+ const $typeImports = new Map();
18
+ for (const c of completions.entries) {
19
+ if (((_a = c.source) === null || _a === void 0 ? void 0 : _a.includes('.svelte-kit/types')) && c.data) {
20
+ $typeImports.set(c.name, c);
21
+ }
22
+ }
23
+ for (const $typeImport of $typeImports.values()) {
24
+ // resolve path from FileName to svelte-kit/types
25
+ // src/routes/foo/+page.svelte -> .svelte-kit/types/foo/$types.d.ts
26
+ const routesFolder = 'src/routes'; // TODO somehow get access to kit.files.routes in here
27
+ const relativeFileName = (_b = fileName.split(routesFolder)[1]) === null || _b === void 0 ? void 0 : _b.slice(1);
28
+ if (relativeFileName) {
29
+ const modifiedSource = $typeImport.source.split('.svelte-kit/types')[0] +
30
+ // note the missing .d.ts at the end - TS wants it that way for some reason
31
+ `.svelte-kit/types/${routesFolder}/${(0, path_1.dirname)(relativeFileName)}/$types`;
32
+ completions.entries.push({
33
+ ...$typeImport,
34
+ // Ensure it's sorted above the other imports
35
+ sortText: !isNaN(Number($typeImport.sortText))
36
+ ? String(Number($typeImport.sortText) - 1)
37
+ : $typeImport.sortText,
38
+ source: modifiedSource,
39
+ data: {
40
+ ...$typeImport.data,
41
+ fileName: (_c = $typeImport.data.fileName) === null || _c === void 0 ? void 0 : _c.replace($typeImport.source, modifiedSource),
42
+ moduleSpecifier: (_d = $typeImport.data.moduleSpecifier) === null || _d === void 0 ? void 0 : _d.replace($typeImport.source, modifiedSource),
43
+ __is_sveltekit$typeImport: true
44
+ }
45
+ });
46
+ }
47
+ }
48
+ }
13
49
  return {
14
50
  ...completions,
15
51
  entries: completions.entries.map((entry) => {
@@ -26,9 +62,25 @@ function decorateCompletions(ls, logger) {
26
62
  };
27
63
  const getCompletionEntryDetails = ls.getCompletionEntryDetails;
28
64
  ls.getCompletionEntryDetails = (fileName, position, entryName, formatOptions, source, preferences, data) => {
65
+ var _a;
66
+ const is$typeImport = data.__is_sveltekit$typeImport;
29
67
  const details = getCompletionEntryDetails(fileName, position, entryName, formatOptions, source, preferences, data);
30
68
  if (details) {
31
- if ((0, utils_1.isSvelteFilePath)(source || '')) {
69
+ if (is$typeImport) {
70
+ details.codeActions = (_a = details.codeActions) === null || _a === void 0 ? void 0 : _a.map((codeAction) => {
71
+ codeAction.description = adjustPath(codeAction.description);
72
+ codeAction.changes = codeAction.changes.map((change) => {
73
+ change.textChanges = change.textChanges.map((textChange) => {
74
+ textChange.newText = adjustPath(textChange.newText);
75
+ return textChange;
76
+ });
77
+ return change;
78
+ });
79
+ return codeAction;
80
+ });
81
+ return details;
82
+ }
83
+ else if ((0, utils_1.isSvelteFilePath)(source || '')) {
32
84
  logger.debug('TS found Svelte Component import completion details');
33
85
  return (0, utils_1.replaceDeep)(details, componentPostfix, '');
34
86
  }
@@ -51,4 +103,9 @@ function decorateCompletions(ls, logger) {
51
103
  };
52
104
  }
53
105
  exports.decorateCompletions = decorateCompletions;
106
+ function adjustPath(path) {
107
+ return path.replace(/(['"])(.+?)['"]/,
108
+ // .js logic for node16 module resolution
109
+ (_match, quote, path) => `${quote}./$types${path.endsWith('.js') ? '.js' : ''}${quote}`);
110
+ }
54
111
  //# sourceMappingURL=completions.js.map
@@ -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.5",
3
+ "version": "0.3.8",
4
4
  "description": "A TypeScript Plugin providing Svelte intellisense",
5
5
  "main": "dist/src/index.js",
6
6
  "scripts": {