From 59fc32929ba3f5a8573b1d9c11a2edb80618b81d Mon Sep 17 00:00:00 2001 From: Aron Griffis Date: Wed, 20 Sep 2023 16:15:12 -0400 Subject: [PATCH] fix: don't remove redundant filename part if there won't be anything left --- .../preview-api/src/modules/store/autoTitle.test.ts | 12 ++++++++++++ code/lib/preview-api/src/modules/store/autoTitle.ts | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/code/lib/preview-api/src/modules/store/autoTitle.test.ts b/code/lib/preview-api/src/modules/store/autoTitle.test.ts index 013ccac6690f..312f1bced806 100644 --- a/code/lib/preview-api/src/modules/store/autoTitle.test.ts +++ b/code/lib/preview-api/src/modules/store/autoTitle.test.ts @@ -240,6 +240,18 @@ describe('userOrAutoTitleFromSpecifier', () => { ).toMatchInlineSnapshot(`to_my/file`); }); + it('match with short path', () => { + // Make sure "stories" isn't trimmed as redundant when there won't be + // anything left. + expect( + userOrAuto( + './path/stories.js', + normalizeStoriesEntry({ directory: './path', files: '**/?(*.)stories.*' }, options), + undefined + ) + ).toMatchInlineSnapshot(`stories`); + }); + it('match with windows path', () => { expect( userOrAuto( diff --git a/code/lib/preview-api/src/modules/store/autoTitle.ts b/code/lib/preview-api/src/modules/store/autoTitle.ts index 85fff681ddf2..72965b06aad4 100644 --- a/code/lib/preview-api/src/modules/store/autoTitle.ts +++ b/code/lib/preview-api/src/modules/store/autoTitle.ts @@ -15,7 +15,7 @@ const stripExtension = (parts: string[]) => { const removeRedundantFilename = (parts: string[]) => { const last = parts[parts.length - 1]; const nextToLast = parts[parts.length - 2]; - return last && (last === nextToLast || /^(?:index|story|stories)$/i.test(last)) + return last && nextToLast && (last === nextToLast || /^(?:index|story|stories)$/i.test(last)) ? parts.slice(0, -1) : parts; };