diff --git a/packages/steiger-plugin-fsd/src/no-file-segments/README.md b/packages/steiger-plugin-fsd/src/no-file-segments/README.md new file mode 100644 index 0000000..b9ec134 --- /dev/null +++ b/packages/steiger-plugin-fsd/src/no-file-segments/README.md @@ -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. diff --git a/packages/steiger-plugin-fsd/src/no-file-segments/index.spec.ts b/packages/steiger-plugin-fsd/src/no-file-segments/index.spec.ts new file mode 100644 index 0000000..2d1df46 --- /dev/null +++ b/packages/steiger-plugin-fsd/src/no-file-segments/index.spec.ts @@ -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.`, + }, + ]) +}) diff --git a/packages/steiger-plugin-fsd/src/no-file-segments/index.ts b/packages/steiger-plugin-fsd/src/no-file-segments/index.ts new file mode 100644 index 0000000..6f323e0 --- /dev/null +++ b/packages/steiger-plugin-fsd/src/no-file-segments/index.ts @@ -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 = [] + + 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) +}