typescript-svelte-plugin 0.2.5 → 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,14 +6,18 @@ 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
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);
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);
17
21
  (0, module_loader_1.patchModuleLoader)(logger, snapshotManager, modules.typescript, info.languageServiceHost, info.project);
18
22
  return (0, language_service_1.decorateLanguageService)(info.languageService, snapshotManager, logger);
19
23
  }
@@ -30,29 +34,12 @@ function init(modules) {
30
34
  ].map((f) => modules.typescript.sys.resolvePath((0, path_1.resolve)(svelteTsPath, f)));
31
35
  return svelteTsxFiles;
32
36
  }
33
- function patchCompilerOptions(project) {
34
- var _a;
35
- const compilerOptions = project.getCompilerOptions();
36
- // Patch needed because svelte2tsx creates jsx/tsx files
37
- compilerOptions.jsx = modules.typescript.JsxEmit.Preserve;
38
- // detect which JSX namespace to use (svelte | svelteNative) if not specified or not compatible
39
- if (!((_a = compilerOptions.jsxFactory) === null || _a === void 0 ? void 0 : _a.startsWith('svelte'))) {
40
- // Default to regular svelte, this causes the usage of the "svelte.JSX" namespace
41
- // We don't need to add a switch for svelte-native because the jsx is only relevant
42
- // within Svelte files, which this plugin does not deal with.
43
- compilerOptions.jsxFactory = 'svelte.createElement';
44
- }
45
- }
46
37
  function isSvelteProject(compilerOptions) {
47
38
  // Add more checks like "no Svelte file found" or "no config file found"?
48
- const isNoJsxProject = (!compilerOptions.jsx || compilerOptions.jsx === modules.typescript.JsxEmit.Preserve) &&
49
- (!compilerOptions.jsxFactory || compilerOptions.jsxFactory.startsWith('svelte')) &&
50
- !compilerOptions.jsxFragmentFactory &&
51
- !compilerOptions.jsxImportSource;
52
39
  try {
53
40
  const isSvelteProject = typeof compilerOptions.configFilePath !== 'string' ||
54
41
  require.resolve('svelte', { paths: [compilerOptions.configFilePath] });
55
- return isNoJsxProject && isSvelteProject;
42
+ return isSvelteProject;
56
43
  }
57
44
  catch (e) {
58
45
  // If require.resolve fails, we end up here
@@ -95,7 +95,7 @@ function patchModuleLoader(logger, snapshotManager, typescript, lsHost, project)
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')
45
+ if (typeof mappings === 'string') {
38
46
  this.mappings = (0, sourcemap_codec_1.decode)(mappings);
39
- else
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;
@@ -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();
@@ -234,7 +235,9 @@ class SvelteSnapshotManager {
234
235
  const isTsFile = true; // TODO check file contents? TS might be okay with importing ts into js.
235
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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-svelte-plugin",
3
- "version": "0.2.5",
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.4.0",
26
+ "svelte2tsx": "~0.5.0",
27
27
  "sourcemap-codec": "^1.4.8"
28
28
  }
29
29
  }