Skip to content

Commit 5478142

Browse files
authored
(fix) remove svelte from typings, add it to every svelte2tsx output instead (#441)
Less setup for people (`"types": ["svelte"]` now obsolete), makes TS search more of the other globals. #430
1 parent 1330142 commit 5478142

File tree

75 files changed

+107
-23
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+107
-23
lines changed

packages/language-server/src/plugins/typescript/features/CompletionProvider.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,11 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
300300
});
301301
}
302302
// prevent newText from being placed like this: <script>import {} from ''
303-
if (range.start.line === 0) {
303+
if (
304+
range.start.line === 0 &&
305+
!change.newText.startsWith('\r\n') &&
306+
!change.newText.startsWith('\n')
307+
) {
304308
change.newText = ts.sys.newLine + change.newText;
305309
}
306310

packages/language-server/src/plugins/typescript/service.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ export function createLanguageService(
6666
const svelteModuleLoader = createSvelteModuleLoader(getSnapshot, compilerOptions);
6767

6868
const svelteTsPath = dirname(require.resolve('svelte2tsx'));
69-
const svelteTsxFiles = ['./svelte-shims.d.ts', './svelte-jsx.d.ts', './svelte-native-jsx.d.ts'].map((f) =>
70-
ts.sys.resolvePath(resolve(svelteTsPath, f)),
71-
);
69+
const svelteTsxFiles = [
70+
'./svelte-shims.d.ts',
71+
'./svelte-jsx.d.ts',
72+
'./svelte-native-jsx.d.ts',
73+
].map((f) => ts.sys.resolvePath(resolve(svelteTsPath, f)));
7274

7375
const host: ts.LanguageServiceHost = {
7476
getCompilationSettings: () => compilerOptions,
@@ -162,7 +164,7 @@ export function createLanguageService(
162164
declaration: false,
163165
skipLibCheck: true,
164166
// these are needed to handle the results of svelte2tsx preprocessing:
165-
jsx: ts.JsxEmit.Preserve
167+
jsx: ts.JsxEmit.Preserve,
166168
};
167169

168170
// always let ts parse config to get default compilerOption
@@ -191,28 +193,22 @@ export function createLanguageService(
191193
);
192194

193195
const files = parsedConfig.fileNames;
194-
195-
const sveltePkgInfo = getPackageInfo('svelte', workspacePath || process.cwd());
196-
const types = (parsedConfig.options?.types ?? []).concat(
197-
resolve(sveltePkgInfo.path, 'types', 'runtime'),
198-
);
199196
const compilerOptions: ts.CompilerOptions = {
200197
...parsedConfig.options,
201-
types,
202198
...forcedCompilerOptions,
203199
};
204200

205201
// detect which JSX namespace to use (svelte | svelteNative) if not specified or not compatible
206-
if (!compilerOptions.jsxFactory || !compilerOptions.jsxFactory.startsWith("svelte")) {
202+
if (!compilerOptions.jsxFactory || !compilerOptions.jsxFactory.startsWith('svelte')) {
207203
//default to regular svelte, this causes the usage of the "svelte.JSX" namespace
208-
compilerOptions.jsxFactory = "svelte.createElement";
204+
compilerOptions.jsxFactory = 'svelte.createElement';
209205

210206
//override if we detect svelte-native
211207
if (workspacePath) {
212208
try {
213209
const svelteNativePkgInfo = getPackageInfo('svelte-native', workspacePath);
214210
if (svelteNativePkgInfo.path) {
215-
compilerOptions.jsxFactory = "svelteNative.createElement";
211+
compilerOptions.jsxFactory = 'svelteNative.createElement';
216212
}
217213
} catch (e) {
218214
//we stay regular svelte

packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ describe('CodeActionsProvider', () => {
193193
},
194194
},
195195
textRange: {
196-
pos: 130,
197-
end: 164,
196+
pos: 162,
197+
end: 196,
198198
},
199199
},
200200
],
@@ -283,8 +283,8 @@ describe('CodeActionsProvider', () => {
283283
},
284284
},
285285
textRange: {
286-
pos: 130,
287-
end: 164,
286+
pos: 162,
287+
end: 196,
288288
},
289289
},
290290
],
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
2-
"compilerOptions": {
3-
"jsxFactory": "svelteNative"
4-
}
2+
"compilerOptions": {
3+
"jsxFactory": "svelteNative",
4+
/**
5+
This is actually not needed, but makes the tests faster
6+
because TS does not look up other types.
7+
*/
8+
"types": ["svelte"]
9+
}
510
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
{
2-
"compilerOptions": { "strict": true }
2+
"compilerOptions": {
3+
"strict": true,
4+
/**
5+
This is actually not needed, but makes the tests faster
6+
because TS does not look up other types.
7+
*/
8+
"types": ["svelte"]
9+
}
310
}

packages/svelte2tsx/src/svelte2tsx.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ function processSvelteTemplate(str: MagicString): TemplateProcessResult {
220220
if (parent.type == 'Property' && prop == 'key') return;
221221
scope.declared.add(node.name);
222222
} else {
223-
if (parent.type == 'MemberExpression' && prop == 'property' && !parent.computed) return;
223+
if (parent.type == 'MemberExpression' && prop == 'property' && !parent.computed)
224+
return;
224225
if (parent.type == 'Property' && prop == 'key') return;
225226
pendingStoreResolutions.push({ node, parent, scope });
226227
}
@@ -1039,6 +1040,8 @@ export function svelte2tsx(
10391040
componentDocumentation,
10401041
);
10411042

1043+
str.prepend('///<reference types="svelte" />\n');
1044+
10421045
return {
10431046
code: str.toString(),
10441047
map: str.generateMap({ hires: true, source: options?.filename }),

packages/svelte2tsx/test/sourcemaps/event-binding.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
///<reference types="svelte" />
12
<></>;function render() {
23
<><Component />{__sveltets_instanceOf(Component).$on('click', __sveltets_store_get(check) ? method1 : method2)}
34
1==== 2==================

packages/svelte2tsx/test/sourcemaps/let.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
///<reference types="svelte" />
12
<></>;function render() {
23

34
let selected = __sveltets_invalidate(() => lookup.get(slug));

packages/svelte2tsx/test/sourcemaps/repl.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
///<reference types="svelte" />
12
<></>;
23
export async function preload({ params }) {
34
const res = await this.fetch(`tutorial/${params.slug}.json`);

packages/svelte2tsx/test/svelte2tsx/samples/$store-index/expected.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
///<reference types="svelte" />
12
<></>;function render() {
23
<>{someRecordOrArr[__sveltets_store_get(store)]}
34
{someObject['$store']}

0 commit comments

Comments
 (0)