Skip to content

Commit 65dfc96

Browse files
authored
Make groupByDecorator regex-able (#90)
1 parent ccd09a3 commit 65dfc96

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Members can be matched to positional slots using several criteria, including nam
110110
- `readonly`: `true|false` to restrict the match to members with typescript `readonly` keyword.
111111
- `async`: `true|false` to restrict the match to async members.
112112
- `sort`: `"alphabetical"|"none"`. Used to require a specific sorting within the slot for matched members. Defaults to `"none"`.
113-
- `groupByDecorator`: a string used to group properties with the same decorator name (e.g. `observable` for `@observable`). 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).
113+
- `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).
114114

115115
A few examples:
116116

src/rules/sort-class-members.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ function expandSlot(input, groups) {
349349
return [];
350350
}
351351

352-
const testName = slot.name && getNameComparer(slot.name);
352+
const testName = slot.name && getStringComparer(slot.name);
353353
if (testName) {
354354
slot.testName = testName;
355355
}
@@ -372,24 +372,24 @@ function matchAccessorPairs(members) {
372372
});
373373
}
374374

375-
function getNameComparer(name) {
376-
if (name[0] === '/') {
377-
let namePattern = name.substr(1, name.length - 2);
375+
function getStringComparer(str) {
376+
if (str[0] === '/') {
377+
let strPattern = str.substr(1, str.length - 2);
378378

379-
if (namePattern[0] !== '^') {
380-
namePattern = `^${namePattern}`;
379+
if (strPattern[0] !== '^') {
380+
strPattern = `^${strPattern}`;
381381
}
382382

383-
if (namePattern[namePattern.length - 1] !== '$') {
384-
namePattern += '$';
383+
if (strPattern[strPattern.length - 1] !== '$') {
384+
strPattern += '$';
385385
}
386386

387-
const re = new RegExp(namePattern);
387+
const re = new RegExp(strPattern);
388388

389-
return (n) => re.test(n);
389+
return (s) => re.test(s);
390390
}
391391

392-
return (n) => n === name;
392+
return (s) => s === str;
393393
}
394394

395395
function flatten(collection) {
@@ -436,7 +436,10 @@ const comparers = [
436436
{
437437
property: 'groupByDecorator',
438438
value: 10,
439-
test: (m, s) => m.decorators.includes(s.groupByDecorator),
439+
test: (m, s) => {
440+
const comparer = getStringComparer(s.groupByDecorator);
441+
return m.decorators.some((decorator) => comparer(decorator));
442+
},
440443
},
441444
{
442445
property: 'accessorPair',

test/rules/sort-class-members.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ const decoratorOptions = [
142142
},
143143
];
144144

145+
const decoratorRegexpOptions = [
146+
{
147+
order: ['before', '[decorator-starts-with-ab]', 'after', '[everything-else]'],
148+
groups: {
149+
'decorator-starts-with-ab': [{ groupByDecorator: '/ab.+/' }],
150+
},
151+
},
152+
];
153+
145154
const decoratorOptionsAlphabetical = [
146155
{
147156
order: ['[observables]', '[properties]'],
@@ -322,6 +331,13 @@ ruleTester.run('sort-class-members', rule, {
322331
code: 'class A { @observable bar = 2; @observable foo = 1; @Inject() @observable fuga = 5; baz = 3; constructor(){}; @Inject() hoge = 4; }',
323332
options: decoratorOptions,
324333
},
334+
335+
// regexp decorators
336+
{ code: 'class A { before(){} @abc() x = 4; after(){} }', options: decoratorRegexpOptions },
337+
{
338+
code: 'class A { before(){} @something @abc() x = 4; after(){} xyz(){} }',
339+
options: decoratorRegexpOptions,
340+
},
325341
]),
326342

327343
// regexp names

0 commit comments

Comments
 (0)