Skip to content

Commit 2a1e979

Browse files
authored
[chore] enable TypeScript strict mode (#1998)
1 parent 075e3a8 commit 2a1e979

File tree

16 files changed

+75
-64
lines changed

16 files changed

+75
-64
lines changed

.changeset/loud-dancers-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
[chore] enable TypeScript strict mode

packages/kit/src/core/adapt/prerender.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
124124
method: 'GET',
125125
headers: {},
126126
path,
127-
rawBody: null,
127+
rawBody: '',
128128
query: new URLSearchParams()
129129
},
130130
{
@@ -247,7 +247,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
247247
method: 'GET',
248248
headers: {},
249249
path: '[fallback]', // this doesn't matter, but it's easiest if it's a string
250-
rawBody: null,
250+
rawBody: '',
251251
query: new URLSearchParams()
252252
},
253253
{

packages/kit/src/core/config/index.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { deep_merge, validate_config } from './index.js';
55
test('fills in defaults', () => {
66
const validated = validate_config({});
77

8+
// @ts-ignore
89
delete validated.kit.vite;
910

1011
assert.equal(validated, {
@@ -104,6 +105,7 @@ test('fills in partial blanks', () => {
104105

105106
assert.equal(validated.kit.vite(), {});
106107

108+
// @ts-ignore
107109
delete validated.kit.vite;
108110

109111
assert.equal(validated, {

packages/kit/src/core/config/test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ async function testLoadDefaultConfig(path) {
1515

1616
const config = await load_config({ cwd });
1717

18+
// @ts-ignore
1819
delete config.kit.vite; // can't test equality of a function
1920

2021
assert.equal(config, {

packages/kit/src/core/create_manifest_data/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,11 @@ export default function create_manifest_data({ config, output, cwd = process.cwd
247247
type: 'page',
248248
pattern,
249249
params,
250+
// @ts-ignore
250251
path,
252+
// @ts-ignore
251253
a,
254+
// @ts-ignore
252255
b
253256
});
254257
} else {

packages/kit/src/core/node/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function getRawBody(req) {
99
const h = req.headers;
1010

1111
if (!h['content-type']) {
12-
return fulfil(null);
12+
return fulfil('');
1313
}
1414

1515
req.on('error', reject);
@@ -18,7 +18,7 @@ export function getRawBody(req) {
1818

1919
// https://github.com/jshttp/type-is/blob/c1f4388c71c8a01f79934e68f630ca4a15fffcd6/index.js#L81-L95
2020
if (isNaN(length) && h['transfer-encoding'] == null) {
21-
return fulfil(null);
21+
return fulfil('');
2222
}
2323

2424
let data = new Uint8Array(length || 0);

packages/kit/src/runtime/client/renderer.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function page_store(value) {
3434

3535
/**
3636
* @param {RequestInfo} resource
37-
* @param {RequestInit} opts
37+
* @param {RequestInit} [opts]
3838
*/
3939
function initial_fetch(resource, opts) {
4040
const url = typeof resource === 'string' ? resource : resource.url;
@@ -82,7 +82,9 @@ export class Renderer {
8282

8383
/** @type {import('./types').NavigationState} */
8484
this.current = {
85+
// @ts-ignore
8586
page: null,
87+
// @ts-ignore
8688
session_id: null,
8789
branch: []
8890
};
@@ -114,7 +116,7 @@ export class Renderer {
114116
this.session_id += 1;
115117

116118
const info = this.router.parse(new URL(location.href));
117-
this.update(info, [], true);
119+
if (info) this.update(info, [], true);
118120
});
119121
ready = true;
120122
}
@@ -137,12 +139,6 @@ export class Renderer {
137139
/** @type {import('./types').NavigationResult | undefined} */
138140
let result;
139141

140-
/** @type {number | undefined} */
141-
let new_status;
142-
143-
/** @type {Error | undefined} new_error */
144-
let new_error;
145-
146142
try {
147143
for (let i = 0; i < nodes.length; i += 1) {
148144
const is_leaf = i === nodes.length - 1;
@@ -160,8 +156,12 @@ export class Renderer {
160156
if (node && node.loaded) {
161157
if (node.loaded.error) {
162158
if (error) throw node.loaded.error;
163-
new_status = node.loaded.status;
164-
new_error = node.loaded.error;
159+
result = await this._load_error({
160+
status: node.loaded.status,
161+
error: node.loaded.error,
162+
path: page.path,
163+
query: page.query
164+
});
165165
} else if (node.loaded.context) {
166166
context = {
167167
...context,
@@ -175,14 +175,9 @@ export class Renderer {
175175
} catch (e) {
176176
if (error) throw e;
177177

178-
new_status = 500;
179-
new_error = e;
180-
}
181-
182-
if (new_error) {
183178
result = await this._load_error({
184-
status: new_status,
185-
error: new_error,
179+
status: 500,
180+
error: e,
186181
path: page.path,
187182
query: page.query
188183
});
@@ -203,6 +198,7 @@ export class Renderer {
203198
dispatchEvent(new CustomEvent('sveltekit:navigation-start'));
204199

205200
if (this.started) {
201+
// @ts-ignore
206202
this.stores.navigating.set({
207203
from: {
208204
path: this.current.page.path,
@@ -269,6 +265,7 @@ export class Renderer {
269265
this.loading.promise = null;
270266
this.loading.id = null;
271267

268+
if (!this.router) return;
272269
const leaf_node = navigation_result.state.branch[navigation_result.state.branch.length - 1];
273270
if (leaf_node && leaf_node.module.router === false) {
274271
this.router.disable();
@@ -294,8 +291,8 @@ export class Renderer {
294291

295292
if (!this.invalidating) {
296293
this.invalidating = Promise.resolve().then(async () => {
297-
const info = this.router.parse(new URL(location.href));
298-
await this.update(info, [], true);
294+
const info = this.router && this.router.parse(new URL(location.href));
295+
if (info) await this.update(info, [], true);
299296

300297
this.invalidating = null;
301298
});
@@ -330,6 +327,7 @@ export class Renderer {
330327
*/
331328
async _get_navigation_result(info, no_cache) {
332329
if (this.loading.id === info.id) {
330+
// @ts-ignore if the id is defined then the promise is too
333331
return this.loading.promise;
334332
}
335333

@@ -338,7 +336,7 @@ export class Renderer {
338336

339337
// check if endpoint route
340338
if (route.length === 1) {
341-
return { reload: true };
339+
return { reload: true, props: {}, state: this.current };
342340
}
343341

344342
// load code for subsequent routes immediately, if they are as
@@ -399,7 +397,8 @@ export class Renderer {
399397
};
400398

401399
for (let i = 0; i < filtered.length; i += 1) {
402-
if (filtered[i].loaded) result.props[`props_${i}`] = await filtered[i].loaded.props;
400+
const loaded = filtered[i].loaded;
401+
if (loaded) result.props[`props_${i}`] = await loaded.props;
403402
}
404403

405404
if (
@@ -534,7 +533,7 @@ export class Renderer {
534533
/**
535534
* @param {import('./types').NavigationCandidate} selected
536535
* @param {boolean} no_cache
537-
* @returns {Promise<import('./types').NavigationResult>}
536+
* @returns {Promise<import('./types').NavigationResult | undefined>} undefined if fallthrough
538537
*/
539538
async _load({ route, path, query }, no_cache) {
540539
const key = `${path}?${query}`;
@@ -545,6 +544,7 @@ export class Renderer {
545544
}
546545

547546
const [pattern, a, b, get_params] = route;
547+
// @ts-ignore - the pattern is for the route which we've already matched to this path
548548
const params = get_params ? get_params(pattern.exec(path)) : {};
549549

550550
const changed = this.current.page && {
@@ -610,7 +610,9 @@ export class Renderer {
610610

611611
if (node.loaded.redirect) {
612612
return {
613-
redirect: node.loaded.redirect
613+
redirect: node.loaded.redirect,
614+
props: {},
615+
state: this.current
614616
};
615617
}
616618

@@ -651,7 +653,7 @@ export class Renderer {
651653
context: node_loaded.context
652654
});
653655

654-
if (error_loaded && error_loaded.loaded.error) {
656+
if (error_loaded && error_loaded.loaded && error_loaded.loaded.error) {
655657
continue;
656658
}
657659

@@ -713,7 +715,7 @@ export class Renderer {
713715
error,
714716
module: await this.fallback[1],
715717
page,
716-
context: node && node.loaded && node.loaded.context
718+
context: (node && node.loaded && node.loaded.context) || {}
717719
})
718720
];
719721

packages/kit/src/runtime/client/types.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ export type NavigationCandidate = {
1717
export type NavigationResult = {
1818
reload?: boolean;
1919
redirect?: string;
20-
state?: NavigationState;
21-
props?: Record<string, any>;
20+
state: NavigationState;
21+
props: Record<string, any>;
2222
};
2323

2424
export type BranchNode = {
2525
module: CSRComponent;
26-
loaded: NormalizedLoadOutput;
26+
loaded: NormalizedLoadOutput | null;
2727
uses: {
2828
params: Set<string>;
2929
path: boolean;

packages/kit/src/runtime/load.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function normalize(loaded) {
1010

1111
if (!loaded.error && has_error_status) {
1212
return {
13-
status,
13+
status: status || 500,
1414
error: new Error()
1515
};
1616
}

packages/kit/src/runtime/server/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ export async function respond(incoming, options, state = {}) {
5050
$session: await options.hooks.getSession(request),
5151
page_config: { ssr: false, router: true, hydrate: true },
5252
status: 200,
53-
branch: [],
54-
page: null
53+
branch: []
5554
});
5655
}
5756

@@ -67,13 +66,13 @@ export async function respond(incoming, options, state = {}) {
6766
// inject ETags for 200 responses
6867
if (response.status === 200) {
6968
if (!/(no-store|immutable)/.test(response.headers['cache-control'])) {
70-
const etag = `"${hash(response.body)}"`;
69+
const etag = `"${hash(response.body || '')}"`;
7170

7271
if (request.headers['if-none-match'] === etag) {
7372
return {
7473
status: 304,
7574
headers: {},
76-
body: null
75+
body: ''
7776
};
7877
}
7978

0 commit comments

Comments
 (0)