Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [member-ordering] add natural sort order#5662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
JoshuaKGoldberg merged 12 commits intotypescript-eslint:mainfromJoshuaKGoldberg:member-ordering-natural-orderOct 24, 2022
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
12 commits Select commitHold shift + click to select a range
91816a9 [WIP] feat(eslint-plugin): [member-ordering] add natural sort order
JoshuaKGoldberg519eb05 Fix yarn.lock and split option on case sensitivity
JoshuaKGoldberg09227fc Document it too
JoshuaKGoldberg155688d Remove last todos
JoshuaKGoldbergf477863 Merge branch 'main'
JoshuaKGoldberg064f9f7 Merge branch 'main' into member-ordering-natural-order
JoshuaKGoldbergea0da9e Merge branch 'main' into member-ordering-natural-order
JoshuaKGoldberg8503b5c Merge branch 'main' into member-ordering-natural-order
JoshuaKGoldberga918d3b Merge branch 'main' into member-ordering-natural-order
JoshuaKGoldbergf9b8f68 Move member-ordering sub-tests into sub-dirs
JoshuaKGoldberg2beed40 Merge branch 'main' into member-ordering-natural-order
JoshuaKGoldberg6d54800 Merge branch 'main' into member-ordering-natural-order
JoshuaKGoldbergFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
18 changes: 17 additions & 1 deletionpackages/eslint-plugin/docs/rules/member-ordering.md
This file contains hidden or 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
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/package.json
This file contains hidden or 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 |
|---|---|---|
| @@ -49,6 +49,7 @@ | ||
| "@typescript-eslint/utils": "5.41.0", | ||
| "debug": "^4.3.4", | ||
| "ignore": "^5.2.0", | ||
| "natural-compare-lite": "^1.4.0", | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| "regexpp": "^3.2.0", | ||
| "semver": "^7.3.7", | ||
| "tsutils": "^3.21.0" | ||
| @@ -57,6 +58,7 @@ | ||
| "@types/debug": "*", | ||
| "@types/json-schema": "*", | ||
| "@types/marked": "*", | ||
| "@types/natural-compare-lite": "^1.4.0", | ||
| "@types/prettier": "*", | ||
| "chalk": "^5.0.1", | ||
| "json-schema": "*", | ||
53 changes: 38 additions & 15 deletionspackages/eslint-plugin/src/rules/member-ordering.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import type { JSONSchema, TSESLint, TSESTree } from '@typescript-eslint/utils'; | ||
| import { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
| import naturalCompare from 'natural-compare-lite'; | ||
| import * as util from '../util'; | ||
| @@ -34,10 +35,13 @@ type BaseMemberType = | ||
| type MemberType = BaseMemberType | BaseMemberType[]; | ||
| typeAlphabeticalOrder = | ||
| | 'alphabetically' | ||
| | 'alphabetically-case-insensitive' | ||
| | 'natural' | ||
| | 'natural-case-insensitive'; | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| type Order = AlphabeticalOrder | 'as-written'; | ||
| interface SortedOrderConfig { | ||
| memberTypes?: MemberType[] | 'never'; | ||
| @@ -87,7 +91,13 @@ const objectConfig = (memberTypes: MemberType[]): JSONSchema.JSONSchema4 => ({ | ||
| }, | ||
| order: { | ||
| type: 'string', | ||
| enum: [ | ||
| 'alphabetically', | ||
| 'alphabetically-case-insensitive', | ||
| 'as-written', | ||
| 'natural', | ||
| 'natural-case-insensitive', | ||
| ], | ||
| }, | ||
| }, | ||
| additionalProperties: false, | ||
| @@ -629,7 +639,7 @@ export default util.createRule<Options, MessageIds>({ | ||
| */ | ||
| function checkAlphaSort( | ||
| members: Member[], | ||
| order: AlphabeticalOrder, | ||
| ): boolean { | ||
| let previousName = ''; | ||
| let isCorrectlySorted = true; | ||
| @@ -640,11 +650,7 @@ export default util.createRule<Options, MessageIds>({ | ||
| // Note: Not all members have names | ||
| if (name) { | ||
| if (naturalOutOfOrder(name, previousName, order)) { | ||
| context.report({ | ||
| node: member, | ||
| messageId: 'incorrectOrder', | ||
| @@ -664,6 +670,25 @@ export default util.createRule<Options, MessageIds>({ | ||
| return isCorrectlySorted; | ||
| } | ||
| function naturalOutOfOrder( | ||
| name: string, | ||
| previousName: string, | ||
| order: AlphabeticalOrder, | ||
| ): boolean { | ||
| switch (order) { | ||
| case 'alphabetically': | ||
| return name < previousName; | ||
| case 'alphabetically-case-insensitive': | ||
| return name.toLowerCase() < previousName.toLowerCase(); | ||
| case 'natural': | ||
| return naturalCompare(name, previousName) !== 1; | ||
| case 'natural-case-insensitive': | ||
| return ( | ||
| naturalCompare(name.toLowerCase(), previousName.toLowerCase()) !== 1 | ||
| ); | ||
| } | ||
| } | ||
| /** | ||
| * Validates if all members are correctly sorted. | ||
| * | ||
| @@ -681,7 +706,7 @@ export default util.createRule<Options, MessageIds>({ | ||
| } | ||
| // Standardize config | ||
| let order: Order |undefined; | ||
| let memberTypes; | ||
| if (Array.isArray(orderConfig)) { | ||
| @@ -691,9 +716,7 @@ export default util.createRule<Options, MessageIds>({ | ||
| memberTypes = orderConfig.memberTypes; | ||
| } | ||
| const hasAlphaSort = !!(order && order !== 'as-written'); | ||
| // Check order | ||
| if (Array.isArray(memberTypes)) { | ||
| @@ -706,11 +729,11 @@ export default util.createRule<Options, MessageIds>({ | ||
| if (hasAlphaSort) { | ||
| grouped.some( | ||
| groupMember => | ||
| !checkAlphaSort(groupMember,order as AlphabeticalOrder), | ||
| ); | ||
| } | ||
| } else if (hasAlphaSort) { | ||
| checkAlphaSort(members,order as AlphabeticalOrder); | ||
| } | ||
| } | ||
6 changes: 3 additions & 3 deletions...abetically-case-insensitive-order.test.ts → ...abetically-case-insensitive-order.test.ts
This file contains hidden or 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
6 changes: 3 additions & 3 deletions...ber-ordering-alphabetically-order.test.ts → ...ber-ordering-alphabetically-order.test.ts
This file contains hidden or 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
135 changes: 135 additions & 0 deletions...plugin/tests/rules/member-ordering/member-ordering-natural-case-insensitive-order.test.ts
This file contains hidden or 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,135 @@ | ||
| import rule from '../../../src/rules/member-ordering'; | ||
| import { RuleTester } from '../../RuleTester'; | ||
| const ruleTester = new RuleTester({ | ||
| parser: '@typescript-eslint/parser', | ||
| }); | ||
| ruleTester.run('member-ordering-natural-order', rule, { | ||
| valid: [ | ||
| { | ||
| code: ` | ||
| interface Example { | ||
| 1: number; | ||
| 5: number; | ||
| 10: number; | ||
| } | ||
| `, | ||
| options: [ | ||
| { | ||
| default: { | ||
| order: 'natural-case-insensitive', | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: ` | ||
| interface Example { | ||
| new (): unknown; | ||
| a1(): void; | ||
| a5(): void; | ||
| a10(): void; | ||
| B1(): void; | ||
| B5(): void; | ||
| B10(): void; | ||
| a1: number; | ||
| a5: number; | ||
| a10: number; | ||
| B1: number; | ||
| B5: number; | ||
| B10: number; | ||
| } | ||
| `, | ||
| options: [ | ||
| { | ||
| default: { | ||
| memberTypes: ['constructor', 'method', 'field'], | ||
| order: 'natural-case-insensitive', | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: ` | ||
| interface Example { | ||
| 1: number; | ||
| 10: number; | ||
| 5: number; | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| messageId: 'incorrectOrder', | ||
| data: { | ||
| beforeMember: 10, | ||
| member: 5, | ||
| }, | ||
| line: 5, | ||
| column: 3, | ||
| }, | ||
| ], | ||
| options: [ | ||
| { | ||
| default: { | ||
| order: 'natural-case-insensitive', | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: ` | ||
| interface Example { | ||
| new (): unknown; | ||
| a1(): void; | ||
| a10(): void; | ||
| a5(): void; | ||
| B5(): void; | ||
| B10(): void; | ||
| B1(): void; | ||
| a5: number; | ||
| a10: number; | ||
| B1: number; | ||
| a1: number; | ||
| B5: number; | ||
| B10: number; | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| column: 3, | ||
| data: { | ||
| beforeMember: 'a10', | ||
| member: 'a5', | ||
| }, | ||
| line: 7, | ||
| messageId: 'incorrectOrder', | ||
| }, | ||
| { | ||
| column: 3, | ||
| data: { | ||
| beforeMember: 'B10', | ||
| member: 'B1', | ||
| }, | ||
| line: 10, | ||
| messageId: 'incorrectOrder', | ||
| }, | ||
| ], | ||
| options: [ | ||
| { | ||
| default: { | ||
| memberTypes: ['constructor', 'method', 'field'], | ||
| order: 'natural-case-insensitive', | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.