Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] correct parent data type for layouts #6025

Merged
merged 2 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wise-berries-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Fix parent data type for layouts referencing named layouts in the same folder
25 changes: 18 additions & 7 deletions packages/kit/src/core/sync/write_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down
8 changes: 8 additions & 0 deletions packages/kit/src/core/sync/write_types.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand Down