2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
+ import 'package:collection/collection.dart' ;
6
+
5
7
import '../../ast/_core/interfaces/declaration.dart' ;
6
8
import '../../ast/_core/shared/referred_type.dart' ;
7
9
import '../../ast/declarations/built_in/built_in_declaration.dart' ;
@@ -11,13 +13,6 @@ import '../../parser/_core/utils.dart';
11
13
import '../../transformer/_core/utils.dart' ;
12
14
import '../transform.dart' ;
13
15
14
- final primitiveWrappers = < ReferredType , ReferredType > {
15
- intType: _createWrapperClass (intType),
16
- floatType: _createWrapperClass (floatType),
17
- doubleType: _createWrapperClass (doubleType),
18
- boolType: _createWrapperClass (boolType),
19
- };
20
-
21
16
ReferredType _createWrapperClass (DeclaredType primitiveType) {
22
17
final property = PropertyDeclaration (
23
18
id: primitiveType.id.addIdSuffix ('wrappedInstance' ),
@@ -40,29 +35,31 @@ ReferredType _createWrapperClass(DeclaredType primitiveType) {
40
35
41
36
(ReferredType , bool ) getWrapperIfNeeded (
42
37
ReferredType type,
43
- bool isThrows ,
38
+ bool shouldWrapPrimitives ,
44
39
TransformationMap transformationMap,
45
40
) {
46
- if (type is ! DeclaredType || ! _isPrimitiveType (type) || ! isThrows ) {
41
+ if (type is ! DeclaredType || ! shouldWrapPrimitives ) {
47
42
return (type, false );
48
43
}
49
44
50
45
final wrapper = getPrimitiveWrapper (type);
46
+ if (wrapper == null ) {
47
+ return (type, false );
48
+ }
49
+
51
50
transformationMap[type.declaration] = (wrapper as DeclaredType ).declaration;
52
51
return (wrapper, true );
53
52
}
54
53
55
- ReferredType getPrimitiveWrapper (DeclaredType other) {
56
- return primitiveWrappers.entries
57
- . firstWhere (
58
- (entry) => entry.key. sameAs (other ),
59
- )
60
- .value;
61
- }
54
+ ReferredType ? getPrimitiveWrapper (DeclaredType other) {
55
+ final primitiveWrappers = < ( ReferredType , ReferredType ) > [
56
+ (intType, _createWrapperClass (intType)),
57
+ (floatType, _createWrapperClass (floatType) ),
58
+ (doubleType, _createWrapperClass (doubleType)),
59
+ (boolType, _createWrapperClass (boolType)),
60
+ ];
62
61
63
- bool _isPrimitiveType (DeclaredType type) {
64
- return type.name == 'Int' ||
65
- type.name == 'Float' ||
66
- type.name == 'Double' ||
67
- type.name == 'Bool' ;
62
+ return primitiveWrappers
63
+ .firstWhereOrNull ((pair) => pair.$1.sameAs (other))
64
+ ? .$2;
68
65
}
0 commit comments