Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dd233b5

Browse files
authored
feat(eslint-plugin): [explicit-member-accessibility] autofix no-public (typescript-eslint#1548)
1 parent baf7c98 commit dd233b5

File tree

3 files changed

+389
-1
lines changed

3 files changed

+389
-1
lines changed

packages/eslint-plugin/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Pro Tip: For larger codebases you may want to consider splitting our linting int
102102
| [`@typescript-eslint/consistent-type-assertions`](./docs/rules/consistent-type-assertions.md) | Enforces consistent usage of type assertions | :heavy_check_mark: | | |
103103
| [`@typescript-eslint/consistent-type-definitions`](./docs/rules/consistent-type-definitions.md) | Consistent with type definition either `interface` or `type` | | :wrench: | |
104104
| [`@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: | | |
105-
| [`@typescript-eslint/explicit-member-accessibility`](./docs/rules/explicit-member-accessibility.md) | Require explicit accessibility modifiers on class properties and methods | | | |
105+
| [`@typescript-eslint/explicit-member-accessibility`](./docs/rules/explicit-member-accessibility.md) | Require explicit accessibility modifiers on class properties and methods | | :wrench: | |
106106
| [`@typescript-eslint/explicit-module-boundary-types`](./docs/rules/explicit-module-boundary-types.md) | Require explicit return and argument types on exported functions' and classes' public class methods | | | |
107107
| [`@typescript-eslint/member-delimiter-style`](./docs/rules/member-delimiter-style.md) | Require a specific member delimiter style for interfaces and type literals | :heavy_check_mark: | :wrench: | |
108108
| [`@typescript-eslint/member-ordering`](./docs/rules/member-ordering.md) | Require a consistent member declaration order | | | |

packages/eslint-plugin/src/rules/explicit-member-accessibility.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {
22
AST_NODE_TYPES,
33
TSESTree,
4+
AST_TOKEN_TYPES,
5+
TSESLint,
46
} from '@typescript-eslint/experimental-utils';
57
import * as util from '../util';
68

@@ -38,6 +40,7 @@ export default util.createRule<Options, MessageIds>({
3840
// too opinionated to be recommended
3941
recommended: false,
4042
},
43+
fixable: 'code',
4144
messages: {
4245
missingAccessibility:
4346
'Missing accessibility modifier on {{type}} {{name}}.',
@@ -91,6 +94,7 @@ export default util.createRule<Options, MessageIds>({
9194
nodeType: string,
9295
node: TSESTree.Node,
9396
nodeName: string,
97+
fix: TSESLint.ReportFixFunction | null = null,
9498
): void {
9599
context.report({
96100
node: node,
@@ -99,6 +103,7 @@ export default util.createRule<Options, MessageIds>({
99103
type: nodeType,
100104
name: nodeName,
101105
},
106+
fix: fix,
102107
});
103108
}
104109

@@ -140,6 +145,7 @@ export default util.createRule<Options, MessageIds>({
140145
nodeType,
141146
methodDefinition,
142147
methodName,
148+
getUnwantedPublicAccessibilityFixer(methodDefinition),
143149
);
144150
} else if (check === 'explicit' && !methodDefinition.accessibility) {
145151
reportIssue(
@@ -151,6 +157,47 @@ export default util.createRule<Options, MessageIds>({
151157
}
152158
}
153159

160+
/**
161+
* Creates a fixer that removes a "public" keyword with following spaces
162+
*/
163+
function getUnwantedPublicAccessibilityFixer(
164+
node:
165+
| TSESTree.MethodDefinition
166+
| TSESTree.ClassProperty
167+
| TSESTree.TSParameterProperty,
168+
): TSESLint.ReportFixFunction {
169+
return function(fixer: TSESLint.RuleFixer): TSESLint.RuleFix {
170+
const tokens = sourceCode.getTokens(node);
171+
let rangeToRemove: TSESLint.AST.Range;
172+
for (let i = 0; i < tokens.length; i++) {
173+
const token = tokens[i];
174+
if (
175+
token.type === AST_TOKEN_TYPES.Keyword &&
176+
token.value === 'public'
177+
) {
178+
const commensAfterPublicKeyword = sourceCode.getCommentsAfter(
179+
token,
180+
);
181+
if (commensAfterPublicKeyword.length) {
182+
// public /* Hi there! */ static foo()
183+
// ^^^^^^^
184+
rangeToRemove = [
185+
token.range[0],
186+
commensAfterPublicKeyword[0].range[0],
187+
];
188+
break;
189+
} else {
190+
// public static foo()
191+
// ^^^^^^^
192+
rangeToRemove = [token.range[0], tokens[i + 1].range[0]];
193+
break;
194+
}
195+
}
196+
}
197+
return fixer.removeRange(rangeToRemove!);
198+
};
199+
}
200+
154201
/**
155202
* Checks if property has an accessibility modifier.
156203
* @param classProperty The node representing a ClassProperty.
@@ -170,6 +217,7 @@ export default util.createRule<Options, MessageIds>({
170217
nodeType,
171218
classProperty,
172219
propertyName,
220+
getUnwantedPublicAccessibilityFixer(classProperty),
173221
);
174222
} else if (propCheck === 'explicit' && !classProperty.accessibility) {
175223
reportIssue(
@@ -217,6 +265,7 @@ export default util.createRule<Options, MessageIds>({
217265
nodeType,
218266
node,
219267
nodeName,
268+
getUnwantedPublicAccessibilityFixer(node),
220269
);
221270
}
222271
break;

0 commit comments

Comments
 (0)