Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "accessor" and "nonAccessor" options to kind #91

Merged
merged 3 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Members can be matched to positional slots using several criteria, including nam

- `name`: a string matching the name of the member. If the string starts and ends with `/` it will be interpreted as a regular expression. E.g., `"/_.+/"` will match members whose name starts with an underscore.
- `type`: `"method"|"property"`. **Note**: Class properties currently require a custom parser like [babel-eslint](https://github.com/babel/babel-eslint).
- `kind`: `"get"|"set"`. A subtype of `type: "method"` that can match getter or setter methods.
- `kind`: `"get"|"set"|"accessor"|"nonAccessor"`. A subtype of `type: "method"` that can match getter or setter methods. `"accessor"` matches both getters and setters, while `"nonAccessor"` matches methods that are neither getters or setters.
- `propertyType`: A subtype of `type: "property"` that can match the type of the property value. e.g., `propertyType: "ArrowFunctionExpression"` to match properties whose value is initialized to an arrow function.
- `accessorPair`: `true|false`. True to match only getters and setters that are part of a pair. i.e., only those that have both `get` and `set` methods defined.
- `static`: `true|false` to restrict the match to static or instance members.
Expand Down
2 changes: 1 addition & 1 deletion src/rules/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const sortClassMembersSchema = [
name: { type: 'string' },
groupByDecorator: { type: 'string' },
type: { enum: ['method', 'property'] },
kind: { enum: ['get', 'set'] },
kind: { enum: ['get', 'set', 'accessor', 'nonAccessor'] },
propertyType: { type: 'string' },
accessorPair: { type: 'boolean' },
sort: { enum: ['alphabetical', 'none'] },
Expand Down
14 changes: 13 additions & 1 deletion src/rules/sort-class-members.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,19 @@ const comparers = [
{ property: 'abstract', value: 10, test: (m, s) => s.abstract == m.abstract },
{ property: 'override', value: 10, test: (m, s) => s.override == m.override },
{ property: 'readonly', value: 10, test: (m, s) => s.readonly == m.readonly },
{ property: 'kind', value: 10, test: (m, s) => s.kind === m.kind },
{
property: 'kind',
value: 10,
test: (m, s) => {
if (s.kind === 'accessor') {
return isAccessor(m);
} else if (s.kind === 'nonAccessor') {
return !isAccessor(m);
} else {
return s.kind === m.kind;
}
},
},
{
property: 'groupByDecorator',
value: 10,
Expand Down
22 changes: 22 additions & 0 deletions test/rules/sort-class-members.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ const accessorOptions = [
},
];

const accessorEitherOptions = [
{
order: [{ type: 'method', kind: 'accessor' }, '[everything-else]'],
},
];

const accessorNeitherOptions = [
{
order: [{ type: 'method', kind: 'nonAccessor' }, '[everything-else]'],
},
];

const propertyTypeOptions = [
{
order: [
Expand Down Expand Up @@ -357,6 +369,16 @@ ruleTester.run('sort-class-members', rule, {
{ code: 'class A { get a(){} set a(v){} }', options: accessorOptions },
{ code: 'class A { set a(v){} }', options: accessorOptions },
{ code: 'class A { get a(){} b(){} }', options: accessorOptions },

{ code: 'class A { get a(){} }', options: accessorEitherOptions },
{ code: 'class A { get a(){} set a(v){} }', options: accessorEitherOptions },
{ code: 'class A { set a(v){} }', options: accessorEitherOptions },
{ code: 'class A { get a(){} b(){} }', options: accessorEitherOptions },

{ code: 'class A { get a(){} }', options: accessorNeitherOptions },
{ code: 'class A { get a(){} set a(v){} }', options: accessorNeitherOptions },
{ code: 'class A { set a(v){} }', options: accessorNeitherOptions },
{ code: 'class A { b(){} get a(){} }', options: accessorNeitherOptions },
{
code: 'class A { get a(){} set a(v){} }',
options: [{ order: ['everything-else'], accessorPairPositioning: 'getThenSet' }],
Expand Down