typescript-svelte-plugin 0.3.11 → 0.3.12

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.
@@ -228,66 +228,83 @@ class SvelteSnapshotManager {
228
228
  return snapshot;
229
229
  }
230
230
  patchProjectServiceReadFile() {
231
- const readFile = this.projectService.host.readFile;
232
- this.projectService.host.readFile = (path, encoding) => {
233
- if (!this.configManager.getConfig().enable) {
234
- return readFile(path, encoding);
235
- }
236
- // The following (very hacky) first two checks make sure that the ambient module definitions
237
- // that tell TS "every import ending with .svelte is a valid module" are removed.
238
- // They exist in svelte2tsx and svelte to make sure that people don't
239
- // get errors in their TS files when importing Svelte files and not using our TS plugin.
240
- // If someone wants to get back the behavior they can add an ambient module definition
241
- // on their own.
242
- const normalizedPath = path.replace(/\\/g, '/');
243
- if (normalizedPath.endsWith('node_modules/svelte/types/runtime/ambient.d.ts')) {
244
- return '';
245
- }
246
- else if (normalizedPath.endsWith('svelte2tsx/svelte-shims.d.ts')) {
247
- let originalText = readFile(path) || '';
248
- if (!originalText.includes('// -- start svelte-ls-remove --')) {
249
- return originalText; // uses an older version of svelte2tsx
231
+ // @ts-ignore The projectService is shared across some instances, make sure we patch readFile only once
232
+ if (!this.projectService.host[onReadSvelteFile]) {
233
+ this.logger.log('patching projectService host readFile');
234
+ // @ts-ignore
235
+ this.projectService.host[onReadSvelteFile] = [];
236
+ const readFile = this.projectService.host.readFile;
237
+ this.projectService.host.readFile = (path, encoding) => {
238
+ if (!this.configManager.getConfig().enable) {
239
+ return readFile(path, encoding);
250
240
  }
251
- originalText =
252
- originalText.substring(0, originalText.indexOf('// -- start svelte-ls-remove --')) +
253
- originalText.substring(originalText.indexOf('// -- end svelte-ls-remove --'));
254
- return originalText;
255
- }
256
- else if ((0, utils_1.isSvelteFilePath)(path)) {
257
- this.logger.debug('Read Svelte file:', path);
258
- const svelteCode = readFile(path) || '';
259
- try {
241
+ // The following (very hacky) first two checks make sure that the ambient module definitions
242
+ // that tell TS "every import ending with .svelte is a valid module" are removed.
243
+ // They exist in svelte2tsx and svelte to make sure that people don't
244
+ // get errors in their TS files when importing Svelte files and not using our TS plugin.
245
+ // If someone wants to get back the behavior they can add an ambient module definition
246
+ // on their own.
247
+ const normalizedPath = path.replace(/\\/g, '/');
248
+ if (normalizedPath.endsWith('node_modules/svelte/types/runtime/ambient.d.ts')) {
249
+ return '';
250
+ }
251
+ else if (normalizedPath.endsWith('svelte2tsx/svelte-shims.d.ts')) {
252
+ let originalText = readFile(path) || '';
253
+ if (!originalText.includes('// -- start svelte-ls-remove --')) {
254
+ return originalText; // uses an older version of svelte2tsx or is already patched
255
+ }
256
+ originalText =
257
+ originalText.substring(0, originalText.indexOf('// -- start svelte-ls-remove --')) +
258
+ originalText.substring(originalText.indexOf('// -- end svelte-ls-remove --'));
259
+ return originalText;
260
+ }
261
+ else if ((0, utils_1.isSvelteFilePath)(path)) {
262
+ this.logger.debug('Read Svelte file:', path);
263
+ const svelteCode = readFile(path) || '';
260
264
  const isTsFile = true; // TODO check file contents? TS might be okay with importing ts into js.
261
- const result = (0, svelte2tsx_1.svelte2tsx)(svelteCode, {
262
- filename: path.split('/').pop(),
263
- isTsFile,
264
- mode: 'ts',
265
- typingsNamespace: this.svelteOptions.namespace
266
- });
267
- const canonicalFilePath = this.projectService.toCanonicalFileName(path);
268
- const existingSnapshot = this.snapshots.get(canonicalFilePath);
269
- if (existingSnapshot) {
270
- existingSnapshot.update(svelteCode, new source_mapper_1.SourceMapper(result.map.mappings));
265
+ let code;
266
+ let mapper;
267
+ try {
268
+ const result = (0, svelte2tsx_1.svelte2tsx)(svelteCode, {
269
+ filename: path.split('/').pop(),
270
+ isTsFile,
271
+ mode: 'ts',
272
+ typingsNamespace: this.svelteOptions.namespace
273
+ });
274
+ code = result.code;
275
+ mapper = new source_mapper_1.SourceMapper(result.map.mappings);
276
+ this.logger.log('Successfully read Svelte file contents of', path);
271
277
  }
272
- else {
273
- this.snapshots.set(canonicalFilePath, new SvelteSnapshot(this.typescript, path, svelteCode, new source_mapper_1.SourceMapper(result.map.mappings), this.logger, isTsFile));
278
+ catch (e) {
279
+ this.logger.log('Error loading Svelte file:', path, ' Using fallback.');
280
+ this.logger.debug('Error:', e);
281
+ // Return something either way, else "X is not a module" errors will appear
282
+ // in the TS files that use this file.
283
+ code = 'export default class extends Svelte2TsxComponent<any,any,any> {}';
284
+ mapper = new source_mapper_1.SourceMapper('');
274
285
  }
275
- this.logger.log('Successfully read Svelte file contents of', path);
276
- return result.code;
286
+ // @ts-ignore
287
+ this.projectService.host[onReadSvelteFile].forEach((listener) => listener(path, code, isTsFile, mapper));
288
+ return code;
277
289
  }
278
- catch (e) {
279
- this.logger.log('Error loading Svelte file:', path, ' Using fallback.');
280
- this.logger.debug('Error:', e);
281
- // Return something either way, else "X is not a module" errors will appear
282
- // in the TS files that use this file.
283
- return 'export default class extends Svelte2TsxComponent<any,any,any> {}';
290
+ else {
291
+ return readFile(path, encoding);
284
292
  }
293
+ };
294
+ }
295
+ // @ts-ignore
296
+ this.projectService.host[onReadSvelteFile].push((path, svelteCode, isTsFile, mapper) => {
297
+ const canonicalFilePath = this.projectService.toCanonicalFileName(path);
298
+ const existingSnapshot = this.snapshots.get(canonicalFilePath);
299
+ if (existingSnapshot) {
300
+ existingSnapshot.update(svelteCode, mapper);
285
301
  }
286
302
  else {
287
- return readFile(path, encoding);
303
+ this.snapshots.set(canonicalFilePath, new SvelteSnapshot(this.typescript, path, svelteCode, mapper, this.logger, isTsFile));
288
304
  }
289
- };
305
+ });
290
306
  }
291
307
  }
292
308
  exports.SvelteSnapshotManager = SvelteSnapshotManager;
309
+ const onReadSvelteFile = Symbol('sveltePluginPatchSymbol');
293
310
  //# sourceMappingURL=svelte-snapshots.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-svelte-plugin",
3
- "version": "0.3.11",
3
+ "version": "0.3.12",
4
4
  "description": "A TypeScript Plugin providing Svelte intellisense",
5
5
  "main": "dist/src/index.js",
6
6
  "scripts": {