Skip to content

Commit

Permalink
Support matching members with any/no decorator.
Browse files Browse the repository at this point in the history
  • Loading branch information
dospunk authored May 9, 2023
1 parent 933deb9 commit c569a9b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Members can be matched to positional slots using several criteria, including nam
- `readonly`: `true|false` to restrict the match to members with typescript `readonly` keyword.
- `async`: `true|false` to restrict the match to async members.
- `sort`: `"alphabetical"|"none"`. Used to require a specific sorting within the slot for matched members. Defaults to `"none"`.
- `groupByDecorator`: a string used to group properties with the same decorator name (e.g. `observable` for `@observable`). Can be used together with `sort`. If the string starts and ends with `/` it will be interpreted as a regular expression. E.g., `"/_.+/"` will match members that have a decorator that starts with an underscore. **Note**: Decorators are a Stage 2 proposal and require a custom parser like [babel-eslint](https://github.com/babel/babel-eslint).
- `groupByDecorator`: `string|true|false`. A boolean used to indicate whether a property/method should have a decorator or a string used to group properties/methods with the same decorator name (e.g. `observable` for `@observable`). If the string starts and ends with `/` it will be interpreted as a regular expression. E.g., `"/_.+/"` will match members that have a decorator that starts with an underscore. Can be used together with `sort`. **Note**: Decorators are a Stage 2 proposal and require a custom parser like [babel-eslint](https://github.com/babel/babel-eslint).

A few examples:

Expand Down
2 changes: 1 addition & 1 deletion src/rules/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const sortClassMembersSchema = [
type: 'object',
properties: {
name: { type: 'string' },
groupByDecorator: { type: 'string' },
groupByDecorator: { oneOf: [{ type: 'string' }, { type: 'boolean' }] },
type: { enum: ['method', 'property'] },
kind: { enum: ['get', 'set', 'accessor', 'nonAccessor'] },
propertyType: { type: 'string' },
Expand Down
11 changes: 8 additions & 3 deletions src/rules/sort-class-members.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,13 @@ const comparers = [
property: 'groupByDecorator',
value: 10,
test: (m, s) => {
const comparer = getStringComparer(s.groupByDecorator);
return m.decorators.some((decorator) => comparer(decorator));
if (typeof s.groupByDecorator === 'boolean') {
const hasDecorators = m.decorators.length > 0;
return s.groupByDecorator === hasDecorators;
} else {
const comparer = getStringComparer(s.groupByDecorator);
return m.decorators.some((decorator) => comparer(decorator));
}
},
},
{
Expand All @@ -461,7 +466,7 @@ const comparers = [
},
{
property: 'propertyType',
value: 11,
value: 12,
test: (m, s) => m.type === 'property' && s.propertyType === m.propertyType,
},
];
15 changes: 15 additions & 0 deletions test/rules/sort-class-members.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ const decoratorOptions = [
},
];

const decoratorBooleanOptions = [
{
order: ['[no-decorators]', 'constructor'],
groups: {
'no-decorators': [{ groupByDecorator: false }],
'any-decorator': [{ groupByDecorator: true }],
},
},
];

const decoratorRegexpOptions = [
{
order: ['before', '[decorator-starts-with-ab]', 'after', '[everything-else]'],
Expand Down Expand Up @@ -344,6 +354,11 @@ ruleTester.run('sort-class-members', rule, {
options: decoratorOptions,
},

// class properties without or without decorators regardless of the name
{
code: 'class A { bar = 2; foo = 3; static x = 55; constructor(){}; @dec something(){};}',
options: decoratorBooleanOptions,
},
// regexp decorators
{ code: 'class A { before(){} @abc() x = 4; after(){} }', options: decoratorRegexpOptions },
{
Expand Down

0 comments on commit c569a9b

Please sign in to comment.