Skip to content

Commit

Permalink
fix(eslint-plugin): Support private fields (#4182)
Browse files Browse the repository at this point in the history
  • Loading branch information
Святослав Зайцев committed Oct 23, 2022
1 parent d14591c commit aab2866
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/eslint-plugin/src/rules/member-ordering.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -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.)
Expand Down
47 changes: 47 additions & 0 deletions packages/eslint-plugin/tests/rules/member-ordering.test.ts
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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,
},
],
},
],
};

Expand Down

0 comments on commit aab2866

Please sign in to comment.