Skip to content

Commit

Permalink
[chore] enable TypeScript strict mode (#1998)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored Jul 23, 2021
1 parent 075e3a8 commit 2a1e979
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 64 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-dancers-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[chore] enable TypeScript strict mode
4 changes: 2 additions & 2 deletions packages/kit/src/core/adapt/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
method: 'GET',
headers: {},
path,
rawBody: null,
rawBody: '',
query: new URLSearchParams()
},
{
Expand Down Expand Up @@ -247,7 +247,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
method: 'GET',
headers: {},
path: '[fallback]', // this doesn't matter, but it's easiest if it's a string
rawBody: null,
rawBody: '',
query: new URLSearchParams()
},
{
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { deep_merge, validate_config } from './index.js';
test('fills in defaults', () => {
const validated = validate_config({});

// @ts-ignore
delete validated.kit.vite;

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

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

// @ts-ignore
delete validated.kit.vite;

assert.equal(validated, {
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/config/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async function testLoadDefaultConfig(path) {

const config = await load_config({ cwd });

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

assert.equal(config, {
Expand Down
3 changes: 3 additions & 0 deletions packages/kit/src/core/create_manifest_data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,11 @@ export default function create_manifest_data({ config, output, cwd = process.cwd
type: 'page',
pattern,
params,
// @ts-ignore
path,
// @ts-ignore
a,
// @ts-ignore
b
});
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/core/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function getRawBody(req) {
const h = req.headers;

if (!h['content-type']) {
return fulfil(null);
return fulfil('');
}

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

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

let data = new Uint8Array(length || 0);
Expand Down
52 changes: 27 additions & 25 deletions packages/kit/src/runtime/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function page_store(value) {

/**
* @param {RequestInfo} resource
* @param {RequestInit} opts
* @param {RequestInit} [opts]
*/
function initial_fetch(resource, opts) {
const url = typeof resource === 'string' ? resource : resource.url;
Expand Down Expand Up @@ -82,7 +82,9 @@ export class Renderer {

/** @type {import('./types').NavigationState} */
this.current = {
// @ts-ignore
page: null,
// @ts-ignore
session_id: null,
branch: []
};
Expand Down Expand Up @@ -114,7 +116,7 @@ export class Renderer {
this.session_id += 1;

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

/** @type {number | undefined} */
let new_status;

/** @type {Error | undefined} new_error */
let new_error;

try {
for (let i = 0; i < nodes.length; i += 1) {
const is_leaf = i === nodes.length - 1;
Expand All @@ -160,8 +156,12 @@ export class Renderer {
if (node && node.loaded) {
if (node.loaded.error) {
if (error) throw node.loaded.error;
new_status = node.loaded.status;
new_error = node.loaded.error;
result = await this._load_error({
status: node.loaded.status,
error: node.loaded.error,
path: page.path,
query: page.query
});
} else if (node.loaded.context) {
context = {
...context,
Expand All @@ -175,14 +175,9 @@ export class Renderer {
} catch (e) {
if (error) throw e;

new_status = 500;
new_error = e;
}

if (new_error) {
result = await this._load_error({
status: new_status,
error: new_error,
status: 500,
error: e,
path: page.path,
query: page.query
});
Expand All @@ -203,6 +198,7 @@ export class Renderer {
dispatchEvent(new CustomEvent('sveltekit:navigation-start'));

if (this.started) {
// @ts-ignore
this.stores.navigating.set({
from: {
path: this.current.page.path,
Expand Down Expand Up @@ -269,6 +265,7 @@ export class Renderer {
this.loading.promise = null;
this.loading.id = null;

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

if (!this.invalidating) {
this.invalidating = Promise.resolve().then(async () => {
const info = this.router.parse(new URL(location.href));
await this.update(info, [], true);
const info = this.router && this.router.parse(new URL(location.href));
if (info) await this.update(info, [], true);

this.invalidating = null;
});
Expand Down Expand Up @@ -330,6 +327,7 @@ export class Renderer {
*/
async _get_navigation_result(info, no_cache) {
if (this.loading.id === info.id) {
// @ts-ignore if the id is defined then the promise is too
return this.loading.promise;
}

Expand All @@ -338,7 +336,7 @@ export class Renderer {

// check if endpoint route
if (route.length === 1) {
return { reload: true };
return { reload: true, props: {}, state: this.current };
}

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

for (let i = 0; i < filtered.length; i += 1) {
if (filtered[i].loaded) result.props[`props_${i}`] = await filtered[i].loaded.props;
const loaded = filtered[i].loaded;
if (loaded) result.props[`props_${i}`] = await loaded.props;
}

if (
Expand Down Expand Up @@ -534,7 +533,7 @@ export class Renderer {
/**
* @param {import('./types').NavigationCandidate} selected
* @param {boolean} no_cache
* @returns {Promise<import('./types').NavigationResult>}
* @returns {Promise<import('./types').NavigationResult | undefined>} undefined if fallthrough
*/
async _load({ route, path, query }, no_cache) {
const key = `${path}?${query}`;
Expand All @@ -545,6 +544,7 @@ export class Renderer {
}

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

const changed = this.current.page && {
Expand Down Expand Up @@ -610,7 +610,9 @@ export class Renderer {

if (node.loaded.redirect) {
return {
redirect: node.loaded.redirect
redirect: node.loaded.redirect,
props: {},
state: this.current
};
}

Expand Down Expand Up @@ -651,7 +653,7 @@ export class Renderer {
context: node_loaded.context
});

if (error_loaded && error_loaded.loaded.error) {
if (error_loaded && error_loaded.loaded && error_loaded.loaded.error) {
continue;
}

Expand Down Expand Up @@ -713,7 +715,7 @@ export class Renderer {
error,
module: await this.fallback[1],
page,
context: node && node.loaded && node.loaded.context
context: (node && node.loaded && node.loaded.context) || {}
})
];

Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/runtime/client/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export type NavigationCandidate = {
export type NavigationResult = {
reload?: boolean;
redirect?: string;
state?: NavigationState;
props?: Record<string, any>;
state: NavigationState;
props: Record<string, any>;
};

export type BranchNode = {
module: CSRComponent;
loaded: NormalizedLoadOutput;
loaded: NormalizedLoadOutput | null;
uses: {
params: Set<string>;
path: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function normalize(loaded) {

if (!loaded.error && has_error_status) {
return {
status,
status: status || 500,
error: new Error()
};
}
Expand Down
7 changes: 3 additions & 4 deletions packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ export async function respond(incoming, options, state = {}) {
$session: await options.hooks.getSession(request),
page_config: { ssr: false, router: true, hydrate: true },
status: 200,
branch: [],
page: null
branch: []
});
}

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

if (request.headers['if-none-match'] === etag) {
return {
status: 304,
headers: {},
body: null
body: ''
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/load_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const s = JSON.stringify;
* status?: number;
* error?: Error;
* }} opts
* @returns {Promise<import('./types').Loaded>}
* @returns {Promise<import('./types').Loaded | undefined>} undefined for fallthrough
*/
export async function load_node({
request,
Expand Down
12 changes: 6 additions & 6 deletions packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const s = JSON.stringify;
* page_config: { hydrate: boolean, router: boolean, ssr: boolean };
* status: number;
* error?: Error,
* branch: Array<import('./types').Loaded> | undefined;
* page: import('types/page').Page
* branch?: Array<import('./types').Loaded>;
* page?: import('types/page').Page
* }} opts
*/
export async function render_response({
Expand Down Expand Up @@ -134,10 +134,10 @@ export async function render_response({
.join(',\n\t\t\t\t\t\t')}
],
page: {
host: ${page.host ? s(page.host) : 'location.host'}, // TODO this is redundant
path: ${s(page.path)},
query: new URLSearchParams(${s(page.query.toString())}),
params: ${s(page.params)}
host: ${page && page.host ? s(page.host) : 'location.host'}, // TODO this is redundant
path: ${s(page && page.path)},
query: new URLSearchParams(${page ? s(page.query.toString()) : ''}),
params: ${page && s(page.params)}
}
}` : 'null'}
});
Expand Down
Loading

0 comments on commit 2a1e979

Please sign in to comment.