-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
packages/steiger-plugin-fsd/src/no-file-segments/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# `no-file-segments` | ||
|
||
Discourage the usage of file segments, suggesting folder segments instead. | ||
|
||
Example of a project structure that passes this rule: | ||
|
||
``` | ||
📂 shared | ||
📂 ui | ||
📄 styles.ts | ||
📄 Button.tsx | ||
📄 TextField.tsx | ||
📄 index.ts | ||
📂 pages | ||
📂 editor | ||
📂 ui | ||
📄 EditorPage.tsx | ||
📄 Editor.tsx | ||
📄 index.ts | ||
``` | ||
|
||
Examples of project structures that violate this rule: | ||
|
||
``` | ||
📂 shared | ||
📂 ui | ||
📄 styles.ts | ||
📄 Button.tsx | ||
📄 TextField.tsx | ||
📄 index.ts | ||
📂 features | ||
📂 comments | ||
📄 ui.tsx // ❌ | ||
📄 index.ts | ||
📂 pages | ||
📂 editor | ||
📄 ui.tsx // ❌ | ||
📄 index.ts | ||
📂 settings | ||
📂 ui | ||
📄 SettingsPage.tsx | ||
📄 index.ts | ||
``` | ||
|
||
``` | ||
📂 shared | ||
📄 routes.ts // ❌ | ||
📂 ui | ||
📄 styles.ts | ||
📄 Button.tsx | ||
📄 TextField.tsx | ||
📄 index.ts | ||
📂 entities | ||
📂 user | ||
📂 ui | ||
📄 UserAvatar.tsx | ||
📄 index.ts | ||
📂 product | ||
📂 ui | ||
📄 ProductCard.tsx | ||
📄 index.ts | ||
📂 features | ||
📂 comments | ||
📂 ui | ||
📄 CommentCard.tsx | ||
📄 index.ts | ||
📂 pages | ||
📂 editor | ||
📂 ui | ||
📄 EditorPage.tsx | ||
📄 Editor.tsx | ||
📄 index.ts | ||
``` | ||
|
||
## Rationale | ||
|
||
File segments are limited in their growth potential because everything has to be put in one file. This can get in the way of using this segment in the future for adjacent purposes. In this way, folder segments are better for the project long-term. |
98 changes: 98 additions & 0 deletions
98
packages/steiger-plugin-fsd/src/no-file-segments/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { expect, it } from 'vitest' | ||
import { join } from 'node:path' | ||
|
||
import { compareMessages, parseIntoFsdRoot } from '../_lib/prepare-test.js' | ||
import noFileSegments from './index.js' | ||
|
||
it('reports no errors on a project with only folder segments', async () => { | ||
const root = parseIntoFsdRoot(` | ||
📂 shared | ||
📂 ui | ||
📄 styles.ts | ||
📄 Button.tsx | ||
📄 TextField.tsx | ||
📄 index.ts | ||
📂 pages | ||
📂 editor | ||
📂 ui | ||
📄 EditorPage.tsx | ||
📄 Editor.tsx | ||
📄 index.ts | ||
`) | ||
|
||
expect(noFileSegments.check(root).diagnostics).toEqual([]) | ||
}) | ||
|
||
it('reports no errors on a project with folder segments on sliced layers', async () => { | ||
const root = parseIntoFsdRoot(` | ||
📂 shared | ||
📂 ui | ||
📄 styles.ts | ||
📄 Button.tsx | ||
📄 TextField.tsx | ||
📄 index.ts | ||
📂 features | ||
📂 comments | ||
📄 ui.tsx | ||
📄 index.ts | ||
📂 pages | ||
📂 editor | ||
📄 ui.tsx | ||
📄 index.ts | ||
📂 settings | ||
📂 ui | ||
📄 SettingsPage.tsx | ||
📄 index.ts | ||
`) | ||
|
||
expect(noFileSegments.check(root).diagnostics).toEqual([ | ||
{ | ||
message: `In "${join('features', 'comments')}", "ui.tsx" is a file segment. Prefer folder segments.`, | ||
}, | ||
{ | ||
message: `In "${join('pages', 'editor')}", "ui.tsx" is a file segment. Prefer folder segments.`, | ||
}, | ||
]) | ||
}) | ||
|
||
it('reports errors on a project with folder segments in Shared', async () => { | ||
const root = parseIntoFsdRoot(` | ||
📂 shared | ||
📄 routes.ts | ||
📂 ui | ||
📄 styles.ts | ||
📄 Button.tsx | ||
📄 TextField.tsx | ||
📄 index.ts | ||
📂 entities | ||
📂 user | ||
📂 ui | ||
📄 UserAvatar.tsx | ||
📄 index.ts | ||
📂 product | ||
📂 ui | ||
📄 ProductCard.tsx | ||
📄 index.ts | ||
📂 features | ||
📂 comments | ||
📂 ui | ||
📄 CommentCard.tsx | ||
📄 index.ts | ||
📂 pages | ||
📂 editor | ||
📂 ui | ||
📄 EditorPage.tsx | ||
📄 Editor.tsx | ||
📄 index.ts | ||
📂 settings | ||
📂 ui | ||
📄 SettingsPage.tsx | ||
📄 index.ts | ||
`) | ||
|
||
expect(noFileSegments.check(root).diagnostics.sort(compareMessages)).toEqual([ | ||
{ | ||
message: `On layer "shared", "routes.ts" is a file segment. Prefer folder segments.`, | ||
}, | ||
]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { basename, relative } from 'node:path' | ||
import { getLayers, getSlices, isSliced } from '@feature-sliced/filesystem' | ||
|
||
import type { Diagnostic, Rule } from '../types.js' | ||
|
||
const noFileSegments = { | ||
name: 'no-file-segments', | ||
check(root) { | ||
const diagnostics: Array<Diagnostic> = [] | ||
|
||
for (const [layerName, layer] of Object.entries(getLayers(root))) { | ||
if (!isSliced(layer)) { | ||
for (const child of layer.children) { | ||
if (child.type === 'file') { | ||
diagnostics.push({ | ||
message: `On layer "${layerName}", "${basename(child.path)}" is a file segment. Prefer folder segments.`, | ||
}) | ||
} | ||
} | ||
} else { | ||
for (const slice of Object.values(getSlices(layer))) { | ||
for (const child of slice.children) { | ||
if (child.type === 'file' && withoutExtension(basename(child.path)) !== 'index') { | ||
diagnostics.push({ | ||
message: `In "${relative(root.path, slice.path)}", "${basename(child.path)}" is a file segment. Prefer folder segments.`, | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return { diagnostics } | ||
}, | ||
} satisfies Rule | ||
|
||
export default noFileSegments | ||
|
||
/** | ||
* Cut away one layer of extension from a filename. | ||
* | ||
* @example | ||
* withoutExtension("index.tsx") // "index" | ||
* withoutExtension("index.spec.tsx") // "index.spec" | ||
* withoutExtension("index") // "index" | ||
*/ | ||
function withoutExtension(filename: string) { | ||
const lastDotIndex = filename.lastIndexOf('.') | ||
return lastDotIndex === -1 ? filename : filename.slice(0, lastDotIndex) | ||
} |