typescript-svelte-plugin 0.2.2 → 0.3.0

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.
package/dist/src/index.js CHANGED
@@ -6,48 +6,45 @@ const module_loader_1 = require("./module-loader");
6
6
  const svelte_snapshots_1 = require("./svelte-snapshots");
7
7
  function init(modules) {
8
8
  function create(info) {
9
+ var _a, _b, _c, _d;
9
10
  const logger = new logger_1.Logger(info.project.projectService.logger);
10
- if (!isSvelteProject(info)) {
11
+ if (!isSvelteProject(info.project.getCompilerOptions())) {
11
12
  logger.log('Detected that this is not a Svelte project, abort patching TypeScript');
12
13
  return info.languageService;
13
14
  }
14
15
  logger.log('Starting Svelte plugin');
15
- const snapshotManager = new svelte_snapshots_1.SvelteSnapshotManager(modules.typescript, info.project.projectService, logger);
16
- patchCompilerOptions(info.project);
17
- module_loader_1.patchModuleLoader(logger, snapshotManager, modules.typescript, info.languageServiceHost, info.project);
18
- return language_service_1.decorateLanguageService(info.languageService, snapshotManager, logger);
16
+ // If someone knows a better/more performant way to get svelteOptions,
17
+ // please tell us :)
18
+ const svelteOptions = ((_d = (_c = (_b = (_a = info.languageServiceHost).getParsedCommandLine) === null || _b === void 0 ? void 0 : _b.call(_a, info.project.getCompilerOptions().configFilePath)) === null || _c === void 0 ? void 0 : _c.raw) === null || _d === void 0 ? void 0 : _d.svelteOptions) || { namespace: 'svelteHTML' };
19
+ logger.log('svelteOptions:', svelteOptions);
20
+ const snapshotManager = new svelte_snapshots_1.SvelteSnapshotManager(modules.typescript, info.project.projectService, svelteOptions, logger);
21
+ (0, module_loader_1.patchModuleLoader)(logger, snapshotManager, modules.typescript, info.languageServiceHost, info.project);
22
+ return (0, language_service_1.decorateLanguageService)(info.languageService, snapshotManager, logger);
19
23
  }
20
24
  function getExternalFiles(project) {
25
+ if (!isSvelteProject(project.getCompilerOptions())) {
26
+ return [];
27
+ }
21
28
  // Needed so the ambient definitions are known inside the tsx files
22
- const svelteTsPath = path_1.dirname(require.resolve('svelte2tsx'));
29
+ const svelteTsPath = (0, path_1.dirname)(require.resolve('svelte2tsx'));
23
30
  const svelteTsxFiles = [
24
31
  './svelte-shims.d.ts',
25
32
  './svelte-jsx.d.ts',
26
33
  './svelte-native-jsx.d.ts'
27
- ].map((f) => modules.typescript.sys.resolvePath(path_1.resolve(svelteTsPath, f)));
34
+ ].map((f) => modules.typescript.sys.resolvePath((0, path_1.resolve)(svelteTsPath, f)));
28
35
  return svelteTsxFiles;
29
36
  }
30
- function patchCompilerOptions(project) {
31
- var _a;
32
- const compilerOptions = project.getCompilerOptions();
33
- // Patch needed because svelte2tsx creates jsx/tsx files
34
- compilerOptions.jsx = modules.typescript.JsxEmit.Preserve;
35
- // detect which JSX namespace to use (svelte | svelteNative) if not specified or not compatible
36
- if (!((_a = compilerOptions.jsxFactory) === null || _a === void 0 ? void 0 : _a.startsWith('svelte'))) {
37
- // Default to regular svelte, this causes the usage of the "svelte.JSX" namespace
38
- // We don't need to add a switch for svelte-native because the jsx is only relevant
39
- // within Svelte files, which this plugin does not deal with.
40
- compilerOptions.jsxFactory = 'svelte.createElement';
41
- }
42
- }
43
- function isSvelteProject(info) {
37
+ function isSvelteProject(compilerOptions) {
44
38
  // Add more checks like "no Svelte file found" or "no config file found"?
45
- const compilerOptions = info.project.getCompilerOptions();
46
- const isNoJsxProject = (!compilerOptions.jsx || compilerOptions.jsx === modules.typescript.JsxEmit.Preserve) &&
47
- (!compilerOptions.jsxFactory || compilerOptions.jsxFactory.startsWith('svelte')) &&
48
- !compilerOptions.jsxFragmentFactory &&
49
- !compilerOptions.jsxImportSource;
50
- return isNoJsxProject;
39
+ try {
40
+ const isSvelteProject = typeof compilerOptions.configFilePath !== 'string' ||
41
+ require.resolve('svelte', { paths: [compilerOptions.configFilePath] });
42
+ return isSvelteProject;
43
+ }
44
+ catch (e) {
45
+ // If require.resolve fails, we end up here
46
+ return false;
47
+ }
51
48
  }
52
49
  return { create, getExternalFiles };
53
50
  }
@@ -13,7 +13,7 @@ function decorateCompletions(ls, logger) {
13
13
  return {
14
14
  ...completions,
15
15
  entries: completions.entries.map((entry) => {
16
- if (!utils_1.isSvelteFilePath(entry.source || '') ||
16
+ if (!(0, utils_1.isSvelteFilePath)(entry.source || '') ||
17
17
  !entry.name.endsWith(componentPostfix)) {
18
18
  return entry;
19
19
  }
@@ -27,7 +27,7 @@ function decorateCompletions(ls, logger) {
27
27
  const getCompletionEntryDetails = ls.getCompletionEntryDetails;
28
28
  ls.getCompletionEntryDetails = (fileName, position, entryName, formatOptions, source, preferences, data) => {
29
29
  const details = getCompletionEntryDetails(fileName, position, entryName, formatOptions, source, preferences, data);
30
- if (details || !utils_1.isSvelteFilePath(source || '')) {
30
+ if (details || !(0, utils_1.isSvelteFilePath)(source || '')) {
31
31
  return details;
32
32
  }
33
33
  // In the completion list we removed the component postfix. Internally,
@@ -38,7 +38,7 @@ function decorateCompletions(ls, logger) {
38
38
  return undefined;
39
39
  }
40
40
  logger.debug('Found Svelte Component import completion details');
41
- return utils_1.replaceDeep(svelteDetails, componentPostfix, '');
41
+ return (0, utils_1.replaceDeep)(svelteDetails, componentPostfix, '');
42
42
  };
43
43
  }
44
44
  exports.decorateCompletions = decorateCompletions;
@@ -14,7 +14,7 @@ function decorateGetDefinition(ls, snapshotManager, logger) {
14
14
  definitions: definition.definitions
15
15
  .map((def) => {
16
16
  var _a;
17
- if (!utils_1.isSvelteFilePath(def.fileName)) {
17
+ if (!(0, utils_1.isSvelteFilePath)(def.fileName)) {
18
18
  return def;
19
19
  }
20
20
  let textSpan = (_a = snapshotManager
@@ -13,7 +13,7 @@ function decorateSyntacticDiagnostics(ls) {
13
13
  ls.getSyntacticDiagnostics = (fileName) => {
14
14
  // Diagnostics inside Svelte files are done
15
15
  // by the svelte-language-server / Svelte for VS Code extension
16
- if (utils_1.isSvelteFilePath(fileName)) {
16
+ if ((0, utils_1.isSvelteFilePath)(fileName)) {
17
17
  return [];
18
18
  }
19
19
  return getSyntacticDiagnostics(fileName);
@@ -24,7 +24,7 @@ function decorateSemanticDiagnostics(ls) {
24
24
  ls.getSemanticDiagnostics = (fileName) => {
25
25
  // Diagnostics inside Svelte files are done
26
26
  // by the svelte-language-server / Svelte for VS Code extension
27
- if (utils_1.isSvelteFilePath(fileName)) {
27
+ if ((0, utils_1.isSvelteFilePath)(fileName)) {
28
28
  return [];
29
29
  }
30
30
  return getSemanticDiagnostics(fileName);
@@ -35,7 +35,7 @@ function decorateSuggestionDiagnostics(ls) {
35
35
  ls.getSuggestionDiagnostics = (fileName) => {
36
36
  // Diagnostics inside Svelte files are done
37
37
  // by the svelte-language-server / Svelte for VS Code extension
38
- if (utils_1.isSvelteFilePath(fileName)) {
38
+ if ((0, utils_1.isSvelteFilePath)(fileName)) {
39
39
  return [];
40
40
  }
41
41
  return getSuggestionDiagnostics(fileName);
@@ -13,7 +13,7 @@ function _decorateFindReferences(ls, snapshotManager, logger) {
13
13
  const references = findReferences(fileName, position);
14
14
  return references === null || references === void 0 ? void 0 : references.map((reference) => {
15
15
  const snapshot = snapshotManager.get(reference.definition.fileName);
16
- if (!utils_1.isSvelteFilePath(reference.definition.fileName) || !snapshot) {
16
+ if (!(0, utils_1.isSvelteFilePath)(reference.definition.fileName) || !snapshot) {
17
17
  return reference;
18
18
  }
19
19
  const textSpan = snapshot.getOriginalTextSpan(reference.definition.textSpan);
@@ -43,7 +43,7 @@ function mapReferences(references, snapshotManager, logger) {
43
43
  return references
44
44
  .map((reference) => {
45
45
  const snapshot = snapshotManager.get(reference.fileName);
46
- if (!utils_1.isSvelteFilePath(reference.fileName) || !snapshot) {
46
+ if (!(0, utils_1.isSvelteFilePath)(reference.fileName) || !snapshot) {
47
47
  return reference;
48
48
  }
49
49
  const textSpan = snapshot.getOriginalTextSpan(reference.textSpan);
@@ -8,7 +8,7 @@ function decorateGetImplementation(ls, snapshotManager, logger) {
8
8
  const implementation = getImplementationAtPosition(fileName, position);
9
9
  return implementation === null || implementation === void 0 ? void 0 : implementation.map((impl) => {
10
10
  var _a;
11
- if (!utils_1.isSvelteFilePath(impl.fileName)) {
11
+ if (!(0, utils_1.isSvelteFilePath)(impl.fileName)) {
12
12
  return impl;
13
13
  }
14
14
  const textSpan = (_a = snapshotManager
@@ -10,12 +10,12 @@ const implementation_1 = require("./implementation");
10
10
  const rename_1 = require("./rename");
11
11
  function decorateLanguageService(ls, snapshotManager, logger) {
12
12
  patchLineColumnOffset(ls, snapshotManager);
13
- rename_1.decorateRename(ls, snapshotManager, logger);
14
- diagnostics_1.decorateDiagnostics(ls, logger);
15
- find_references_1.decorateFindReferences(ls, snapshotManager, logger);
16
- completions_1.decorateCompletions(ls, logger);
17
- definition_1.decorateGetDefinition(ls, snapshotManager, logger);
18
- implementation_1.decorateGetImplementation(ls, snapshotManager, logger);
13
+ (0, rename_1.decorateRename)(ls, snapshotManager, logger);
14
+ (0, diagnostics_1.decorateDiagnostics)(ls, logger);
15
+ (0, find_references_1.decorateFindReferences)(ls, snapshotManager, logger);
16
+ (0, completions_1.decorateCompletions)(ls, logger);
17
+ (0, definition_1.decorateGetDefinition)(ls, snapshotManager, logger);
18
+ (0, implementation_1.decorateGetImplementation)(ls, snapshotManager, logger);
19
19
  return ls;
20
20
  }
21
21
  exports.decorateLanguageService = decorateLanguageService;
@@ -26,7 +26,7 @@ function patchLineColumnOffset(ls, snapshotManager) {
26
26
  // We need to patch this because (according to source, only) getDefinition uses this
27
27
  const toLineColumnOffset = ls.toLineColumnOffset;
28
28
  ls.toLineColumnOffset = (fileName, position) => {
29
- if (utils_1.isSvelteFilePath(fileName)) {
29
+ if ((0, utils_1.isSvelteFilePath)(fileName)) {
30
30
  const snapshot = snapshotManager.get(fileName);
31
31
  if (snapshot) {
32
32
  return snapshot.positionAt(position);
@@ -8,7 +8,7 @@ function decorateRename(ls, snapshotManager, logger) {
8
8
  const renameLocations = findRenameLocations(fileName, position, findInStrings, findInComments, providePrefixAndSuffixTextForRename);
9
9
  return renameLocations === null || renameLocations === void 0 ? void 0 : renameLocations.map((renameLocation) => {
10
10
  const snapshot = snapshotManager.get(renameLocation.fileName);
11
- if (!utils_1.isSvelteFilePath(renameLocation.fileName) || !snapshot) {
11
+ if (!(0, utils_1.isSvelteFilePath)(renameLocation.fileName) || !snapshot) {
12
12
  return renameLocation;
13
13
  }
14
14
  // TODO more needed to filter invalid locations, see RenameProvider
@@ -37,7 +37,7 @@ class ModuleResolutionCache {
37
37
  });
38
38
  }
39
39
  getKey(moduleName, containingFile) {
40
- return containingFile + ':::' + utils_1.ensureRealSvelteFilePath(moduleName);
40
+ return containingFile + ':::' + (0, utils_1.ensureRealSvelteFilePath)(moduleName);
41
41
  }
42
42
  }
43
43
  /**
@@ -51,7 +51,7 @@ class ModuleResolutionCache {
51
51
  */
52
52
  function patchModuleLoader(logger, snapshotManager, typescript, lsHost, project) {
53
53
  var _a;
54
- const svelteSys = svelte_sys_1.createSvelteSys(logger);
54
+ const svelteSys = (0, svelte_sys_1.createSvelteSys)(logger);
55
55
  const moduleCache = new ModuleResolutionCache();
56
56
  const origResolveModuleNames = (_a = lsHost.resolveModuleNames) === null || _a === void 0 ? void 0 : _a.bind(lsHost);
57
57
  lsHost.resolveModuleNames = resolveModuleNames;
@@ -70,7 +70,7 @@ function patchModuleLoader(logger, snapshotManager, typescript, lsHost, project)
70
70
  const resolved = (origResolveModuleNames === null || origResolveModuleNames === void 0 ? void 0 : origResolveModuleNames(moduleNames, containingFile, reusedNames, redirectedReference, compilerOptions)) || Array.from(Array(moduleNames.length));
71
71
  return resolved.map((moduleName, idx) => {
72
72
  const fileName = moduleNames[idx];
73
- if (moduleName || !utils_1.ensureRealSvelteFilePath(fileName).endsWith('.svelte')) {
73
+ if (moduleName || !(0, utils_1.ensureRealSvelteFilePath)(fileName).endsWith('.svelte')) {
74
74
  return moduleName;
75
75
  }
76
76
  const cachedModule = moduleCache.get(fileName, containingFile);
@@ -85,17 +85,17 @@ function patchModuleLoader(logger, snapshotManager, typescript, lsHost, project)
85
85
  function resolveModuleName(name, containingFile, compilerOptions) {
86
86
  const svelteResolvedModule = typescript.resolveModuleName(name, containingFile, compilerOptions, svelteSys).resolvedModule;
87
87
  if (!svelteResolvedModule ||
88
- !utils_1.isVirtualSvelteFilePath(svelteResolvedModule.resolvedFileName)) {
88
+ !(0, utils_1.isVirtualSvelteFilePath)(svelteResolvedModule.resolvedFileName)) {
89
89
  return svelteResolvedModule;
90
90
  }
91
- const resolvedFileName = utils_1.ensureRealSvelteFilePath(svelteResolvedModule.resolvedFileName);
91
+ const resolvedFileName = (0, utils_1.ensureRealSvelteFilePath)(svelteResolvedModule.resolvedFileName);
92
92
  logger.log('Resolved', name, 'to Svelte file', resolvedFileName);
93
93
  const snapshot = snapshotManager.create(resolvedFileName);
94
94
  if (!snapshot) {
95
95
  return undefined;
96
96
  }
97
97
  const resolvedSvelteModule = {
98
- extension: snapshot.isTsFile ? typescript.Extension.Tsx : typescript.Extension.Jsx,
98
+ extension: snapshot.isTsFile ? typescript.Extension.Ts : typescript.Extension.Js,
99
99
  resolvedFileName
100
100
  };
101
101
  return resolvedSvelteModule;
@@ -3,41 +3,51 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SourceMapper = void 0;
4
4
  const sourcemap_codec_1 = require("sourcemap-codec");
5
5
  function binaryInsert(array, value, key) {
6
- if (0 === key)
6
+ if (0 === key) {
7
7
  key = '0';
8
+ }
8
9
  const index = 1 + binarySearch(array, (key ? value[key] : value), key);
9
10
  let i = array.length;
10
- while (index !== i--)
11
+ while (index !== i--) {
11
12
  array[1 + i] = array[i];
13
+ }
12
14
  array[index] = value;
13
15
  }
14
16
  function binarySearch(array, target, key) {
15
- if (!array || 0 === array.length)
17
+ if (!array || 0 === array.length) {
16
18
  return -1;
17
- if (0 === key)
19
+ }
20
+ if (0 === key) {
18
21
  key = '0';
22
+ }
19
23
  let low = 0;
20
24
  let high = array.length - 1;
21
25
  while (low <= high) {
22
26
  const i = low + ((high - low) >> 1);
23
27
  const item = undefined === key ? array[i] : array[i][key];
24
- if (item === target)
28
+ if (item === target) {
25
29
  return i;
26
- if (item < target)
30
+ }
31
+ if (item < target) {
27
32
  low = i + 1;
28
- else
33
+ }
34
+ else {
29
35
  high = i - 1;
36
+ }
30
37
  }
31
- if ((low = ~low) < 0)
38
+ if ((low = ~low) < 0) {
32
39
  low = ~low - 1;
40
+ }
33
41
  return low;
34
42
  }
35
43
  class SourceMapper {
36
44
  constructor(mappings) {
37
- if (typeof mappings === 'string')
38
- this.mappings = sourcemap_codec_1.decode(mappings);
39
- else
45
+ if (typeof mappings === 'string') {
46
+ this.mappings = (0, sourcemap_codec_1.decode)(mappings);
47
+ }
48
+ else {
40
49
  this.mappings = mappings;
50
+ }
41
51
  }
42
52
  getOriginalPosition(position) {
43
53
  const lineMap = this.mappings[position.line];
@@ -53,8 +63,9 @@ class SourceMapper {
53
63
  return { line, character };
54
64
  }
55
65
  getGeneratedPosition(position) {
56
- if (!this.reverseMappings)
66
+ if (!this.reverseMappings) {
57
67
  this.computeReversed();
68
+ }
58
69
  const lineMap = this.reverseMappings[position.line];
59
70
  if (!lineMap) {
60
71
  return { line: -1, character: -1 };
@@ -77,10 +88,12 @@ class SourceMapper {
77
88
  generated_line,
78
89
  generated_index
79
90
  ];
80
- if (original_line in this.reverseMappings)
91
+ if (original_line in this.reverseMappings) {
81
92
  binaryInsert(this.reverseMappings[original_line], reordered_char, 0);
82
- else
93
+ }
94
+ else {
83
95
  this.reverseMappings[original_line] = [reordered_char];
96
+ }
84
97
  }
85
98
  }
86
99
  }
@@ -36,9 +36,12 @@ export declare class SvelteSnapshot {
36
36
  export declare class SvelteSnapshotManager {
37
37
  private typescript;
38
38
  private projectService;
39
+ private svelteOptions;
39
40
  private logger;
40
41
  private snapshots;
41
- constructor(typescript: typeof ts, projectService: ts.server.ProjectService, logger: Logger);
42
+ constructor(typescript: typeof ts, projectService: ts.server.ProjectService, svelteOptions: {
43
+ namespace: string;
44
+ }, logger: Logger);
42
45
  get(fileName: string): SvelteSnapshot | undefined;
43
46
  create(fileName: string): SvelteSnapshot | undefined;
44
47
  private patchProjectServiceReadFile;
@@ -21,7 +21,7 @@ class SvelteSnapshot {
21
21
  this.log('Updated Snapshot');
22
22
  }
23
23
  getOriginalTextSpan(textSpan) {
24
- if (!utils_1.isNoTextSpanInGeneratedCode(this.getText(), textSpan)) {
24
+ if (!(0, utils_1.isNoTextSpanInGeneratedCode)(this.getText(), textSpan)) {
25
25
  return null;
26
26
  }
27
27
  const start = this.getOriginalOffset(textSpan.start);
@@ -189,9 +189,10 @@ class SvelteSnapshot {
189
189
  }
190
190
  exports.SvelteSnapshot = SvelteSnapshot;
191
191
  class SvelteSnapshotManager {
192
- constructor(typescript, projectService, logger) {
192
+ constructor(typescript, projectService, svelteOptions, logger) {
193
193
  this.typescript = typescript;
194
194
  this.projectService = projectService;
195
+ this.svelteOptions = svelteOptions;
195
196
  this.logger = logger;
196
197
  this.snapshots = new Map();
197
198
  this.patchProjectServiceReadFile();
@@ -227,14 +228,16 @@ class SvelteSnapshotManager {
227
228
  patchProjectServiceReadFile() {
228
229
  const readFile = this.projectService.host.readFile;
229
230
  this.projectService.host.readFile = (path) => {
230
- if (utils_1.isSvelteFilePath(path)) {
231
+ if ((0, utils_1.isSvelteFilePath)(path)) {
231
232
  this.logger.debug('Read Svelte file:', path);
232
233
  const svelteCode = readFile(path) || '';
233
234
  try {
234
235
  const isTsFile = true; // TODO check file contents? TS might be okay with importing ts into js.
235
- const result = svelte2tsx_1.svelte2tsx(svelteCode, {
236
+ const result = (0, svelte2tsx_1.svelte2tsx)(svelteCode, {
236
237
  filename: path.split('/').pop(),
237
- isTsFile
238
+ isTsFile,
239
+ mode: 'ts',
240
+ typingsNamespace: this.svelteOptions.namespace
238
241
  });
239
242
  const existingSnapshot = this.snapshots.get(path);
240
243
  if (existingSnapshot) {
@@ -13,7 +13,7 @@ function createSvelteSys(logger) {
13
13
  const svelteSys = {
14
14
  ...typescript_1.default.sys,
15
15
  fileExists(path) {
16
- return typescript_1.default.sys.fileExists(utils_1.ensureRealSvelteFilePath(path));
16
+ return typescript_1.default.sys.fileExists((0, utils_1.ensureRealSvelteFilePath)(path));
17
17
  },
18
18
  readDirectory(path, extensions, exclude, include, depth) {
19
19
  const extensionsWithSvelte = (extensions !== null && extensions !== void 0 ? extensions : []).concat('.svelte');
@@ -23,8 +23,8 @@ function createSvelteSys(logger) {
23
23
  if (typescript_1.default.sys.realpath) {
24
24
  const realpath = typescript_1.default.sys.realpath;
25
25
  svelteSys.realpath = function (path) {
26
- if (utils_1.isVirtualSvelteFilePath(path)) {
27
- return realpath(utils_1.toRealSvelteFilePath(path)) + '.ts';
26
+ if ((0, utils_1.isVirtualSvelteFilePath)(path)) {
27
+ return realpath((0, utils_1.toRealSvelteFilePath)(path)) + '.ts';
28
28
  }
29
29
  return realpath(path);
30
30
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-svelte-plugin",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "A TypeScript Plugin providing Svelte intellisense",
5
5
  "main": "dist/src/index.js",
6
6
  "scripts": {
@@ -23,7 +23,7 @@
23
23
  "typescript": "*"
24
24
  },
25
25
  "dependencies": {
26
- "svelte2tsx": "~0.3.0",
26
+ "svelte2tsx": "~0.5.0",
27
27
  "sourcemap-codec": "^1.4.8"
28
28
  }
29
29
  }