diff --git a/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md b/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md index 96295809138..a8625778391 100644 --- a/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md +++ b/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md @@ -20,6 +20,7 @@ type AccessibilityLevel = type Options = { accessibility?: AccessibilityLevel; + ignoredMethodNames?: string[]; overrides?: { accessors?: AccessibilityLevel; constructors?: AccessibilityLevel; @@ -310,6 +311,25 @@ class Animal { } ``` +### Except specific methods + +If you want to ignore some specific methods, you can do it by specifing method names. Note that this option does not care for the context, and will ignore every method with these names, which could lead to it missing some cases. You should use this sparingly. +e.g. `[ { ignoredMethodNames: ['specificMethod', 'whateverMethod'] } ]` + +```ts +class Animal { + get specificMethod() { + console.log('No error because you specified this method on option'); + } + get whateverMethod() { + console.log('No error because you specified this method on option'); + } + public get otherMethod() { + console.log('This method comply with this rule'); + } +} +``` + ## When Not To Use It If you think defaulting to public is a good default, then you should consider using the `no-public` setting. If you want to mix implicit and explicit public members then disable this rule. diff --git a/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts b/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts index a1d61d56ded..864c630da17 100644 --- a/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts +++ b/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts @@ -11,6 +11,7 @@ type AccessibilityLevel = interface Config { accessibility?: AccessibilityLevel; + ignoredMethodNames?: string[]; overrides?: { accessors?: AccessibilityLevel; constructors?: AccessibilityLevel; @@ -57,8 +58,15 @@ export default util.createRule({ properties: accessibilityLevel, parameterProperties: accessibilityLevel, }, + additionalProperties: false, }, + ignoredMethodNames: { + type: 'array', + items: { + type: 'string', + }, + }, }, additionalProperties: false, }, @@ -74,7 +82,7 @@ export default util.createRule({ const methodCheck = overrides.methods || baseCheck; const propCheck = overrides.properties || baseCheck; const paramPropCheck = overrides.parameterProperties || baseCheck; - + const ignoredMethodNames = new Set(option.ignoredMethodNames || []); /** * Generates the report for rule violations */ @@ -116,14 +124,16 @@ export default util.createRule({ nodeType = `${methodDefinition.kind} property accessor`; break; } - if (check === 'off') { - return; - } const methodName = util.getNameFromClassMember( methodDefinition, sourceCode, ); + + if (check === 'off' || ignoredMethodNames.has(methodName)) { + return; + } + if ( check === 'no-public' && methodDefinition.accessibility === 'public' diff --git a/packages/eslint-plugin/tests/rules/explicit-member-accessibility.test.ts b/packages/eslint-plugin/tests/rules/explicit-member-accessibility.test.ts index 051cfb30642..7eb6bcd076b 100644 --- a/packages/eslint-plugin/tests/rules/explicit-member-accessibility.test.ts +++ b/packages/eslint-plugin/tests/rules/explicit-member-accessibility.test.ts @@ -258,6 +258,66 @@ class Test { }, ], }, + { + filename: 'test.ts', + code: ` +class Test { + public getX () { + return this.x + } +} + `, + options: [ + { + ignoredMethodNames: ['getX'], + }, + ], + }, + { + filename: 'test.ts', + code: ` +class Test { + public static getX () { + return this.x + } +} + `, + options: [ + { + ignoredMethodNames: ['getX'], + }, + ], + }, + { + filename: 'test.ts', + code: ` +class Test { + get getX () { + return this.x + } +} + `, + options: [ + { + ignoredMethodNames: ['getX'], + }, + ], + }, + { + filename: 'test.ts', + code: ` +class Test { + getX () { + return this.x + } +} + `, + options: [ + { + ignoredMethodNames: ['getX'], + }, + ], + }, ], invalid: [ {