-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.js
82 lines (75 loc) · 2.6 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const defaultGroups = {
'accessor-pairs': [{ 'accessorPair': true, 'sort': 'alphabetical' }],
'accessors': [{ 'kind': 'get', 'accessorPair': false, 'sort': 'alphabetical' }, { 'kind': 'set', 'accessorPair': false, 'sort': 'alphabetical' }],
'methods': [{ 'type': 'method', 'sort': 'alphabetical' }],
'private-accessor-pairs': [{ 'name': '/(_|#).+/', 'accessorPair': true, 'sort': 'alphabetical' }],
'private-accessors': [{ 'name': '/(_|#).+/', 'kind': 'get', 'accessorPair': false, 'sort': 'alphabetical' }, { 'name': '/(_|#).+/', 'kind': 'set', 'accessorPair': false, 'sort': 'alphabetical' }],
'private-methods': [{ 'name': '/(_|#).+/', 'type': 'method', 'sort': 'alphabetical' }],
'private-properties': [{ 'name': '/(_|#).+/', 'type': 'property', 'sort': 'none' }],
'properties': [{ 'type': 'property', 'sort': 'none' }],
'static-methods': [{ 'type': 'method', 'sort': 'alphabetical', 'static': true }],
'static-properties': [{ 'type': 'property', 'sort': 'none', 'static': true }]
};
const defaultOrder = [
'[static-properties]',
'[static-methods]',
'[properties]',
'constructor',
'[accessor-pairs]',
'[accessors]',
'[methods]',
'[private-properties]',
'[private-accessor-pairs]',
'[private-accessors]',
'[private-methods]'
];
export const getSortMemberRules = (order, groups) => {
return {
'sort-class-members/sort-class-members': [2, {
'order': order ? order : defaultOrder,
'groups': { ...defaultGroups, ...groups },
'accessorPairPositioning': 'getThenSet'
}]
};
};
function isGlobalIgnores(config) {
const keys = Object.keys(config);
return keys.length === 1 && keys[0] === 'ignores';
}
export function setDirectoryConfigs(globalConfig, directoryConfigs) {
const configs = globalConfig.map(c => ({ ...c }));
for (const dir in directoryConfigs) {
const pattern = `${dir}/**/*`;
for (const baseConfig of configs) {
if (isGlobalIgnores(baseConfig)) continue;
if (!(baseConfig.ignores)) baseConfig.ignores = [];
baseConfig.ignores.push(pattern);
}
for (const dirConfig of directoryConfigs[dir]) {
if (isGlobalIgnores(dirConfig)) continue;
const files = dirConfig.files ? dirConfig.files.map(f => `${dir}/${f}`) : [pattern];
configs.push({
...dirConfig,
files
});
}
}
return configs;
}
export function addExtensions(config, extensions) {
return config.map(c => {
if (isGlobalIgnores(c)) return c;
const newFiles = [];
for (const f of (c.files ?? ['**/*'])) {
if (f.includes('.')) newFiles.push(f);
else {
for (const ext of extensions)
newFiles.push(`${f}${f.endsWith('*') ? '' : '*'}${ext}`);
}
}
return {
...c,
files: newFiles
};
});
}