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): Make explicit-member-accessibility fixable #331

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/eslint-plugin/README.md
Expand Up @@ -117,7 +117,7 @@ Then you should add `airbnb` (or `airbnb-base`) to your `extends` section of `.e
| [`@typescript-eslint/camelcase`](./docs/rules/camelcase.md) | Enforce camelCase naming convention | :heavy_check_mark: | |
| [`@typescript-eslint/class-name-casing`](./docs/rules/class-name-casing.md) | Require PascalCased class and interface names (`class-name` from TSLint) | :heavy_check_mark: | |
| [`@typescript-eslint/explicit-function-return-type`](./docs/rules/explicit-function-return-type.md) | Require explicit return types on functions and class methods | :heavy_check_mark: | |
| [`@typescript-eslint/explicit-member-accessibility`](./docs/rules/explicit-member-accessibility.md) | Require explicit accessibility modifiers on class properties and methods (`member-access` from TSLint) | :heavy_check_mark: | |
| [`@typescript-eslint/explicit-member-accessibility`](./docs/rules/explicit-member-accessibility.md) | Require explicit accessibility modifiers on class properties and methods (`member-access` from TSLint) | :heavy_check_mark: | :wrench: |
| [`@typescript-eslint/generic-type-naming`](./docs/rules/generic-type-naming.md) | Enforces naming of generic type variables | | |
| [`@typescript-eslint/indent`](./docs/rules/indent.md) | Enforce consistent indentation (`indent` from TSLint) | :heavy_check_mark: | :wrench: |
| [`@typescript-eslint/interface-name-prefix`](./docs/rules/interface-name-prefix.md) | Require that interface names be prefixed with `I` (`interface-name` from TSLint) | :heavy_check_mark: | |
Expand Down
Expand Up @@ -12,6 +12,7 @@ export default util.createRule({
category: 'Best Practices',
recommended: 'error',
},
fixable: 'code',
messages: {
missingAccessibility:
'Missing accessibility modifier on {{type}} {{name}}.',
Expand All @@ -38,6 +39,9 @@ export default util.createRule({
type: 'method definition',
name: util.getNameFromPropertyName(methodDefinition.key),
},
fix(fixer) {
return fixer.insertTextBefore(methodDefinition, 'public ');
},
});
}
}
Expand All @@ -60,6 +64,9 @@ export default util.createRule({
type: 'class property',
name: util.getNameFromPropertyName(classProperty.key),
},
fix(fixer) {
return fixer.insertTextBefore(classProperty, 'public ');
},
});
}
}
Expand Down
Expand Up @@ -36,6 +36,19 @@ class Test {
getX () {
return 1;
}
}
`,
},
{
filename: 'test.ts',
code: `
class Test {
protected readonly name: string
private readonly x: number
public readonly b: boolean
public getX () {
return this.x
}
}
`,
},
Expand All @@ -62,6 +75,14 @@ class Test {
column: 3,
},
],
output: `
class Test {
public x: number
public getX () {
return this.x
}
}
`,
},
{
filename: 'test.ts',
Expand All @@ -84,6 +105,14 @@ class Test {
column: 3,
},
],
output: `
class Test {
private x: number
public getX () {
return this.x
}
}
`,
},
{
filename: 'test.ts',
Expand Down Expand Up @@ -115,6 +144,44 @@ class Test {
column: 3,
},
],
output: `
class Test {
public x?: number
public getX? () {
return this.x
}
}
`,
},
{
filename: 'test.ts',
code: `
class Test {
readonly x: number
public getX () {
return this.x
}
}
`,
errors: [
{
messageId: 'missingAccessibility',
data: {
type: 'class property',
name: 'x',
},
line: 3,
column: 3,
},
],
output: `
class Test {
public readonly x: number
public getX () {
return this.x
}
}
`,
},
],
});