Skip to content

Commit

Permalink
feat(eslint-plugin): [naming-convention] add support for "override" a…
Browse files Browse the repository at this point in the history
…nd "async" modifiers (typescript-eslint#5310)
  • Loading branch information
eliasm307 committed Sep 7, 2022
1 parent f7c5deb commit e0cec8e
Show file tree
Hide file tree
Showing 4 changed files with 466 additions and 3 deletions.
Expand Up @@ -102,6 +102,10 @@ enum Modifiers {
unused = 1 << 10,
// properties that require quoting
requiresQuotes = 1 << 11,
// class members that are overridden
override = 1 << 12,
// class methods, object function properties, or functions that are async via the `async` keyword
async = 1 << 13,

// make sure TypeModifiers starts at Modifiers + 1 or else sorting won't work
}
Expand Down
19 changes: 17 additions & 2 deletions packages/eslint-plugin/src/rules/naming-convention-utils/schema.ts
Expand Up @@ -167,15 +167,21 @@ const SCHEMA: JSONSchema.JSONSchema4 = {
selectorsSchema(),
...selectorSchema('default', false, util.getEnumNames(Modifiers)),

...selectorSchema('variableLike', false, ['unused']),
...selectorSchema('variableLike', false, ['unused', 'async']),
...selectorSchema('variable', true, [
'const',
'destructured',
'exported',
'global',
'unused',
'async',
]),
...selectorSchema('function', false, [
'exported',
'global',
'unused',
'async',
]),
...selectorSchema('function', false, ['exported', 'global', 'unused']),
...selectorSchema('parameter', true, ['destructured', 'unused']),

...selectorSchema('memberLike', false, [
Expand All @@ -186,6 +192,7 @@ const SCHEMA: JSONSchema.JSONSchema4 = {
'readonly',
'requiresQuotes',
'static',
'override',
]),
...selectorSchema('classProperty', true, [
'abstract',
Expand All @@ -195,6 +202,7 @@ const SCHEMA: JSONSchema.JSONSchema4 = {
'readonly',
'requiresQuotes',
'static',
'override',
]),
...selectorSchema('objectLiteralProperty', true, [
'public',
Expand All @@ -210,6 +218,7 @@ const SCHEMA: JSONSchema.JSONSchema4 = {
'protected',
'public',
'readonly',
'override',
]),
...selectorSchema('property', true, [
'abstract',
Expand All @@ -219,6 +228,7 @@ const SCHEMA: JSONSchema.JSONSchema4 = {
'readonly',
'requiresQuotes',
'static',
'override',
]),

...selectorSchema('classMethod', false, [
Expand All @@ -228,10 +238,13 @@ const SCHEMA: JSONSchema.JSONSchema4 = {
'public',
'requiresQuotes',
'static',
'override',
'async',
]),
...selectorSchema('objectLiteralMethod', false, [
'public',
'requiresQuotes',
'async',
]),
...selectorSchema('typeMethod', false, ['public', 'requiresQuotes']),
...selectorSchema('method', false, [
Expand All @@ -241,6 +254,8 @@ const SCHEMA: JSONSchema.JSONSchema4 = {
'public',
'requiresQuotes',
'static',
'override',
'async',
]),
...selectorSchema('accessor', true, [
'abstract',
Expand Down
53 changes: 53 additions & 0 deletions packages/eslint-plugin/src/rules/naming-convention.ts
Expand Up @@ -138,6 +138,9 @@ export default util.createRule<Options, MessageIds>({
if ('readonly' in node && node.readonly) {
modifiers.add(Modifiers.readonly);
}
if ('override' in node && node.override) {
modifiers.add(Modifiers.override);
}
if (
node.type === AST_NODE_TYPES.TSAbstractPropertyDefinition ||
node.type === AST_NODE_TYPES.TSAbstractMethodDefinition
Expand Down Expand Up @@ -182,6 +185,34 @@ export default util.createRule<Options, MessageIds>({
);
}

function isAsyncMemberOrProperty(
propertyOrMemberNode:
| TSESTree.PropertyNonComputedName
| TSESTree.TSMethodSignatureNonComputedName
| TSESTree.PropertyDefinitionNonComputedName
| TSESTree.TSAbstractPropertyDefinitionNonComputedName
| TSESTree.MethodDefinitionNonComputedName
| TSESTree.TSAbstractMethodDefinitionNonComputedName,
): boolean {
return Boolean(
'value' in propertyOrMemberNode &&
propertyOrMemberNode.value &&
'async' in propertyOrMemberNode.value &&
propertyOrMemberNode.value.async,
);
}

function isAsyncVariableIdentifier(id: TSESTree.Identifier): boolean {
return Boolean(
id.parent &&
(('async' in id.parent && id.parent.async) ||
('init' in id.parent &&
id.parent.init &&
'async' in id.parent.init &&
id.parent.init.async)),
);
}

return {
// #region variable

Expand Down Expand Up @@ -219,6 +250,10 @@ export default util.createRule<Options, MessageIds>({
modifiers.add(Modifiers.unused);
}

if (isAsyncVariableIdentifier(id)) {
modifiers.add(Modifiers.async);
}

validator(id, modifiers);
});
},
Expand Down Expand Up @@ -254,6 +289,10 @@ export default util.createRule<Options, MessageIds>({
modifiers.add(Modifiers.unused);
}

if (node.async) {
modifiers.add(Modifiers.async);
}

validator(node.id, modifiers);
},

Expand Down Expand Up @@ -291,6 +330,10 @@ export default util.createRule<Options, MessageIds>({
modifiers.add(Modifiers.unused);
}

if (node.async) {
modifiers.add(Modifiers.async);
}

validator(i, modifiers);
});
});
Expand Down Expand Up @@ -360,6 +403,11 @@ export default util.createRule<Options, MessageIds>({
| TSESTree.TSMethodSignatureNonComputedName,
): void {
const modifiers = new Set<Modifiers>([Modifiers.public]);

if (isAsyncMemberOrProperty(node)) {
modifiers.add(Modifiers.async);
}

handleMember(validators.objectLiteralMethod, node, modifiers);
},

Expand All @@ -376,6 +424,11 @@ export default util.createRule<Options, MessageIds>({
| TSESTree.TSAbstractMethodDefinitionNonComputedName,
): void {
const modifiers = getMemberModifiers(node);

if (isAsyncMemberOrProperty(node)) {
modifiers.add(Modifiers.async);
}

handleMember(validators.classMethod, node, modifiers);
},

Expand Down

0 comments on commit e0cec8e

Please sign in to comment.