forked from sveltejs/language-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
157 lines (138 loc) · 5.55 KB
/
index.ts
File metadata and controls
157 lines (138 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import * as assert from 'assert';
import ts from 'typescript';
import { internalHelpers } from '../../src';
describe('Internal Helpers - upsertKitFile', () => {
function upsert(file: string, source: string, expected: string) {
const sourceFile = ts.createSourceFile('d', source, ts.ScriptTarget.Latest, true);
const result = internalHelpers.upsertKitFile(
ts,
file,
{
clientHooksPath: 'hooks.client',
paramsPath: 'params',
serverHooksPath: 'hooks.server',
universalHooksPath: 'hooks'
},
() => sourceFile
);
assert.strictEqual(result?.text, expected);
}
it('upserts +page.ts function', () => {
upsert(
'+page.ts',
`export function load(e) { return e; }`,
`export function load(e: import('./$types.js').PageLoadEvent) { return e; }`
);
});
it('upserts +page.js function with jsdoc', () => {
upsert(
'+page.js',
`export function load(e) { return e; }`,
`/** @param {import('./$types.js').PageLoadEvent} e */ export function load(e) { return e; }`
);
});
it('leaves +page.js function with jsdoc as is #1', () => {
upsert(
'+page.js',
`/** @type {import('./$types.js').PageLoad} */ export function load(e) { return e; }`,
`/** @type {import('./$types.js').PageLoad} */ export function load(e) { return e; }`
);
});
it('leaves +page.js function with jsdoc as is #2', () => {
upsert(
'+page.js',
`/** @param {import('./$types.js').PageLoadEvent} e */ export function load(e) { return e; }`,
`/** @param {import('./$types.js').PageLoadEvent} e */ export function load(e) { return e; }`
);
});
it('upserts handle hook const', () => {
upsert(
'hooks.server.ts',
`export const handle = async ({ event, resolve }) => {};`,
`export const handle = async ({ event, resolve }: Parameters<import('@sveltejs/kit').Handle>[0]) : ReturnType<import('@sveltejs/kit').Handle> => {};`
);
});
it('upserts handle hook const with jsdoc', () => {
upsert(
'hooks.server.js',
`export const handle = async ({ event, resolve }) => {};`,
`export const handle = /** @type {import('@sveltejs/kit').Handle} */ async ({ event, resolve }) => {};`
);
});
it('upserts GET async function', () => {
upsert(
'+server.ts',
`export async function GET(e) {}`,
`export async function GET(e: import('./$types.js').RequestEvent) : Promise<Response> {}`
);
});
it('upserts GET non-async function', () => {
upsert(
'+server.ts',
`export function GET(e) { return new Response(); }`,
`export function GET(e: import('./$types.js').RequestEvent) : Response | Promise<Response> { return new Response(); }`
);
});
it('upserts GET async function with jsdoc', () => {
upsert(
'+server.js',
`export async function GET(e) {}`,
`/** @type {(arg0: import('./$types.js').RequestEvent) => Response | Promise<Response>} */ export async function GET(e) {}`
);
});
it('upserts load const with paranthesis', () => {
upsert(
'+page.ts',
`export const load = (async (e) => {});`,
`export const load = (async (e: import('./$types.js').PageLoadEvent) => {});`
);
});
it('upserts load const with paranthesis and jsdoc', () => {
upsert(
'+page.js',
`export const load = (async (e) => {});`,
`export const load = (/** @param {import('./$types.js').PageLoadEvent} e */ async (e) => {});`
);
});
it('upserts actions with jsdoc', () => {
upsert(
'+page.server.js',
`export const actions = { default: async (e) => {} };`,
`export const actions = /** @satisfies {import('./$types.js').Actions} */ ({ default: async (e) => {} });`
);
});
it('leaves actions with existing jsdoc @satisfies as is', () => {
upsert(
'+page.server.js',
`/** @satisfies {import('./$types').Actions} */\nexport const actions = { default: async (e) => {} };`,
`/** @satisfies {import('./$types').Actions} */\nexport const actions = { default: async (e) => {} };`
);
});
it('leaves load const with existing jsdoc @satisfies as is', () => {
upsert(
'+page.js',
`/** @satisfies {import('./$types').PageLoad} */\nexport const load = (async (e) => {});`,
`/** @satisfies {import('./$types').PageLoad} */\nexport const load = (async (e) => {});`
);
});
it('recognizes page@', () => {
upsert('+page@.ts', `export const ssr = true;`, `export const ssr : boolean = true;`);
});
it('recognizes page@ with jsdoc', () => {
upsert(
'+page@.js',
`export const ssr = true;`,
`export const ssr = /** @type {boolean} */ (true);`
);
});
it('recognizes layout@foo', () => {
upsert('+layout@foo.ts', `export const ssr = true;`, `export const ssr : boolean = true;`);
});
it('recognizes layout@foo with jsdoc', () => {
upsert(
'+layout@foo.js',
`export const ssr = true;`,
`export const ssr = /** @type {boolean} */ (true);`
);
});
});