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): [explicit-member-accessibility] suggest adding explicit accessibility specifiers#5492
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:mainfromjtbandes:fix-explicit-member-accessibilityAug 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
37bfd8f feat(eslint-plugin): [explicit-member-accessibility] autofix missing …
jtbandesa955225 Convert auto-fix to suggestions
jtbandesb7a4d69 Merge branch 'main' into fix-explicit-member-accessibility
jtbandes0d57ef5 Merge remote-tracking branch 'upstream/main' into fix-explicit-member…
jtbandes5fd7ae3 remove trim()s
jtbandesd38da51 Update packages/eslint-plugin/src/rules/explicit-member-accessibility.ts
jtbandes65dd4ce Merge remote-tracking branch 'origin/fix-explicit-member-accessibilit…
jtbandes4623489 merge message IDs
jtbandesb46d54e !
jtbandes143c1aa Merge branch 'main' into fix-explicit-member-accessibility
jtbandes6395b8f Merge branch 'main' into fix-explicit-member-accessibility
jtbandes5471380 Merge branch 'main' into fix-explicit-member-accessibility
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
155 changes: 101 additions & 54 deletionspackages/eslint-plugin/src/rules/explicit-member-accessibility.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 |
|---|---|---|
| @@ -25,7 +25,10 @@ interface Config { | ||
| type Options = [Config]; | ||
| type MessageIds = | ||
| | 'unwantedPublicAccessibility' | ||
| | 'missingAccessibility' | ||
| | 'addExplicitAccessibility'; | ||
| const accessibilityLevel = { | ||
| oneOf: [ | ||
| @@ -47,6 +50,7 @@ const accessibilityLevel = { | ||
| export default util.createRule<Options, MessageIds>({ | ||
| name: 'explicit-member-accessibility', | ||
| meta: { | ||
| hasSuggestions: true, | ||
| type: 'problem', | ||
| docs: { | ||
| description: | ||
| @@ -60,6 +64,7 @@ export default util.createRule<Options, MessageIds>({ | ||
| 'Missing accessibility modifier on {{type}} {{name}}.', | ||
| unwantedPublicAccessibility: | ||
| 'Public accessibility modifier on {{type}} {{name}}.', | ||
| addExplicitAccessibility: "Add '{{ type }}' accessibility modifier", | ||
| }, | ||
| schema: [ | ||
| { | ||
| @@ -103,26 +108,6 @@ export default util.createRule<Options, MessageIds>({ | ||
| const propCheck = overrides.properties ?? baseCheck; | ||
| const paramPropCheck = overrides.parameterProperties ?? baseCheck; | ||
| const ignoredMethodNames = new Set(option.ignoredMethodNames ?? []); | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| /** | ||
| * Checks if a method declaration has an accessibility modifier. | ||
| @@ -164,20 +149,25 @@ export default util.createRule<Options, MessageIds>({ | ||
| check === 'no-public' && | ||
| methodDefinition.accessibility === 'public' | ||
| ) { | ||
| context.report({ | ||
| node: methodDefinition, | ||
| messageId: 'unwantedPublicAccessibility', | ||
| data: { | ||
| type: nodeType, | ||
| name: methodName, | ||
| }, | ||
| fix: getUnwantedPublicAccessibilityFixer(methodDefinition), | ||
| }); | ||
| } else if (check === 'explicit' && !methodDefinition.accessibility) { | ||
| context.report({ | ||
| node: methodDefinition, | ||
| messageId: 'missingAccessibility', | ||
| data: { | ||
| type: nodeType, | ||
| name: methodName, | ||
| }, | ||
| suggest: getMissingAccessibilitySuggestions(methodDefinition), | ||
| }); | ||
| } | ||
| } | ||
| @@ -223,6 +213,48 @@ export default util.createRule<Options, MessageIds>({ | ||
| }; | ||
| } | ||
| /** | ||
| * Creates a fixer that adds a "public" keyword with following spaces | ||
| */ | ||
| function getMissingAccessibilitySuggestions( | ||
| node: | ||
| | TSESTree.MethodDefinition | ||
| | TSESTree.PropertyDefinition | ||
| | TSESTree.TSAbstractMethodDefinition | ||
| | TSESTree.TSAbstractPropertyDefinition | ||
| | TSESTree.TSParameterProperty, | ||
| ): TSESLint.ReportSuggestionArray<MessageIds> { | ||
| function fix( | ||
| accessibility: TSESTree.Accessibility, | ||
| fixer: TSESLint.RuleFixer, | ||
| ): TSESLint.RuleFix | null { | ||
| if (node?.decorators?.length) { | ||
| const lastDecorator = node.decorators[node.decorators.length - 1]; | ||
| const nextToken = sourceCode.getTokenAfter(lastDecorator)!; | ||
| return fixer.insertTextBefore(nextToken, `${accessibility} `); | ||
| } | ||
| return fixer.insertTextBefore(node, `${accessibility} `); | ||
| } | ||
| return [ | ||
| { | ||
| messageId: 'addExplicitAccessibility', | ||
| data: { type: 'public' }, | ||
| fix: fixer => fix('public', fixer), | ||
| }, | ||
| { | ||
| messageId: 'addExplicitAccessibility', | ||
| data: { type: 'private' }, | ||
| fix: fixer => fix('private', fixer), | ||
| }, | ||
| { | ||
| messageId: 'addExplicitAccessibility', | ||
| data: { type: 'protected' }, | ||
| fix: fixer => fix('protected', fixer), | ||
| }, | ||
| ]; | ||
| } | ||
| /** | ||
| * Checks if property has an accessibility modifier. | ||
| * @param propertyDefinition The node representing a PropertyDefinition. | ||
| @@ -246,23 +278,28 @@ export default util.createRule<Options, MessageIds>({ | ||
| propCheck === 'no-public' && | ||
| propertyDefinition.accessibility === 'public' | ||
| ) { | ||
| context.report({ | ||
| node: propertyDefinition, | ||
| messageId: 'unwantedPublicAccessibility', | ||
| data: { | ||
| type: nodeType, | ||
| name: propertyName, | ||
| }, | ||
| fix: getUnwantedPublicAccessibilityFixer(propertyDefinition), | ||
| }); | ||
| } else if ( | ||
| propCheck === 'explicit' && | ||
| !propertyDefinition.accessibility | ||
| ) { | ||
| context.report({ | ||
| node: propertyDefinition, | ||
| messageId: 'missingAccessibility', | ||
| data: { | ||
| type: nodeType, | ||
| name: propertyName, | ||
| }, | ||
| suggest: getMissingAccessibilitySuggestions(propertyDefinition), | ||
| }); | ||
| } | ||
| } | ||
| @@ -291,19 +328,29 @@ export default util.createRule<Options, MessageIds>({ | ||
| switch (paramPropCheck) { | ||
| case 'explicit': { | ||
| if (!node.accessibility) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'missingAccessibility', | ||
| data: { | ||
| type: nodeType, | ||
| name: nodeName, | ||
| }, | ||
| suggest: getMissingAccessibilitySuggestions(node), | ||
| }); | ||
| } | ||
| break; | ||
| } | ||
| case 'no-public': { | ||
| if (node.accessibility === 'public' && node.readonly) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'unwantedPublicAccessibility', | ||
| data: { | ||
| type: nodeType, | ||
| name: nodeName, | ||
| }, | ||
| fix: getUnwantedPublicAccessibilityFixer(node), | ||
| }); | ||
| } | ||
| break; | ||
| } | ||
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.