Skip to content

Commit

Permalink
(fix) remove svelte from typings, add it to every svelte2tsx output i…
Browse files Browse the repository at this point in the history
…nstead (#441)

Less setup for people (`"types": ["svelte"]` now obsolete), makes TS search more of the other globals.
#430
  • Loading branch information
dummdidumm authored Aug 11, 2020
1 parent 1330142 commit 5478142
Show file tree
Hide file tree
Showing 75 changed files with 107 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,11 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
});
}
// prevent newText from being placed like this: <script>import {} from ''
if (range.start.line === 0) {
if (
range.start.line === 0 &&
!change.newText.startsWith('\r\n') &&
!change.newText.startsWith('\n')
) {
change.newText = ts.sys.newLine + change.newText;
}

Expand Down
22 changes: 9 additions & 13 deletions packages/language-server/src/plugins/typescript/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ export function createLanguageService(
const svelteModuleLoader = createSvelteModuleLoader(getSnapshot, compilerOptions);

const svelteTsPath = dirname(require.resolve('svelte2tsx'));
const svelteTsxFiles = ['./svelte-shims.d.ts', './svelte-jsx.d.ts', './svelte-native-jsx.d.ts'].map((f) =>
ts.sys.resolvePath(resolve(svelteTsPath, f)),
);
const svelteTsxFiles = [
'./svelte-shims.d.ts',
'./svelte-jsx.d.ts',
'./svelte-native-jsx.d.ts',
].map((f) => ts.sys.resolvePath(resolve(svelteTsPath, f)));

const host: ts.LanguageServiceHost = {
getCompilationSettings: () => compilerOptions,
Expand Down Expand Up @@ -162,7 +164,7 @@ export function createLanguageService(
declaration: false,
skipLibCheck: true,
// these are needed to handle the results of svelte2tsx preprocessing:
jsx: ts.JsxEmit.Preserve
jsx: ts.JsxEmit.Preserve,
};

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

const files = parsedConfig.fileNames;

const sveltePkgInfo = getPackageInfo('svelte', workspacePath || process.cwd());
const types = (parsedConfig.options?.types ?? []).concat(
resolve(sveltePkgInfo.path, 'types', 'runtime'),
);
const compilerOptions: ts.CompilerOptions = {
...parsedConfig.options,
types,
...forcedCompilerOptions,
};

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

//override if we detect svelte-native
if (workspacePath) {
try {
const svelteNativePkgInfo = getPackageInfo('svelte-native', workspacePath);
if (svelteNativePkgInfo.path) {
compilerOptions.jsxFactory = "svelteNative.createElement";
compilerOptions.jsxFactory = 'svelteNative.createElement';
}
} catch (e) {
//we stay regular svelte
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ describe('CodeActionsProvider', () => {
},
},
textRange: {
pos: 130,
end: 164,
pos: 162,
end: 196,
},
},
],
Expand Down Expand Up @@ -283,8 +283,8 @@ describe('CodeActionsProvider', () => {
},
},
textRange: {
pos: 130,
end: 164,
pos: 162,
end: 196,
},
},
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"compilerOptions": {
"jsxFactory": "svelteNative"
}
"compilerOptions": {
"jsxFactory": "svelteNative",
/**
This is actually not needed, but makes the tests faster
because TS does not look up other types.
*/
"types": ["svelte"]
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{
"compilerOptions": { "strict": true }
"compilerOptions": {
"strict": true,
/**
This is actually not needed, but makes the tests faster
because TS does not look up other types.
*/
"types": ["svelte"]
}
}
5 changes: 4 additions & 1 deletion packages/svelte2tsx/src/svelte2tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ function processSvelteTemplate(str: MagicString): TemplateProcessResult {
if (parent.type == 'Property' && prop == 'key') return;
scope.declared.add(node.name);
} else {
if (parent.type == 'MemberExpression' && prop == 'property' && !parent.computed) return;
if (parent.type == 'MemberExpression' && prop == 'property' && !parent.computed)
return;
if (parent.type == 'Property' && prop == 'key') return;
pendingStoreResolutions.push({ node, parent, scope });
}
Expand Down Expand Up @@ -1039,6 +1040,8 @@ export function svelte2tsx(
componentDocumentation,
);

str.prepend('///<reference types="svelte" />\n');

return {
code: str.toString(),
map: str.generateMap({ hires: true, source: options?.filename }),
Expand Down
1 change: 1 addition & 0 deletions packages/svelte2tsx/test/sourcemaps/event-binding.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
<><Component />{__sveltets_instanceOf(Component).$on('click', __sveltets_store_get(check) ? method1 : method2)}
1==== 2==================
Expand Down
1 change: 1 addition & 0 deletions packages/svelte2tsx/test/sourcemaps/let.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let selected = __sveltets_invalidate(() => lookup.get(slug));
Expand Down
1 change: 1 addition & 0 deletions packages/svelte2tsx/test/sourcemaps/repl.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;
export async function preload({ params }) {
const res = await this.fetch(`tutorial/${params.slug}.json`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
<>{someRecordOrArr[__sveltets_store_get(store)]}
{someObject['$store']}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let [a,b,c] = [1,2,3];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
__sveltets_store_get(var);
() => (<></>);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
__sveltets_store_get(var);
() => (<></>);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;
import { readable } from 'svelte/store';
function render() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
<><input id="dom-input" type="radio" {...__sveltets_empty(__sveltets_store_get(compile_options).generate)} value="dom"/></>
return { props: {}, slots: {}, getters: {}, events: {} }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let i = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let b = 7;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let b = 7;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let b = 7;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
<>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
<>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
<>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
<><Button ></Button>
<Radio ></Radio></>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
<><Button ></Button></>
return { props: {}, slots: {}, getters: {}, events: {'click':__sveltets_bubbleEventDef(__sveltets_instanceOf(Button).$$events_def, 'click')} }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
<><button onclick={undefined}></button></>
return { props: {}, slots: {}, getters: {}, events: {'click':__sveltets_mapElementEvent('click')} }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
<><sveltebody onclick={undefined}></sveltebody>
<sveltewindow onresize={undefined}></sveltewindow></>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

class Foo {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let a;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let name = "world"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let world = "world";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

/**@type { string | number }*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;
import Test from './Test.svelte';
function render() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;
import { a as b } from "./test.svelte"
import * as c from "b.ts"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;let b = 5;;<></>;function render() {
let world = "name";
() => (<><h1>hello {world}</h1></>);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;let b = 5;;<></>;function render() {
let world = "name";
() => (<><h1>hello {world}</h1></>);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;
export function preload() {}
let b = 5;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;
export function preload() {}
let b = 5;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let top1 = someStore()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
<><h1 onclick={ () => {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let { name: rename } = { name: "world" };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let a: 1 | 2 = 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

$: store.set( __sveltets_invalidate(() => __sveltets_store_get(store) + 1));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let name = "world"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;
export function preload() {}
let b = 5;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let b = 'top level';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let world = "name"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let Script, Style;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;
import Test from './Test.svelte';
function render() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
<><h1>hello</h1></>
return { props: {}, slots: {}, getters: {}, events: {} }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let name = "world"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
<><Me f={`${__sveltets_store_get(s)} `}/></>
return { props: {}, slots: {}, getters: {}, events: {} }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
<>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
<></>
return { props: {}, slots: {}, getters: {}, events: {} }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {
<></>
return { props: {}, slots: {}, getters: {}, events: {} }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

let f = (a: number, b: number) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///<reference types="svelte" />
<></>;function render() {

const name: string = "world";
Expand Down
Loading

0 comments on commit 5478142

Please sign in to comment.