-
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.
feat: extend the functionality of
inconsistent-naming
- Loading branch information
Showing
4 changed files
with
226 additions
and
27 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,5 @@ | ||
--- | ||
'@feature-sliced/steiger-plugin': minor | ||
--- | ||
|
||
Extend the functionality of `inconsistent-naming` to support multi-word names and handle rename collisions |
90 changes: 90 additions & 0 deletions
90
packages/steiger-plugin-fsd/src/inconsistent-naming/get-main-subject.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,90 @@ | ||
import { wordPattern } from '../inconsistent-naming-scheme/detect-naming-scheme.js' | ||
|
||
/** | ||
* Extract the main subject in a multi-word subject name. | ||
* | ||
* @example | ||
* getMainSubject("a book with pages") // "book" | ||
* getMainSubject("admin-users") // "users" | ||
* getMainSubject("receiptsByOrder") // "receipts" | ||
*/ | ||
export function getMainSubject(name: string) { | ||
const words = [...name.matchAll(wordPattern)] | ||
.map((match) => match[0]) | ||
.filter((word) => !articles.includes(word.toLocaleLowerCase())) | ||
|
||
const prepositionIndex = words.findIndex((word) => prepositions.includes(word.toLocaleLowerCase())) | ||
if (prepositionIndex === -1) { | ||
return words[words.length - 1] | ||
} | ||
const mainPart = words.slice(0, prepositionIndex) | ||
return mainPart[mainPart.length - 1] | ||
} | ||
|
||
if (import.meta.vitest) { | ||
const { test, expect } = import.meta.vitest | ||
|
||
test('getMainSubject', () => { | ||
expect(getMainSubject('a book with pages')).toEqual('book') | ||
expect(getMainSubject('admin-users')).toEqual('users') | ||
expect(getMainSubject('receiptsByOrder')).toEqual('receipts') | ||
}) | ||
} | ||
|
||
const articles = ['a', 'an', 'the'] | ||
const prepositions = [ | ||
'about', | ||
'above', | ||
'across', | ||
'after', | ||
'against', | ||
'along', | ||
'among', | ||
'around', | ||
'at', | ||
'before', | ||
'behind', | ||
'below', | ||
'beneath', | ||
'beside', | ||
'besides', | ||
'between', | ||
'beyond', | ||
'but', | ||
'by', | ||
'concerning', | ||
'despite', | ||
'down', | ||
'during', | ||
'except', | ||
'excepting', | ||
'for', | ||
'from', | ||
'in', | ||
'inside', | ||
'into', | ||
'like', | ||
'near', | ||
'of', | ||
'off', | ||
'on', | ||
'onto', | ||
'out', | ||
'outside', | ||
'over', | ||
'past', | ||
'regarding', | ||
'since', | ||
'through', | ||
'throughout', | ||
'to', | ||
'toward', | ||
'under', | ||
'underneath', | ||
'until', | ||
'up', | ||
'upon', | ||
'with', | ||
'within', | ||
'without', | ||
] |
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