From 0185dc85d5b90001334b2be9ae85c98e68c1e579 Mon Sep 17 00:00:00 2001 From: Svyatoslav Zaytsev Date: Sun, 23 Oct 2022 17:36:40 +0300 Subject: [PATCH] fix(eslint-plugin): Support private fields (#4182) --- .../src/rules/member-ordering.ts | 15 ++++-- .../tests/rules/member-ordering.test.ts | 47 +++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/src/rules/member-ordering.ts b/packages/eslint-plugin/src/rules/member-ordering.ts index 66fe9b623342..212dc3a0217c 100644 --- a/packages/eslint-plugin/src/rules/member-ordering.ts +++ b/packages/eslint-plugin/src/rules/member-ordering.ts @@ -397,6 +397,16 @@ function getRankOrder( return rank; } +function getAccessibility(node: Member): TSESTree.Accessibility { + if ('accessibility' in node && node.accessibility) { + return node.accessibility; + } + if ('key' in node && node.key?.type === AST_NODE_TYPES.PrivateIdentifier) { + return 'private'; + } + return 'public'; +} + /** * Gets the rank of the node given the order. * @param node the node to be evaluated. @@ -425,10 +435,7 @@ function getRank( : abstract ? 'abstract' : 'instance'; - const accessibility = - 'accessibility' in node && node.accessibility - ? node.accessibility - : 'public'; + const accessibility = getAccessibility(node); // Collect all existing member groups that apply to this node... // (e.g. 'public-instance-field', 'instance-field', 'public-field', 'constructor' etc.) diff --git a/packages/eslint-plugin/tests/rules/member-ordering.test.ts b/packages/eslint-plugin/tests/rules/member-ordering.test.ts index 68bbfa7eff6e..8be7653ca97d 100644 --- a/packages/eslint-plugin/tests/rules/member-ordering.test.ts +++ b/packages/eslint-plugin/tests/rules/member-ordering.test.ts @@ -1517,6 +1517,24 @@ class Foo { }, ], }, + { + name: 'with private identifier', + code: ` +// no accessibility === public +class Foo { + imPublic() {}; + #imPrivate() {}; +} + `, + options: [ + { + default: { + memberTypes: ['public-method', 'private-method'], + order: 'alphabetically-case-insensitive', + }, + }, + ], + }, ], invalid: [ { @@ -4138,6 +4156,35 @@ class Foo { }, ], }, + { + name: 'with private identifier', + code: ` +// no accessibility === public +class Foo { + #imPrivate() {}; + imPublic() {}; +} + `, + options: [ + { + default: { + memberTypes: ['public-method', 'private-method'], + order: 'alphabetically-case-insensitive', + }, + }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'imPublic', + rank: 'private method', + }, + line: 5, + column: 5, + }, + ], + }, ], };