|
1 | 1 | part of 'member_ordering_rule.dart';
|
2 | 2 |
|
3 | 3 | class _ConfigParser {
|
4 |
| - static const _orderConfig = 'order'; |
5 |
| - |
6 |
| - static List<_MembersGroup> parseOrder(Map<String, Object> config) { |
7 |
| - final order = config[_orderConfig] is Iterable |
8 |
| - ? List<String>.from(config[_orderConfig] as Iterable) |
9 |
| - : <String>[]; |
10 |
| - |
11 |
| - return order.isEmpty |
12 |
| - ? _MembersGroup._groupsOrder |
13 |
| - : order |
14 |
| - .map((group) => group.snakeCaseToKebab()) |
15 |
| - .map(_MembersGroup.parse) |
16 |
| - .whereNotNull() |
17 |
| - .toList(); |
| 4 | + static const _defaultOrderList = [ |
| 5 | + 'public-fields', |
| 6 | + 'private-fields', |
| 7 | + 'public-getters', |
| 8 | + 'private-getters', |
| 9 | + 'public-setters', |
| 10 | + 'private-setters', |
| 11 | + 'constructors', |
| 12 | + 'public-methods', |
| 13 | + 'private-methods', |
| 14 | + ]; |
| 15 | + |
| 16 | + static final _regExp = RegExp( |
| 17 | + '(overridden-|protected-)?(private-|public-)?(static-)?(late-)?' |
| 18 | + '(var-|final-|const-)?(nullable-)?(named-)?(factory-)?(build-)?' |
| 19 | + '(init-state-)?(did-change-dependencies-)?(did-update-widget-)?' |
| 20 | + '(dispose-)?', |
| 21 | + ); |
| 22 | + |
| 23 | + static List<_MemberGroup> parseOrder(Map<String, Object> config) { |
| 24 | + final order = config['order'] is Iterable |
| 25 | + ? List<String>.from(config['order'] as Iterable) |
| 26 | + : _defaultOrderList; |
| 27 | + |
| 28 | + return order.map(_parseGroup).whereNotNull().toList(); |
18 | 29 | }
|
19 | 30 |
|
20 | 31 | static bool parseAlphabetize(Map<String, Object> config) =>
|
21 | 32 | (config['alphabetize'] as bool?) ?? false;
|
| 33 | + |
| 34 | + static bool parseAlphabetizeByType(Map<String, Object> config) => |
| 35 | + (config['alphabetize-by-type'] as bool?) ?? false; |
| 36 | + |
| 37 | + // ignore: long-method |
| 38 | + static _MemberGroup? _parseGroup(String group) { |
| 39 | + final lastGroup = group.endsWith('getters-setters') |
| 40 | + ? 'getters-setters' |
| 41 | + : group.split('-').lastOrNull; |
| 42 | + final type = _MemberType.parse(lastGroup); |
| 43 | + final result = _regExp.allMatches(group.toLowerCase()); |
| 44 | + |
| 45 | + final hasGroups = result.isNotEmpty && result.first.groupCount > 0; |
| 46 | + if (hasGroups && type != null) { |
| 47 | + final match = result.first; |
| 48 | + |
| 49 | + final annotation = _Annotation.parse(match.group(1)?.replaceAll('-', '')); |
| 50 | + final modifier = _Modifier.parse(match.group(2)?.replaceAll('-', '')); |
| 51 | + final isStatic = match.group(3) != null; |
| 52 | + final isLate = match.group(4) != null; |
| 53 | + final keyword = _FieldKeyword.parse(match.group(5)?.replaceAll('-', '')); |
| 54 | + final isNullable = match.group(6) != null; |
| 55 | + final isNamed = match.group(7) != null; |
| 56 | + final isFactory = match.group(8) != null; |
| 57 | + final isBuild = match.group(9) != null; |
| 58 | + final isInitState = match.group(10) != null; |
| 59 | + final isDidChangeDependencies = match.group(11) != null; |
| 60 | + final isDidUpdateWidget = match.group(12) != null; |
| 61 | + final isDispose = match.group(13) != null; |
| 62 | + |
| 63 | + switch (type) { |
| 64 | + case _MemberType.field: |
| 65 | + return _FieldMemberGroup._( |
| 66 | + isLate: isLate, |
| 67 | + isNullable: isNullable, |
| 68 | + isStatic: isStatic, |
| 69 | + keyword: keyword, |
| 70 | + annotation: annotation, |
| 71 | + memberType: type, |
| 72 | + modifier: modifier, |
| 73 | + rawRepresentation: group, |
| 74 | + ); |
| 75 | + |
| 76 | + case _MemberType.method: |
| 77 | + return _MethodMemberGroup._( |
| 78 | + isNullable: isNullable, |
| 79 | + isStatic: isStatic, |
| 80 | + isBuild: isBuild, |
| 81 | + isInitState: isInitState, |
| 82 | + isDidChangeDependencies: isDidChangeDependencies, |
| 83 | + isDidUpdateWidget: isDidUpdateWidget, |
| 84 | + isDispose: isDispose, |
| 85 | + annotation: annotation, |
| 86 | + memberType: type, |
| 87 | + modifier: modifier, |
| 88 | + rawRepresentation: group, |
| 89 | + ); |
| 90 | + |
| 91 | + case _MemberType.getter: |
| 92 | + case _MemberType.setter: |
| 93 | + case _MemberType.getterAndSetter: |
| 94 | + return _GetSetMemberGroup._( |
| 95 | + isNullable: isNullable, |
| 96 | + isStatic: isStatic, |
| 97 | + annotation: annotation, |
| 98 | + memberType: type, |
| 99 | + modifier: modifier, |
| 100 | + rawRepresentation: group, |
| 101 | + ); |
| 102 | + |
| 103 | + case _MemberType.constructor: |
| 104 | + return _ConstructorMemberGroup._( |
| 105 | + isNamed: isFactory || isNamed, |
| 106 | + isFactory: isFactory, |
| 107 | + annotation: annotation, |
| 108 | + memberType: type, |
| 109 | + modifier: modifier, |
| 110 | + rawRepresentation: group, |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return null; |
| 116 | + } |
22 | 117 | }
|
0 commit comments