Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): [explicit-member-accessibility] add support of "ignoredMethodNames" #895

Merged
20 changes: 20 additions & 0 deletions packages/eslint-plugin/docs/rules/explicit-member-accessibility.md
Expand Up @@ -20,6 +20,7 @@ type AccessibilityLevel =

type Options = {
accessibility?: AccessibilityLevel;
exceptMethods?: string[];
overrides?: {
accessors?: AccessibilityLevel;
constructors?: AccessibilityLevel;
Expand Down Expand Up @@ -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.
lonyele marked this conversation as resolved.
Show resolved Hide resolved
e.g. `[ { exceptMethods: ['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.
Expand Down
18 changes: 14 additions & 4 deletions packages/eslint-plugin/src/rules/explicit-member-accessibility.ts
Expand Up @@ -11,6 +11,7 @@ type AccessibilityLevel =

interface Config {
accessibility?: AccessibilityLevel;
exceptMethods?: string[];
overrides?: {
accessors?: AccessibilityLevel;
constructors?: AccessibilityLevel;
Expand Down Expand Up @@ -57,8 +58,15 @@ export default util.createRule<Options, MessageIds>({
properties: accessibilityLevel,
parameterProperties: accessibilityLevel,
},

additionalProperties: false,
},
exceptMethods: {
type: 'array',
items: {
type: 'string',
},
},
},
additionalProperties: false,
},
Expand All @@ -74,7 +82,7 @@ export default util.createRule<Options, MessageIds>({
const methodCheck = overrides.methods || baseCheck;
const propCheck = overrides.properties || baseCheck;
const paramPropCheck = overrides.parameterProperties || baseCheck;

const exceptMethods = option.exceptMethods || [];
lonyele marked this conversation as resolved.
Show resolved Hide resolved
/**
* Generates the report for rule violations
*/
Expand Down Expand Up @@ -116,14 +124,16 @@ export default util.createRule<Options, MessageIds>({
nodeType = `${methodDefinition.kind} property accessor`;
break;
}
if (check === 'off') {
return;
}

const methodName = util.getNameFromClassMember(
methodDefinition,
sourceCode,
);

if (check === 'off' || exceptMethods.includes(methodName)) {
lonyele marked this conversation as resolved.
Show resolved Hide resolved
return;
}

if (
check === 'no-public' &&
methodDefinition.accessibility === 'public'
Expand Down
Expand Up @@ -258,6 +258,21 @@ class Test {
},
],
},
{
filename: 'test.ts',
code: `
class Test {
getX () {
lonyele marked this conversation as resolved.
Show resolved Hide resolved
return this.x
}
}
`,
options: [
{
exceptMethods: ['getX'],
},
],
},
],
invalid: [
{
Expand Down