From 61f28df85cd0c4b68bb69ab04b3ff2aca34141d6 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Thu, 18 Aug 2022 11:09:58 +0200 Subject: [PATCH 1/2] [fix] correct parent data type for layouts Was wrong if a layout referenced a named layout in the same folder Fixes #6013 --- packages/kit/src/core/sync/write_types.js | 25 +++++++++++++------ .../kit/src/core/sync/write_types.spec.js | 8 ++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/packages/kit/src/core/sync/write_types.js b/packages/kit/src/core/sync/write_types.js index 34ec6f33e7bf..c5cfe1ac5acd 100644 --- a/packages/kit/src/core/sync/write_types.js +++ b/packages/kit/src/core/sync/write_types.js @@ -139,12 +139,23 @@ function get_groups(manifest_data, routes_dir) { return group; } - // first, sort nodes by path length (necessary for finding the nearest layout more efficiently)... - const nodes = [...manifest_data.nodes].sort( - (n1, n2) => + // first, sort nodes (necessary for finding the nearest layout more efficiently)... + const nodes = [...manifest_data.nodes].sort((n1, n2) => { + // Sort by path length first... + const path_length_diff = /** @type {string} */ (n1.component ?? n1.shared ?? n1.server).split('/').length - - /** @type {string} */ (n2.component ?? n2.shared ?? n2.server).split('/').length - ); + /** @type {string} */ (n2.component ?? n2.shared ?? n2.server).split('/').length; + + return ( + path_length_diff || + // ...on ties, sort named layouts first + (path.basename(n1.component || '').includes('-') + ? -1 + : path.basename(n2.component || '').includes('-') + ? 1 + : 0) + ); + }); // ...then, populate `directories` with +page/+layout files... for (let i = 0; i < nodes.length; i += 1) { @@ -713,12 +724,12 @@ export function find_nearest_layout(routes_dir, nodes, start_idx) { } let common_path = path.dirname(start_file); - if (match[1] === 'layout' && !name) { + if (match[1] === 'layout' && !match[2] && !name) { // We are a default layout, so we skip the current level common_path = path.dirname(common_path); } - for (let i = start_idx; i >= 0; i -= 1) { + for (let i = start_idx - 1; i >= 0; i -= 1) { const node = nodes[i]; const file = /** @type {string} */ (node.component || node.shared || node.server); diff --git a/packages/kit/src/core/sync/write_types.spec.js b/packages/kit/src/core/sync/write_types.spec.js index 2191e742116e..4f0a85d140b7 100644 --- a/packages/kit/src/core/sync/write_types.spec.js +++ b/packages/kit/src/core/sync/write_types.spec.js @@ -160,6 +160,14 @@ test('Finds nearest layout (named)', () => { }); }); +test('Finds nearest named layout from layout', () => { + assert.equal(find_nearest_layout('src/routes', nodes, 1), { + key: '', + folder_depth_diff: 0, + name: '' + }); +}); + test('Finds nearest layout (recursively named)', () => { assert.equal(find_nearest_layout('src/routes', nodes, 3), { key: '', From db08670cf9b7d83fe70dc96e57374809c41df31b Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Thu, 18 Aug 2022 11:11:14 +0200 Subject: [PATCH 2/2] changeset --- .changeset/wise-berries-pay.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/wise-berries-pay.md diff --git a/.changeset/wise-berries-pay.md b/.changeset/wise-berries-pay.md new file mode 100644 index 000000000000..457972dc1483 --- /dev/null +++ b/.changeset/wise-berries-pay.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Fix parent data type for layouts referencing named layouts in the same folder