-
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.
Add the
typo-in-layer-name
rule (#102)
- Loading branch information
Showing
10 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
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,6 @@ | ||
--- | ||
'@feature-sliced/steiger-plugin': minor | ||
'steiger': minor | ||
--- | ||
|
||
Add typo-in-layer-name rule |
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
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
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
29 changes: 29 additions & 0 deletions
29
packages/steiger-plugin-fsd/src/typo-in-layer-name/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,29 @@ | ||
# `typo-in-layer-name` | ||
|
||
Ensure that all layers are named consistently without any typos. | ||
|
||
Examples of project structures that pass this rule: | ||
|
||
``` | ||
📂 shared | ||
📂 entities | ||
📂 features | ||
📂 widgets | ||
📂 pages | ||
📂 app | ||
``` | ||
|
||
Examples of project structures that fail this rule: | ||
|
||
``` | ||
📂 shraed // ❌ | ||
📂 entities | ||
📂 fietures // ❌ | ||
📂 wigdets // ❌ | ||
📂 page // ❌ | ||
📂 app | ||
``` | ||
|
||
## Rationale | ||
|
||
The methodology contains a standardized set of layers. Enforcing these naming conventions is important for other developers, as well as for other rules of the linter to work correctly. |
100 changes: 100 additions & 0 deletions
100
packages/steiger-plugin-fsd/src/typo-in-layer-name/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,100 @@ | ||
import { expect, it } from 'vitest' | ||
|
||
import typoInLayerName from './index.js' | ||
import { joinFromRoot, parseIntoFsdRoot } from '../_lib/prepare-test.js' | ||
|
||
it('reports no errors on a project without typos in layer names', () => { | ||
const root = parseIntoFsdRoot(` | ||
📂 shared | ||
📂 entities | ||
📂 features | ||
📂 widgets | ||
📂 pages | ||
📂 app | ||
`) | ||
|
||
expect(typoInLayerName.check(root)).toEqual({ diagnostics: [] }) | ||
}) | ||
|
||
it('reports errors on a project with typos in layer names', () => { | ||
const root = parseIntoFsdRoot(` | ||
📂 shraed | ||
📂 entities | ||
📂 fietures | ||
📂 wigdets | ||
📂 page | ||
📂 app | ||
`) | ||
|
||
const diagnostics = typoInLayerName.check(root).diagnostics | ||
expect(diagnostics).toEqual([ | ||
{ | ||
message: 'Layer "page" potentially contains a typo. Did you mean "pages"?', | ||
location: { path: joinFromRoot('page') }, | ||
}, | ||
{ | ||
message: 'Layer "shraed" potentially contains a typo. Did you mean "shared"?', | ||
location: { path: joinFromRoot('shraed') }, | ||
}, | ||
{ | ||
message: 'Layer "fietures" potentially contains a typo. Did you mean "features"?', | ||
location: { path: joinFromRoot('fietures') }, | ||
}, | ||
{ | ||
message: 'Layer "wigdets" potentially contains a typo. Did you mean "widgets"?', | ||
location: { path: joinFromRoot('wigdets') }, | ||
}, | ||
]) | ||
}) | ||
|
||
it('reports no errors on a project with custom layers if base layers are present', () => { | ||
const root = parseIntoFsdRoot(` | ||
📂 shared | ||
📂 shapes | ||
📂 entities | ||
📂 entries | ||
📂 features | ||
📂 fixtures | ||
📂 widgets | ||
📂 pages | ||
📂 places | ||
📂 app | ||
📂 amp | ||
`) | ||
|
||
expect(typoInLayerName.check(root)).toEqual({ diagnostics: [] }) | ||
}) | ||
|
||
it('reports errors on a project with custom layers if base layers are absent', () => { | ||
const root = parseIntoFsdRoot(` | ||
📂 shapes | ||
📂 entries | ||
📂 fixtures | ||
📂 places | ||
📂 amp | ||
`) | ||
|
||
const diagnostics = typoInLayerName.check(root).diagnostics | ||
expect(diagnostics).toEqual([ | ||
{ | ||
message: 'Layer "amp" potentially contains a typo. Did you mean "app"?', | ||
location: { path: joinFromRoot('amp') }, | ||
}, | ||
{ | ||
message: 'Layer "shapes" potentially contains a typo. Did you mean "shared"?', | ||
location: { path: joinFromRoot('shapes') }, | ||
}, | ||
{ | ||
message: 'Layer "entries" potentially contains a typo. Did you mean "entities"?', | ||
location: { path: joinFromRoot('entries') }, | ||
}, | ||
{ | ||
message: 'Layer "fixtures" potentially contains a typo. Did you mean "features"?', | ||
location: { path: joinFromRoot('fixtures') }, | ||
}, | ||
{ | ||
message: 'Layer "places" potentially contains a typo. Did you mean "pages"?', | ||
location: { path: joinFromRoot('places') }, | ||
}, | ||
]) | ||
}) |
68 changes: 68 additions & 0 deletions
68
packages/steiger-plugin-fsd/src/typo-in-layer-name/index.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,68 @@ | ||
import type { Diagnostic, Rule } from '@steiger/types' | ||
import { NAMESPACE } from '../constants.js' | ||
import { LayerName, layerSequence } from '@feature-sliced/filesystem' | ||
import { distance } from 'fastest-levenshtein' | ||
import { basename } from 'node:path' | ||
import { joinFromRoot } from '../_lib/prepare-test.js' | ||
|
||
const LEVENSHTEIN_DISTANCE_UPPER_BOUND = 3 | ||
|
||
const typoInLayerName = { | ||
name: `${NAMESPACE}/typo-in-layer-name`, | ||
check(root) { | ||
const diagnostics: Array<Diagnostic> = [] | ||
|
||
// construct list of suggestions, like [{ input: 'shraed', suggestion: 'shared', distance: 2 }, ...], | ||
// limit Levenshtein distance upper bound to 3, | ||
// sort by Levenshtein distance in ascending order, | ||
const suggestionsList = root.children | ||
.filter((child) => child.type === 'folder') | ||
.flatMap((child) => { | ||
const layer = basename(child.path) | ||
|
||
return layerSequence | ||
.map((sequenceLayer) => ({ | ||
input: layer, | ||
suggestion: sequenceLayer, | ||
distance: distance(layer, sequenceLayer), | ||
})) | ||
.filter((layer) => layer.distance <= LEVENSHTEIN_DISTANCE_UPPER_BOUND) | ||
}) | ||
.sort((a, b) => a.distance - b.distance) | ||
|
||
const processedInputs: string[] = [] | ||
const suggestedLayers: LayerName[] = [] | ||
|
||
suggestionsList.forEach((layer) => { | ||
// if Levenshtein distance is 0, the layer name is correct - add it as a "suggestion" | ||
if (layer.distance === 0) { | ||
suggestedLayers.push(layer.suggestion) | ||
|
||
return | ||
} | ||
|
||
// if the input is already processed, the suggestion for this input is already added | ||
if (processedInputs.includes(layer.input)) { | ||
return | ||
} | ||
|
||
// if the suggestion is already added, it cannot be used for this input | ||
if (suggestedLayers.includes(layer.suggestion)) { | ||
return | ||
} | ||
|
||
// mark the input as processed & add suitable suggestion | ||
processedInputs.push(layer.input) | ||
suggestedLayers.push(layer.suggestion) | ||
|
||
diagnostics.push({ | ||
message: `Layer "${layer.input}" potentially contains a typo. Did you mean "${layer.suggestion}"?`, | ||
location: { path: joinFromRoot(layer.input) }, | ||
}) | ||
}) | ||
|
||
return { diagnostics } | ||
}, | ||
} satisfies Rule | ||
|
||
export default typoInLayerName |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.