Skip to content

Commit

Permalink
Improve parseIntoFsdRoot function by introducing the capability to sp…
Browse files Browse the repository at this point in the history
…ecify a path to the root
  • Loading branch information
daniilsapa committed Jul 24, 2024
1 parent 892f5de commit 249e5b2
Showing 1 changed file with 101 additions and 2 deletions.
103 changes: 101 additions & 2 deletions packages/steiger-plugin-fsd/src/_lib/prepare-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,31 @@ import type { FsdRoot } from '@feature-sliced/filesystem'
import type { Folder, File, Diagnostic } from '@steiger/types'
import { vi } from 'vitest'

function findSubfolder(folder: Folder, path: string): Folder {
function checkFolder(folder: Folder): Folder {
if (folder.path === path) {
return folder
}

if (path.startsWith(folder.path)) {
for (const child of folder.children) {
if (child.type === 'folder') {
const result = checkFolder(child)
if (result) {
return result
}
}
}
}

throw new Error(`Path "${path}" not found in the provided file system mock!`)
}

return checkFolder(folder)
}

/** Parse a multi-line indented string with emojis for files and folders into an FSD root. */
export function parseIntoFsdRoot(fsMarkup: string): FsdRoot {
export function parseIntoFsdRoot(fsMarkup: string, rootPath?: string): FsdRoot {
function parseFolder(lines: Array<string>, path: string): Folder {
const children: Array<Folder | File> = []

Expand All @@ -32,8 +55,9 @@ export function parseIntoFsdRoot(fsMarkup: string): FsdRoot {
.filter(Boolean)
.map((line, _i, lines) => line.slice(lines[0].search(/\S/)))
.filter(Boolean)
const parsedFolder = parseFolder(lines, joinFromRoot())

return parseFolder(lines, joinFromRoot())
return rootPath ? findSubfolder(parsedFolder, rootPath) : parsedFolder
}

export function compareMessages(a: Diagnostic, b: Diagnostic): number {
Expand Down Expand Up @@ -149,4 +173,79 @@ if (import.meta.vitest) {
],
})
})

test('it should return a nested root folder when the optional rootPath argument is passed', () => {
const markup = `
📂 src
📂 entities
📂 users
📂 ui
📄 index.ts
📂 posts
📂 ui
📄 index.ts
📂 shared
📂 ui
📄 index.ts
📄 Button.tsx
`
const root = parseIntoFsdRoot(markup, joinFromRoot('src', 'entities'))

expect(root).toEqual({
type: 'folder',
path: joinFromRoot('src', 'entities'),
children: [
{
type: 'folder',
path: joinFromRoot('src', 'entities', 'users'),
children: [
{
type: 'folder',
path: joinFromRoot('src', 'entities', 'users', 'ui'),
children: [],
},
{
type: 'file',
path: joinFromRoot('src', 'entities', 'users', 'index.ts'),
},
],
},
{
type: 'folder',
path: joinFromRoot('src', 'entities', 'posts'),
children: [
{
type: 'folder',
path: joinFromRoot('src', 'entities', 'posts', 'ui'),
children: [],
},
{
type: 'file',
path: joinFromRoot('src', 'entities', 'posts', 'index.ts'),
},
],
},
],
})
})

test('it should throw an error when the path (from rootPath argument) is not found in the provided file system mock', () => {
const markup = `
📂 src
📂 entities
📂 users
📂 ui
📄 index.ts
📂 posts
📂 ui
📄 index.ts
📂 shared
📂 ui
📄 index.ts
📄 Button.tsx
`
expect(() => parseIntoFsdRoot(markup, joinFromRoot('src', 'non-existent-folder'))).toThrowError(
'Path "/src/non-existent-folder" not found in the provided file system mock!',
)
})
}

0 comments on commit 249e5b2

Please sign in to comment.