@@ -15,17 +15,17 @@ PropertyDeclaration parsePropertyDeclaration(
15
15
ParsedSymbolgraph symbolgraph, {
16
16
bool isStatic = false ,
17
17
}) {
18
- final isConstant = _parseVariableIsConstant (propertySymbolJson);
18
+ final info = parsePropertyInfo (propertySymbolJson[ 'declarationFragments' ] );
19
19
return PropertyDeclaration (
20
20
id: parseSymbolId (propertySymbolJson),
21
21
name: parseSymbolName (propertySymbolJson),
22
22
type: _parseVariableType (propertySymbolJson, symbolgraph),
23
23
hasObjCAnnotation: parseSymbolHasObjcAnnotation (propertySymbolJson),
24
- isConstant: isConstant ,
25
- hasSetter: isConstant ? false : _parsePropertyHasSetter (propertySymbolJson) ,
24
+ isConstant: info.constant ,
25
+ hasSetter: info.constant ? false : info.setter ,
26
26
isStatic: isStatic,
27
- throws: _parseVariableThrows (propertySymbolJson) ,
28
- async : _parseVariableAsync (propertySymbolJson) ,
27
+ throws: info.throws ,
28
+ async : info. async ,
29
29
);
30
30
}
31
31
@@ -34,15 +34,14 @@ GlobalVariableDeclaration parseGlobalVariableDeclaration(
34
34
ParsedSymbolgraph symbolgraph, {
35
35
bool isStatic = false ,
36
36
}) {
37
- final isConstant = _parseVariableIsConstant (variableSymbolJson);
38
- final hasSetter = _parsePropertyHasSetter (variableSymbolJson);
37
+ final info = parsePropertyInfo (variableSymbolJson['declarationFragments' ]);
39
38
return GlobalVariableDeclaration (
40
39
id: parseSymbolId (variableSymbolJson),
41
40
name: parseSymbolName (variableSymbolJson),
42
41
type: _parseVariableType (variableSymbolJson, symbolgraph),
43
- isConstant: isConstant || ! hasSetter ,
44
- throws: _parseVariableThrows (variableSymbolJson) ,
45
- async : _parseVariableAsync (variableSymbolJson) ,
42
+ isConstant: info.constant || ! info.setter ,
43
+ throws: info.throws ,
44
+ async : info. async ,
46
45
);
47
46
}
48
47
@@ -53,9 +52,7 @@ ReferredType _parseVariableType(
53
52
parseTypeAfterSeparator (
54
53
TokenList (propertySymbolJson['names' ]['subHeading' ]), symbolgraph);
55
54
56
- bool _parseVariableIsConstant (Json variableSymbolJson) {
57
- final fragmentsJson = variableSymbolJson['declarationFragments' ];
58
-
55
+ bool _parseVariableIsConstant (Json fragmentsJson) {
59
56
final declarationKeyword = fragmentsJson.firstWhere (
60
57
(json) =>
61
58
matchFragment (json, 'keyword' , 'var' ) ||
@@ -65,24 +62,39 @@ bool _parseVariableIsConstant(Json variableSymbolJson) {
65
62
'Expected to find "var" or "let" as a keyword, found none' ,
66
63
),
67
64
);
68
-
69
65
return matchFragment (declarationKeyword, 'keyword' , 'let' );
70
66
}
71
67
72
- bool _parseVariableThrows (Json json) {
73
- final throws = json['declarationFragments' ]
74
- .any ((frag) => matchFragment (frag, 'keyword' , 'throws' ));
75
- return throws;
68
+ bool _findKeywordInFragments (Json json, String keyword) {
69
+ return json.any ((frag) => matchFragment (frag, 'keyword' , keyword));
76
70
}
77
71
78
- bool _parseVariableAsync (Json json) {
79
- final async = json['declarationFragments' ]
80
- .any ((frag) => matchFragment (frag, 'keyword' , 'async' ));
81
- return async ;
72
+ typedef ParsedPropertyInfo = ({
73
+ bool async ,
74
+ bool throws,
75
+ bool constant,
76
+ bool getter,
77
+ bool setter,
78
+ bool mutating,
79
+ });
80
+
81
+ ParsedPropertyInfo parsePropertyInfo (Json json) {
82
+ final (getter, setter) = _parsePropertyGetAndSet (json);
83
+ return (
84
+ constant: _parseVariableIsConstant (json),
85
+ async : _findKeywordInFragments (json, 'async' ),
86
+ throws: _findKeywordInFragments (json, 'throws' ),
87
+ getter: getter,
88
+ setter: setter,
89
+ mutating: _findKeywordInFragments (json, 'mutating' )
90
+ );
82
91
}
83
92
84
- bool _parsePropertyHasSetter (Json propertySymbolJson) {
85
- final fragmentsJson = propertySymbolJson['declarationFragments' ];
93
+ (bool , bool ) _parsePropertyGetAndSet (Json fragmentsJson, {String ? path}) {
94
+ if (fragmentsJson.any ((frag) => matchFragment (frag, 'text' , ' { get }' ))) {
95
+ // has explicit getter and no explicit setter
96
+ return (true , false );
97
+ }
86
98
87
99
final hasExplicitSetter =
88
100
fragmentsJson.any ((frag) => matchFragment (frag, 'keyword' , 'set' ));
@@ -92,21 +104,21 @@ bool _parsePropertyHasSetter(Json propertySymbolJson) {
92
104
if (hasExplicitGetter) {
93
105
if (hasExplicitSetter) {
94
106
// has explicit getter and has explicit setter
95
- return true ;
107
+ return ( true , true ) ;
96
108
} else {
97
109
// has explicit getter and no explicit setter
98
- return false ;
110
+ return ( true , false ) ;
99
111
}
100
112
} else {
101
113
if (hasExplicitSetter) {
102
114
// has no explicit getter and but has explicit setter
103
115
throw Exception (
104
- 'Invalid property at ${ propertySymbolJson . path }. '
116
+ 'Invalid property${ path != null ? ' at $path ' : '' }. '
105
117
'Properties can not have a setter without a getter' ,
106
118
);
107
119
} else {
108
- // has no explicit getter and no explicit setter
109
- return true ;
120
+ // has implicit getter and implicit setter
121
+ return ( true , true ) ;
110
122
}
111
123
}
112
124
}
0 commit comments