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

Skip to content

Commit 089bdaa

Browse files
committed
feat: method
1 parent c1e294a commit 089bdaa

File tree

2 files changed

+207
-67
lines changed

2 files changed

+207
-67
lines changed

packages/eslint-plugin/src/rules/naming-convention.ts

Lines changed: 108 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ type Options = (
7878
| Selector<'parameter'>
7979
| Selector<'property'>
8080
| Selector<'parameterProperty'>
81-
| Selector<'enumMember'>
8281
| Selector<'method'>
8382
| Selector<'accessor'>
83+
| Selector<'enumMember'>
8484
| Selector<'class'>
8585
| Selector<'interface'>
8686
| Selector<'typeAlias'>
@@ -203,7 +203,6 @@ const SCHEMA: JSONSchema.JSONSchema4 = {
203203
'public',
204204
'readonly',
205205
]),
206-
...selectorSchema('enumMember', false),
207206
...selectorSchema('method', false, [
208207
'private',
209208
'protected',
@@ -218,6 +217,7 @@ const SCHEMA: JSONSchema.JSONSchema4 = {
218217
'static',
219218
'abstract',
220219
]),
220+
...selectorSchema('enumMember', false),
221221
...selectorSchema('class', false, ['abstract']),
222222
...selectorSchema('interface', false),
223223
...selectorSchema('typeAlias', false),
@@ -270,15 +270,40 @@ export default util.createRule<Options, MessageIds>({
270270

271271
const key = node.key;
272272
/* istanbul ignore if */ if (!util.isLiteralOrIdentifier(key)) {
273-
// shouldn't happen due to selector
273+
// shouldn't happen due to the selectors that are used
274+
return;
275+
}
276+
277+
validator(key, modifiers);
278+
}
279+
280+
function handleMethod(
281+
node:
282+
| TSESTree.Property
283+
| TSESTree.ClassProperty
284+
| TSESTree.TSAbstractClassProperty
285+
| TSESTree.MethodDefinition
286+
| TSESTree.TSAbstractMethodDefinition
287+
| TSESTree.TSMethodSignature,
288+
modifiers: Set<Modifiers>,
289+
): void {
290+
const validator = validators.method;
291+
if (!validator) {
292+
return;
293+
}
294+
295+
const key = node.key;
296+
/* istanbul ignore if */ if (!util.isLiteralOrIdentifier(key)) {
297+
// shouldn't happen due to the selectors that are used
274298
return;
275299
}
276300

277301
validator(key, modifiers);
278302
}
279303

280304
return {
281-
// variable
305+
// #region variable
306+
282307
VariableDeclarator(node: TSESTree.VariableDeclarator): void {
283308
const validator = validators.variable;
284309
if (!validator) {
@@ -293,7 +318,10 @@ export default util.createRule<Options, MessageIds>({
293318
});
294319
},
295320

296-
// function
321+
// #endregion
322+
323+
// #region function
324+
297325
'FunctionDeclaration, TSDeclareFunction, FunctionExpression'(
298326
node:
299327
| TSESTree.FunctionDeclaration
@@ -308,7 +336,10 @@ export default util.createRule<Options, MessageIds>({
308336
validator(node.id);
309337
},
310338

311-
// parameter
339+
// #endregion function
340+
341+
// #region parameter
342+
312343
'FunctionDeclaration, TSDeclareFunction, FunctionExpression, ArrowFunctionExpression'(
313344
node:
314345
| TSESTree.FunctionDeclaration
@@ -334,7 +365,10 @@ export default util.createRule<Options, MessageIds>({
334365
});
335366
},
336367

337-
// parameterProperty
368+
// #endregion parameter
369+
370+
// #region parameterProperty
371+
338372
TSParameterProperty(node): void {
339373
const validator = validators.parameterProperty;
340374
if (!validator) {
@@ -359,19 +393,28 @@ export default util.createRule<Options, MessageIds>({
359393
});
360394
},
361395

362-
// property
363-
'Property[computed = false][method = false][kind = "init"][value.type != "ArrowFunctionExpression"][value.type != "FunctionExpression"]'(
396+
// #endregion parameterProperty
397+
398+
// #region property
399+
400+
'Property[computed = false][kind = "init"][value.type != "ArrowFunctionExpression"][value.type != "FunctionExpression"][value.type != "TSEmptyBodyFunctionExpression"]'(
364401
node: TSESTree.Property,
365402
): void {
366403
const modifiers = new Set<Modifiers>(['public']);
367404
handleProperty(node, modifiers);
368405
},
369-
'ClassProperty[computed = false], TSAbstractClassProperty[computed = false]'(
406+
407+
[[
408+
'ClassProperty[computed = false][value.type != "ArrowFunctionExpression"][value.type != "FunctionExpression"][value.type != "TSEmptyBodyFunctionExpression"]',
409+
'TSAbstractClassProperty[computed = false][value.type != "ArrowFunctionExpression"][value.type != "FunctionExpression"][value.type != "TSEmptyBodyFunctionExpression"]',
410+
].join(', ')](
370411
node: TSESTree.ClassProperty | TSESTree.TSAbstractClassProperty,
371412
): void {
372413
const modifiers = new Set<Modifiers>();
373414
if (node.accessibility) {
374415
modifiers.add(node.accessibility);
416+
} else {
417+
modifiers.add('public');
375418
}
376419
if (node.readonly) {
377420
modifiers.add('readonly');
@@ -385,16 +428,70 @@ export default util.createRule<Options, MessageIds>({
385428

386429
handleProperty(node, modifiers);
387430
},
431+
388432
'TSPropertySignature[computed = false]'(
389433
node: TSESTree.TSPropertySignature,
390434
): void {
391-
const modifiers = new Set<Modifiers>();
435+
const modifiers = new Set<Modifiers>(['public']);
392436
if (node.readonly) {
393437
modifiers.add('readonly');
394438
}
395439

396440
handleProperty(node, modifiers);
397441
},
442+
443+
// #endregion property
444+
445+
// #region method
446+
447+
[[
448+
'Property[computed = false][kind = "init"][value.type = "ArrowFunctionExpression"]',
449+
'Property[computed = false][kind = "init"][value.type = "FunctionExpression"]',
450+
'Property[computed = false][kind = "init"][value.type = "TSEmptyBodyFunctionExpression"]',
451+
'TSMethodSignature[computed = false]',
452+
].join(', ')](
453+
node: TSESTree.Property | TSESTree.TSMethodSignature,
454+
): void {
455+
const modifiers = new Set<Modifiers>(['public']);
456+
handleMethod(node, modifiers);
457+
},
458+
459+
[[
460+
'ClassProperty[computed = false][value.type = "ArrowFunctionExpression"]',
461+
'ClassProperty[computed = false][value.type = "FunctionExpression"]',
462+
'ClassProperty[computed = false][value.type = "TSEmptyBodyFunctionExpression"]',
463+
'TSAbstractClassProperty[computed = false][value.type = "ArrowFunctionExpression"]',
464+
'TSAbstractClassProperty[computed = false][value.type = "FunctionExpression"]',
465+
'TSAbstractClassProperty[computed = false][value.type = "TSEmptyBodyFunctionExpression"]',
466+
'MethodDefinition[computed = false][kind = "method"]',
467+
'TSAbstractMethodDefinition[computed = false][kind = "method"]',
468+
].join(', ')](
469+
node:
470+
| TSESTree.ClassProperty
471+
| TSESTree.TSAbstractClassProperty
472+
| TSESTree.MethodDefinition
473+
| TSESTree.TSAbstractMethodDefinition,
474+
): void {
475+
const modifiers = new Set<Modifiers>();
476+
if (node.accessibility) {
477+
modifiers.add(node.accessibility);
478+
} else {
479+
modifiers.add('public');
480+
}
481+
if (node.static) {
482+
modifiers.add('static');
483+
}
484+
if (
485+
node.type === AST_NODE_TYPES.TSAbstractClassProperty ||
486+
node.type === AST_NODE_TYPES.TSAbstractMethodDefinition
487+
) {
488+
modifiers.add('abstract');
489+
}
490+
491+
handleMethod(node, modifiers);
492+
},
493+
494+
// #endregion method
398495
};
399496
},
400497
});

0 commit comments

Comments
 (0)