From 96556df1525f1c0f72bcaafda6ccc8b0e368d09e Mon Sep 17 00:00:00 2001 From: Florian Adonis Date: Thu, 6 May 2021 13:23:49 +0300 Subject: [PATCH 1/2] feat(eslint-plugin): add callback as a filtering type of node --- .../src/rules/member-ordering.ts | 18 ++- .../tests/rules/member-ordering.test.ts | 140 ++++++++++++++---- 2 files changed, 127 insertions(+), 31 deletions(-) diff --git a/packages/eslint-plugin/src/rules/member-ordering.ts b/packages/eslint-plugin/src/rules/member-ordering.ts index de5b971f5339..984acbdeb0e2 100644 --- a/packages/eslint-plugin/src/rules/member-ordering.ts +++ b/packages/eslint-plugin/src/rules/member-ordering.ts @@ -56,6 +56,9 @@ export const defaultOrder = [ // Index signature 'signature', + // Callbacks + 'callback', + // Fields 'public-static-field', 'protected-static-field', @@ -122,9 +125,13 @@ export const defaultOrder = [ 'method', ]; -const allMemberTypes = ['signature', 'field', 'method', 'constructor'].reduce< - string[] ->((all, type) => { +const allMemberTypes = [ + 'signature', + 'field', + 'method', + 'callback', + 'constructor', +].reduce((all, type) => { all.push(type); ['public', 'protected', 'private'].forEach(accessibility => { @@ -170,13 +177,14 @@ const functionExpressions = [ * @param node the node to be evaluated. */ function getNodeType(node: Member): string | null { - // TODO: add missing TSCallSignatureDeclaration switch (node.type) { case AST_NODE_TYPES.TSAbstractMethodDefinition: case AST_NODE_TYPES.MethodDefinition: return node.kind; case AST_NODE_TYPES.TSMethodSignature: return 'method'; + case AST_NODE_TYPES.TSCallSignatureDeclaration: + return 'callback'; case AST_NODE_TYPES.TSConstructSignatureDeclaration: return 'constructor'; case AST_NODE_TYPES.TSAbstractClassProperty: @@ -216,6 +224,8 @@ function getMemberName( : util.getNameFromMember(node, sourceCode); case AST_NODE_TYPES.TSConstructSignatureDeclaration: return 'new'; + case AST_NODE_TYPES.TSCallSignatureDeclaration: + return 'call'; case AST_NODE_TYPES.TSIndexSignature: return util.getNameFromIndexSignature(node); default: diff --git a/packages/eslint-plugin/tests/rules/member-ordering.test.ts b/packages/eslint-plugin/tests/rules/member-ordering.test.ts index 5134d591ffa8..efbe782d8274 100644 --- a/packages/eslint-plugin/tests/rules/member-ordering.test.ts +++ b/packages/eslint-plugin/tests/rules/member-ordering.test.ts @@ -75,7 +75,16 @@ interface Foo { `, options: [{ default: ['signature', 'field', 'constructor', 'method'] }], }, - + { + code: ` +interface X { + (): void; + a: unknown; + b(): void; +} + `, + options: [{ default: ['callback', 'field', 'method'] }], + }, { code: ` // no accessibility === public @@ -1448,6 +1457,27 @@ interface Foo { }, { code: ` +interface X { + a: unknown; + (): void; + b(): void; +} + `, + options: [{ default: ['callback', 'field', 'method'] }], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'call', + rank: 'field', + }, + line: 4, + column: 3, + }, + ], + }, + { + code: ` // no accessibility === public interface Foo { A: string; @@ -3811,8 +3841,8 @@ const sortedWithoutGroupingDefaultOption: TSESLint.RunTests< { code: ` interface Foo { - a(): Foo; (): Foo; + a(): Foo; b(): Foo; } `, @@ -3850,8 +3880,8 @@ type Foo = { a : b; [a: string] : number; b() : void; - new () : Bar; () : Baz; + new () : Bar; } `, options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], @@ -3990,6 +4020,13 @@ interface Foo { beforeMember: 'b', }, }, + { + messageId: 'incorrectOrder', + data: { + member: 'call', + beforeMember: 'new', + }, + }, ], }, @@ -4041,6 +4078,13 @@ type Foo = { beforeMember: 'b', }, }, + { + messageId: 'incorrectOrder', + data: { + member: 'call', + beforeMember: 'new', + }, + }, ], }, @@ -4190,10 +4234,10 @@ const sortedWithoutGroupingClassesOption: TSESLint.RunTests< code: ` interface Foo { [a: string] : number; + () : Baz; c : b; new () : Bar; b() : void; - () : Baz; } `, options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], @@ -4226,10 +4270,10 @@ interface Foo { code: ` type Foo = { [a: string] : number; + () : Baz; c : b; new () : Bar; b() : void; - () : Baz; } `, options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], @@ -4400,10 +4444,10 @@ const sortedWithoutGroupingClassExpressionsOption: TSESLint.RunTests< code: ` interface Foo { [a: string] : number; + () : Baz; c : b; new () : Bar; b() : void; - () : Baz; } `, options: [ @@ -4442,10 +4486,10 @@ interface Foo { code: ` type Foo = { [a: string] : number; + () : Baz; c : b; new () : Bar; b() : void; - () : Baz; } `, options: [ @@ -4639,8 +4683,8 @@ interface Foo { [a: string] : number; a : b; b() : void; - new () : Bar; () : Baz; + new () : Bar; } `, options: [ @@ -4679,10 +4723,10 @@ interface Foo { code: ` type Foo = { [a: string] : number; + () : Baz; c : b; new () : Bar; b() : void; - () : Baz; } `, options: [ @@ -4827,6 +4871,13 @@ interface Foo { beforeMember: 'b', }, }, + { + messageId: 'incorrectOrder', + data: { + member: 'call', + beforeMember: 'new', + }, + }, ], }, @@ -4872,10 +4923,10 @@ const sortedWithoutGroupingTypeLiteralsOption: TSESLint.RunTests< code: ` interface Foo { [a: string] : number; + () : Baz; c : b; new () : Bar; b() : void; - () : Baz; } `, options: [ @@ -4916,8 +4967,8 @@ type Foo = { [a: string] : number; a : b; b() : void; - new () : Bar; () : Baz; + new () : Bar; } `, options: [ @@ -5062,6 +5113,13 @@ type Foo = { beforeMember: 'b', }, }, + { + messageId: 'incorrectOrder', + data: { + member: 'call', + beforeMember: 'new', + }, + }, ], }, @@ -5108,6 +5166,8 @@ const sortedWithGroupingDefaultOption: TSESLint.RunTests< interface Foo { [a: string] : number; + () : Baz; + a : x; b : x; c : x; @@ -5117,8 +5177,6 @@ interface Foo { a() : void; b() : void; c() : void; - - () : Baz; } `, options: [ @@ -5160,6 +5218,8 @@ interface Foo { type Foo = { [a: string] : number; + () : Baz; + a : x; b : x; c : x; @@ -5169,8 +5229,6 @@ type Foo = { a() : void; b() : void; c() : void; - - () : Baz; } `, options: [ @@ -5344,6 +5402,13 @@ interface Foo { { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, ], errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'call', + rank: 'field', + }, + }, { messageId: 'incorrectGroupOrder', data: { @@ -5377,6 +5442,13 @@ type Foo = { { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, ], errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'call', + rank: 'field', + }, + }, { messageId: 'incorrectGroupOrder', data: { @@ -5503,6 +5575,8 @@ const sortedWithGroupingClassesOption: TSESLint.RunTests< interface Foo { [a: string] : number; + () : Baz; + c : x; b : x; a : x; @@ -5512,8 +5586,6 @@ interface Foo { c() : void; b() : void; a() : void; - - () : Baz; } `, options: [{ classes: { order: 'alphabetically' } }], @@ -5525,6 +5597,8 @@ interface Foo { type Foo = { [a: string] : number; + () : Baz; + c : x; b : x; a : x; @@ -5534,8 +5608,6 @@ type Foo = { c() : void; b() : void; a() : void; - - () : Baz; } `, options: [{ classes: { order: 'alphabetically' } }], @@ -5645,6 +5717,8 @@ const sortedWithGroupingClassExpressionsOption: TSESLint.RunTests< interface Foo { [a: string] : number; + () : Baz; + c : x; b : x; a : x; @@ -5654,8 +5728,6 @@ interface Foo { c() : void; b() : void; a() : void; - - () : Baz; } `, options: [{ classExpressions: { order: 'alphabetically' } }], @@ -5667,6 +5739,8 @@ interface Foo { type Foo = { [a: string] : number; + () : Baz; + c : x; b : x; a : x; @@ -5676,8 +5750,6 @@ type Foo = { c() : void; b() : void; a() : void; - - () : Baz; } `, options: [{ classExpressions: { order: 'alphabetically' } }], @@ -5849,6 +5921,8 @@ interface Foo { type Foo = { [a: string] : number; + () : Baz; + c : x; b : x; a : x; @@ -5858,8 +5932,6 @@ type Foo = { c() : void; b() : void; a() : void; - - () : Baz; } `, options: [{ interfaces: { order: 'alphabetically' } }], @@ -5925,6 +5997,13 @@ interface Foo { { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, ], errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'call', + rank: 'field', + }, + }, { messageId: 'incorrectGroupOrder', data: { @@ -5948,6 +6027,8 @@ const sortedWithGroupingTypeLiteralsOption: TSESLint.RunTests< interface Foo { [a: string] : number; + () : Baz; + c : x; b : x; a : x; @@ -5957,8 +6038,6 @@ interface Foo { c() : void; b() : void; a() : void; - - () : Baz; } `, options: [{ typeLiterals: { order: 'alphabetically' } }], @@ -6081,6 +6160,13 @@ type Foo = { { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, ], errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'call', + rank: 'field', + }, + }, { messageId: 'incorrectGroupOrder', data: { From 56e66f980ec7a04ff9c0d8af14ba5e7b8ac0e7b2 Mon Sep 17 00:00:00 2001 From: Florian Adonis Date: Thu, 27 May 2021 12:02:19 +0300 Subject: [PATCH 2/2] fix(eslint-plugin): change callback type to call-signature --- packages/eslint-plugin/src/rules/member-ordering.ts | 8 +++----- .../eslint-plugin/tests/rules/member-ordering.test.ts | 4 ++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/packages/eslint-plugin/src/rules/member-ordering.ts b/packages/eslint-plugin/src/rules/member-ordering.ts index 984acbdeb0e2..a6b1b74c20e8 100644 --- a/packages/eslint-plugin/src/rules/member-ordering.ts +++ b/packages/eslint-plugin/src/rules/member-ordering.ts @@ -55,9 +55,7 @@ const objectConfig = (memberTypes: string[]): JSONSchema.JSONSchema4 => ({ export const defaultOrder = [ // Index signature 'signature', - - // Callbacks - 'callback', + 'call-signature', // Fields 'public-static-field', @@ -129,7 +127,7 @@ const allMemberTypes = [ 'signature', 'field', 'method', - 'callback', + 'call-signature', 'constructor', ].reduce((all, type) => { all.push(type); @@ -184,7 +182,7 @@ function getNodeType(node: Member): string | null { case AST_NODE_TYPES.TSMethodSignature: return 'method'; case AST_NODE_TYPES.TSCallSignatureDeclaration: - return 'callback'; + return 'call-signature'; case AST_NODE_TYPES.TSConstructSignatureDeclaration: return 'constructor'; case AST_NODE_TYPES.TSAbstractClassProperty: diff --git a/packages/eslint-plugin/tests/rules/member-ordering.test.ts b/packages/eslint-plugin/tests/rules/member-ordering.test.ts index efbe782d8274..c17aa57b90e7 100644 --- a/packages/eslint-plugin/tests/rules/member-ordering.test.ts +++ b/packages/eslint-plugin/tests/rules/member-ordering.test.ts @@ -83,7 +83,7 @@ interface X { b(): void; } `, - options: [{ default: ['callback', 'field', 'method'] }], + options: [{ default: ['call-signature', 'field', 'method'] }], }, { code: ` @@ -1463,7 +1463,7 @@ interface X { b(): void; } `, - options: [{ default: ['callback', 'field', 'method'] }], + options: [{ default: ['call-signature', 'field', 'method'] }], errors: [ { messageId: 'incorrectGroupOrder', pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy