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 a required option for required vs. optional member ordering#5965
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
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
15 commits Select commitHold shift + click to select a range
00d0066 fix(eslint-plugin): [member-ordering] add requiredFirst as an option …
asdf93074c00c2a9 fix(eslint-plugin): [member-ordering] adding types so build passes.
asdf930740920426 fix(eslint-plugin): [member-ordering] fixing types so build passes.
asdf93074bf90b29 fix(eslint-plugin): [member-ordering] refactoring getIndexOfLastRequi…
asdf93074afcb6b9 fix(eslint-plugin): [member-ordering] additional test cases and handl…
asdf930748c3e9d4 fix(eslint-plugin): [member-ordering] linting fix.
asdf9307431e9a99 fix(eslint-plugin): [member-ordering] change requiredFirst to require…
asdf93074ce40b8e fix(eslint-plugin): [member-ordering] refactoring according to PR com…
asdf93074c09d42a fix(eslint-plugin): [member-ordering] refactoring for PR and adding a…
asdf93074abf6b8d fix(eslint-plugin): [member-ordering] refactoring for PR.
asdf93074f583399 fix(eslint-plugin): [member-ordering] adding test cases for coverage …
asdf930741691fa7 fix(eslint-plugin): [member-ordering] increasing coverage to pass check.
asdf93074bb8444f feat(eslint-plugin): [member-ordering] adding more tests to increase …
asdf93074832aa08 Updated name to optionalityOrder
JoshuaKGoldberg17c0595 Merge branch 'main' into feat/5596
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
94 changes: 93 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
145 changes: 129 additions & 16 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 |
|---|---|---|
| @@ -4,7 +4,10 @@ import naturalCompare from 'natural-compare-lite'; | ||
| import * as util from '../util'; | ||
| export type MessageIds = | ||
| | 'incorrectGroupOrder' | ||
| | 'incorrectOrder' | ||
| | 'incorrectRequiredMembersOrder'; | ||
| type MemberKind = | ||
| | 'call-signature' | ||
| @@ -47,12 +50,15 @@ type Order = AlphabeticalOrder | 'as-written'; | ||
| interface SortedOrderConfig { | ||
| memberTypes?: MemberType[] | 'never'; | ||
| optionalityOrder?: OptionalityOrder; | ||
| order: Order; | ||
| } | ||
| type OrderConfig = MemberType[] | SortedOrderConfig | 'never'; | ||
| type Member = TSESTree.ClassElement | TSESTree.TypeElement; | ||
| type OptionalityOrder = 'optional-first' | 'required-first'; | ||
| export type Options = [ | ||
| { | ||
| default?: OrderConfig; | ||
| @@ -101,6 +107,10 @@ const objectConfig = (memberTypes: MemberType[]): JSONSchema.JSONSchema4 => ({ | ||
| 'natural-case-insensitive', | ||
| ], | ||
| }, | ||
| optionalityOrder: { | ||
| type: 'string', | ||
| enum: ['optional-first', 'required-first'], | ||
| }, | ||
| }, | ||
| additionalProperties: false, | ||
| }); | ||
| @@ -405,6 +415,26 @@ function getMemberName( | ||
| } | ||
| } | ||
| /** | ||
| * Returns true if the member is optional based on the member type. | ||
| * | ||
| * @param node the node to be evaluated. | ||
| * | ||
| * @returns Whether the member is optional, or false if it cannot be optional at all. | ||
| */ | ||
| function isMemberOptional(node: Member): boolean { | ||
| switch (node.type) { | ||
| case AST_NODE_TYPES.TSPropertySignature: | ||
| case AST_NODE_TYPES.TSMethodSignature: | ||
| case AST_NODE_TYPES.TSAbstractPropertyDefinition: | ||
| case AST_NODE_TYPES.PropertyDefinition: | ||
| case AST_NODE_TYPES.TSAbstractMethodDefinition: | ||
| case AST_NODE_TYPES.MethodDefinition: | ||
| return !!node.optional; | ||
| } | ||
| return false; | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| } | ||
| /** | ||
| * Gets the calculated rank using the provided method definition. | ||
| * The algorithm is as follows: | ||
| @@ -561,6 +591,7 @@ export default util.createRule<Options, MessageIds>({ | ||
| 'Member {{member}} should be declared before member {{beforeMember}}.', | ||
| incorrectGroupOrder: | ||
| 'Member {{name}} should be declared before all {{rank}} definitions.', | ||
| incorrectRequiredMembersOrder: `Member {{member}} should be declared after all {{optionalOrRequired}} members.`, | ||
| }, | ||
| schema: [ | ||
| { | ||
| @@ -725,6 +756,59 @@ export default util.createRule<Options, MessageIds>({ | ||
| } | ||
| } | ||
| /** | ||
| * Checks if the order of optional and required members is correct based | ||
| * on the given 'required' parameter. | ||
| * | ||
| * @param members Members to be validated. | ||
| * @param optionalityOrder Where to place optional members, if not intermixed. | ||
| * | ||
| * @return True if all required and optional members are correctly sorted. | ||
| */ | ||
| function checkRequiredOrder( | ||
| members: Member[], | ||
| optionalityOrder: OptionalityOrder | undefined, | ||
| ): boolean { | ||
| const switchIndex = members.findIndex( | ||
| (member, i) => | ||
| i && isMemberOptional(member) !== isMemberOptional(members[i - 1]), | ||
| ); | ||
| const report = (member: Member): void => | ||
| context.report({ | ||
| messageId: 'incorrectRequiredMembersOrder', | ||
| loc: member.loc, | ||
| data: { | ||
| member: getMemberName(member, context.getSourceCode()), | ||
| optionalOrRequired: | ||
| optionalityOrder === 'optional-first' ? 'required' : 'optional', | ||
| }, | ||
| }); | ||
| // if the optionality of the first item is correct (based on optionalityOrder) | ||
| // then the first 0 inclusive to switchIndex exclusive members all | ||
| // have the correct optionality | ||
| if ( | ||
| isMemberOptional(members[0]) !== | ||
| (optionalityOrder === 'required-first') | ||
| ) { | ||
| report(members[0]); | ||
| return false; | ||
| } | ||
| for (let i = switchIndex + 1; i < members.length; i++) { | ||
| if ( | ||
| isMemberOptional(members[i]) !== | ||
| isMemberOptional(members[switchIndex]) | ||
| ) { | ||
| report(members[switchIndex]); | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| /** | ||
| * Validates if all members are correctly sorted. | ||
| * | ||
| @@ -743,33 +827,62 @@ export default util.createRule<Options, MessageIds>({ | ||
| // Standardize config | ||
| let order: Order | undefined; | ||
| let memberTypes: string | MemberType[] | undefined; | ||
| let optionalityOrder: OptionalityOrder | undefined; | ||
| // returns true if everything is good and false if an error was reported | ||
| const checkOrder = (memberSet: Member[]): boolean => { | ||
| const hasAlphaSort = !!(order && order !== 'as-written'); | ||
| // Check order | ||
| if (Array.isArray(memberTypes)) { | ||
| const grouped = checkGroupSort( | ||
| memberSet, | ||
| memberTypes, | ||
| supportsModifiers, | ||
| ); | ||
| if (grouped === null) { | ||
| return false; | ||
| } | ||
| if (hasAlphaSort) { | ||
| return !grouped.some( | ||
| groupMember => | ||
| !checkAlphaSort(groupMember, order as AlphabeticalOrder), | ||
| ); | ||
| } | ||
| } else if (hasAlphaSort) { | ||
| return checkAlphaSort(memberSet, order as AlphabeticalOrder); | ||
| } | ||
| return true; | ||
| }; | ||
| if (Array.isArray(orderConfig)) { | ||
| memberTypes = orderConfig; | ||
| } else { | ||
| order = orderConfig.order; | ||
| memberTypes = orderConfig.memberTypes; | ||
| optionalityOrder = orderConfig.optionalityOrder; | ||
| } | ||
| if (!optionalityOrder) { | ||
| checkOrder(members); | ||
| return; | ||
| } | ||
| const switchIndex = members.findIndex( | ||
| (member, i) => | ||
| i && isMemberOptional(member) !== isMemberOptional(members[i - 1]), | ||
| ); | ||
| if (switchIndex !== -1) { | ||
| if (!checkRequiredOrder(members, optionalityOrder)) { | ||
| return; | ||
| } | ||
| checkOrder(members.slice(0, switchIndex)); | ||
| checkOrder(members.slice(switchIndex)); | ||
| } | ||
| } | ||
24 changes: 24 additions & 0 deletionspackages/eslint-plugin/src/util/misc.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
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.