Navigation Menu

Skip to content

Commit

Permalink
feat(eslint-plugin): [explicit-member-accessibility] add support of "…
Browse files Browse the repository at this point in the history
…ignoredMethodNames" (#895)
  • Loading branch information
lonyele authored and JamesHenry committed Sep 12, 2019
1 parent 6a55921 commit 46ee4c9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
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;
ignoredMethodNames?: 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. 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.
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;
ignoredMethodNames?: 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,
},
ignoredMethodNames: {
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 ignoredMethodNames = new Set(option.ignoredMethodNames || []);
/**
* 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' || ignoredMethodNames.has(methodName)) {
return;
}

if (
check === 'no-public' &&
methodDefinition.accessibility === 'public'
Expand Down
Expand Up @@ -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: [
{
Expand Down

0 comments on commit 46ee4c9

Please sign in to comment.