diff --git a/packages/eslint-plugin/README.md b/packages/eslint-plugin/README.md index 10268b00635..1b6c8314181 100644 --- a/packages/eslint-plugin/README.md +++ b/packages/eslint-plugin/README.md @@ -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: | | diff --git a/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts b/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts index afae022c262..dc55ae3b620 100644 --- a/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts +++ b/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts @@ -12,6 +12,7 @@ export default util.createRule({ category: 'Best Practices', recommended: 'error', }, + fixable: 'code', messages: { missingAccessibility: 'Missing accessibility modifier on {{type}} {{name}}.', @@ -38,6 +39,9 @@ export default util.createRule({ type: 'method definition', name: util.getNameFromPropertyName(methodDefinition.key), }, + fix(fixer) { + return fixer.insertTextBefore(methodDefinition, 'public '); + }, }); } } @@ -60,6 +64,9 @@ export default util.createRule({ type: 'class property', name: util.getNameFromPropertyName(classProperty.key), }, + fix(fixer) { + return fixer.insertTextBefore(classProperty, '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 e630450689c..32980e8d751 100644 --- a/packages/eslint-plugin/tests/rules/explicit-member-accessibility.test.ts +++ b/packages/eslint-plugin/tests/rules/explicit-member-accessibility.test.ts @@ -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 + } } `, }, @@ -62,6 +75,14 @@ class Test { column: 3, }, ], + output: ` +class Test { + public x: number + public getX () { + return this.x + } +} + `, }, { filename: 'test.ts', @@ -84,6 +105,14 @@ class Test { column: 3, }, ], + output: ` +class Test { + private x: number + public getX () { + return this.x + } +} + `, }, { filename: 'test.ts', @@ -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 + } +} + `, }, ], });