|
1 |
| -import { TSESTree } from '@typescript-eslint/experimental-utils'; |
| 1 | +import { |
| 2 | + TSESTree, |
| 3 | + AST_NODE_TYPES, |
| 4 | +} from '@typescript-eslint/experimental-utils'; |
2 | 5 | import * as util from '../util';
|
3 | 6 |
|
4 | 7 | interface Config<T = string> {
|
@@ -61,37 +64,73 @@ export default util.createRule<Options, MessageIds>({
|
61 | 64 | return acc;
|
62 | 65 | }, {});
|
63 | 66 |
|
64 |
| - /** |
65 |
| - * Check that the property name matches the convention for its |
66 |
| - * accessibility. |
67 |
| - * @param {ASTNode} node the named node to evaluate. |
68 |
| - * @returns {void} |
69 |
| - * @private |
70 |
| - */ |
| 67 | + function getParameterNode( |
| 68 | + node: TSESTree.TSParameterProperty, |
| 69 | + ): TSESTree.Identifier | null { |
| 70 | + if (node.parameter.type === AST_NODE_TYPES.AssignmentPattern) { |
| 71 | + return node.parameter.left as TSESTree.Identifier; |
| 72 | + } |
| 73 | + |
| 74 | + if (node.parameter.type === AST_NODE_TYPES.Identifier) { |
| 75 | + return node.parameter; |
| 76 | + } |
| 77 | + |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + function validateParameterName(node: TSESTree.TSParameterProperty): void { |
| 82 | + const parameterNode = getParameterNode(node); |
| 83 | + if (!parameterNode) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + validate(parameterNode, parameterNode.name, node.accessibility); |
| 88 | + } |
| 89 | + |
71 | 90 | function validateName(
|
72 | 91 | node: TSESTree.MethodDefinition | TSESTree.ClassProperty,
|
73 | 92 | ): void {
|
74 |
| - const name = util.getNameFromClassMember(node, sourceCode); |
75 |
| - const accessibility: Modifiers = node.accessibility || 'public'; |
76 |
| - const convention = conventions[accessibility]; |
77 |
| - |
78 |
| - const method = node as TSESTree.MethodDefinition; |
79 |
| - if (method.kind === 'constructor') { |
| 93 | + if ( |
| 94 | + node.type === AST_NODE_TYPES.MethodDefinition && |
| 95 | + node.kind === 'constructor' |
| 96 | + ) { |
80 | 97 | return;
|
81 | 98 | }
|
82 | 99 |
|
| 100 | + validate( |
| 101 | + node.key, |
| 102 | + util.getNameFromClassMember(node, sourceCode), |
| 103 | + node.accessibility, |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Check that the name matches the convention for its accessibility. |
| 109 | + * @param {ASTNode} node the named node to evaluate. |
| 110 | + * @param {string} name |
| 111 | + * @param {Modifiers} accessibility |
| 112 | + * @returns {void} |
| 113 | + * @private |
| 114 | + */ |
| 115 | + function validate( |
| 116 | + node: TSESTree.Identifier | TSESTree.Expression, |
| 117 | + name: string, |
| 118 | + accessibility: Modifiers = 'public', |
| 119 | + ): void { |
| 120 | + const convention = conventions[accessibility]; |
83 | 121 | if (!convention || convention.test(name)) {
|
84 | 122 | return;
|
85 | 123 | }
|
86 | 124 |
|
87 | 125 | context.report({
|
88 |
| - node: node.key, |
| 126 | + node, |
89 | 127 | messageId: 'incorrectName',
|
90 | 128 | data: { accessibility, name, convention },
|
91 | 129 | });
|
92 | 130 | }
|
93 | 131 |
|
94 | 132 | return {
|
| 133 | + TSParameterProperty: validateParameterName, |
95 | 134 | MethodDefinition: validateName,
|
96 | 135 | ClassProperty: validateName,
|
97 | 136 | };
|
|
0 commit comments