typescript-svelte-plugin
Advanced tools
Comparing version 0.3.11 to 0.3.12
@@ -231,64 +231,81 @@ "use strict"; | ||
patchProjectServiceReadFile() { | ||
const readFile = this.projectService.host.readFile; | ||
this.projectService.host.readFile = (path, encoding) => { | ||
if (!this.configManager.getConfig().enable) { | ||
return readFile(path, encoding); | ||
} | ||
// The following (very hacky) first two checks make sure that the ambient module definitions | ||
// that tell TS "every import ending with .svelte is a valid module" are removed. | ||
// They exist in svelte2tsx and svelte to make sure that people don't | ||
// get errors in their TS files when importing Svelte files and not using our TS plugin. | ||
// If someone wants to get back the behavior they can add an ambient module definition | ||
// on their own. | ||
const normalizedPath = path.replace(/\\/g, '/'); | ||
if (normalizedPath.endsWith('node_modules/svelte/types/runtime/ambient.d.ts')) { | ||
return ''; | ||
} | ||
else if (normalizedPath.endsWith('svelte2tsx/svelte-shims.d.ts')) { | ||
let originalText = readFile(path) || ''; | ||
if (!originalText.includes('// -- start svelte-ls-remove --')) { | ||
return originalText; // uses an older version of svelte2tsx | ||
// @ts-ignore The projectService is shared across some instances, make sure we patch readFile only once | ||
if (!this.projectService.host[onReadSvelteFile]) { | ||
this.logger.log('patching projectService host readFile'); | ||
// @ts-ignore | ||
this.projectService.host[onReadSvelteFile] = []; | ||
const readFile = this.projectService.host.readFile; | ||
this.projectService.host.readFile = (path, encoding) => { | ||
if (!this.configManager.getConfig().enable) { | ||
return readFile(path, encoding); | ||
} | ||
originalText = | ||
originalText.substring(0, originalText.indexOf('// -- start svelte-ls-remove --')) + | ||
originalText.substring(originalText.indexOf('// -- end svelte-ls-remove --')); | ||
return originalText; | ||
} | ||
else if ((0, utils_1.isSvelteFilePath)(path)) { | ||
this.logger.debug('Read Svelte file:', path); | ||
const svelteCode = readFile(path) || ''; | ||
try { | ||
// The following (very hacky) first two checks make sure that the ambient module definitions | ||
// that tell TS "every import ending with .svelte is a valid module" are removed. | ||
// They exist in svelte2tsx and svelte to make sure that people don't | ||
// get errors in their TS files when importing Svelte files and not using our TS plugin. | ||
// If someone wants to get back the behavior they can add an ambient module definition | ||
// on their own. | ||
const normalizedPath = path.replace(/\\/g, '/'); | ||
if (normalizedPath.endsWith('node_modules/svelte/types/runtime/ambient.d.ts')) { | ||
return ''; | ||
} | ||
else if (normalizedPath.endsWith('svelte2tsx/svelte-shims.d.ts')) { | ||
let originalText = readFile(path) || ''; | ||
if (!originalText.includes('// -- start svelte-ls-remove --')) { | ||
return originalText; // uses an older version of svelte2tsx or is already patched | ||
} | ||
originalText = | ||
originalText.substring(0, originalText.indexOf('// -- start svelte-ls-remove --')) + | ||
originalText.substring(originalText.indexOf('// -- end svelte-ls-remove --')); | ||
return originalText; | ||
} | ||
else if ((0, utils_1.isSvelteFilePath)(path)) { | ||
this.logger.debug('Read Svelte file:', path); | ||
const svelteCode = readFile(path) || ''; | ||
const isTsFile = true; // TODO check file contents? TS might be okay with importing ts into js. | ||
const result = (0, svelte2tsx_1.svelte2tsx)(svelteCode, { | ||
filename: path.split('/').pop(), | ||
isTsFile, | ||
mode: 'ts', | ||
typingsNamespace: this.svelteOptions.namespace | ||
}); | ||
const canonicalFilePath = this.projectService.toCanonicalFileName(path); | ||
const existingSnapshot = this.snapshots.get(canonicalFilePath); | ||
if (existingSnapshot) { | ||
existingSnapshot.update(svelteCode, new source_mapper_1.SourceMapper(result.map.mappings)); | ||
let code; | ||
let mapper; | ||
try { | ||
const result = (0, svelte2tsx_1.svelte2tsx)(svelteCode, { | ||
filename: path.split('/').pop(), | ||
isTsFile, | ||
mode: 'ts', | ||
typingsNamespace: this.svelteOptions.namespace | ||
}); | ||
code = result.code; | ||
mapper = new source_mapper_1.SourceMapper(result.map.mappings); | ||
this.logger.log('Successfully read Svelte file contents of', path); | ||
} | ||
else { | ||
this.snapshots.set(canonicalFilePath, new SvelteSnapshot(this.typescript, path, svelteCode, new source_mapper_1.SourceMapper(result.map.mappings), this.logger, isTsFile)); | ||
catch (e) { | ||
this.logger.log('Error loading Svelte file:', path, ' Using fallback.'); | ||
this.logger.debug('Error:', e); | ||
// Return something either way, else "X is not a module" errors will appear | ||
// in the TS files that use this file. | ||
code = 'export default class extends Svelte2TsxComponent<any,any,any> {}'; | ||
mapper = new source_mapper_1.SourceMapper(''); | ||
} | ||
this.logger.log('Successfully read Svelte file contents of', path); | ||
return result.code; | ||
// @ts-ignore | ||
this.projectService.host[onReadSvelteFile].forEach((listener) => listener(path, code, isTsFile, mapper)); | ||
return code; | ||
} | ||
catch (e) { | ||
this.logger.log('Error loading Svelte file:', path, ' Using fallback.'); | ||
this.logger.debug('Error:', e); | ||
// Return something either way, else "X is not a module" errors will appear | ||
// in the TS files that use this file. | ||
return 'export default class extends Svelte2TsxComponent<any,any,any> {}'; | ||
else { | ||
return readFile(path, encoding); | ||
} | ||
}; | ||
} | ||
// @ts-ignore | ||
this.projectService.host[onReadSvelteFile].push((path, svelteCode, isTsFile, mapper) => { | ||
const canonicalFilePath = this.projectService.toCanonicalFileName(path); | ||
const existingSnapshot = this.snapshots.get(canonicalFilePath); | ||
if (existingSnapshot) { | ||
existingSnapshot.update(svelteCode, mapper); | ||
} | ||
else { | ||
return readFile(path, encoding); | ||
this.snapshots.set(canonicalFilePath, new SvelteSnapshot(this.typescript, path, svelteCode, mapper, this.logger, isTsFile)); | ||
} | ||
}; | ||
}); | ||
} | ||
} | ||
exports.SvelteSnapshotManager = SvelteSnapshotManager; | ||
const onReadSvelteFile = Symbol('sveltePluginPatchSymbol'); | ||
//# sourceMappingURL=svelte-snapshots.js.map |
{ | ||
"name": "typescript-svelte-plugin", | ||
"version": "0.3.11", | ||
"version": "0.3.12", | ||
"description": "A TypeScript Plugin providing Svelte intellisense", | ||
@@ -5,0 +5,0 @@ "main": "dist/src/index.js", |
73884
1608