Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased
- [#989] Optimize generated code to decrease dart2js compile size, saving ~577 bytes per component (when using `-03 --csp --minify`)
- [#992] Fix compilation errors for legacy boilerplate defined in libraries with a Dart language version of >=3.0
- [#993] Allow `@convertJsMapProp` props to be typed as `Map<dynamic, dynamic>?`, not just `Map?`

## 5.4.6
- [#986] Set up gha-dart-oss
Expand Down
13 changes: 10 additions & 3 deletions lib/src/builder/codegen/accessors_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,22 @@ PropConversionConfig? parseConversionConfig({
final convertJsRefProp =
getConstantAnnotation(field, 'convertJsRefProp', annotations.convertJsRefProp);
if (convertJsMapProp != null) {
const allowedConvertedTypes = {
'Map?',
'Map<dynamic, dynamic>?',
};
conversionConfig = PropConversionConfig(
rawType: 'JsMap?',
convertedType: 'Map?',
setter: 'jsifyMapProp',
getter: 'unjsifyMapProp',
);
if (conversionConfig.convertedType != typeSource) {
assert(allowedConvertedTypes.contains(conversionConfig.convertedType));
if (!allowedConvertedTypes.contains(typeSource)) {
handleAnnotationError(
'A prop annotated with `@convertJsMapProp` should be typed as `Map?`.', field);
'A prop annotated with `@convertJsMapProp` must be typed as one of: '
'${allowedConvertedTypes.map((t) => '`$t`').join(', ')}',
field);
return null;
}
} else if (convertJsRefProp != null) {
Expand All @@ -489,7 +496,7 @@ PropConversionConfig? parseConversionConfig({
);
if (conversionConfig.convertedType != typeSource) {
handleAnnotationError(
'A prop annotated with `@convertJsRefProp` should be typed as `dynamic`.', field);
'A prop annotated with `@convertJsRefProp` must be typed as `dynamic`.', field);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,30 @@ void main() {
reason: 'getter should convert value');
});

test('@convertJsMapProp works as expected', () {
final map = {'abc': true};
final mapPropKey = Test().getPropKey((p) => p.mapProp);

final setterResult = (Test()..mapProp = map)[mapPropKey];
expect(setterResult, isA<JsMap>(), reason: 'setter should convert value');
expect(unjsifyMapProp(setterResult as JsMap), map);

expect(Test({mapPropKey: jsifyMapProp(map)}).mapProp, map,
reason: 'getter should convert value');
group('@convertJsMapProp works as expected', () {
test('without generics', () {
final map = {'abc': true};
final propKey = Test().getPropKey((p) => p.mapPropWithGenerics);

final setterResult = (Test()..mapPropWithGenerics = map)[propKey];
expect(setterResult, isA<JsMap>(), reason: 'setter should convert value');
expect(unjsifyMapProp(setterResult as JsMap), map);

expect(Test({propKey: jsifyMapProp(map)}).mapPropWithGenerics, map,
reason: 'getter should convert value');
});

test('with generics', () {
final map = {'abc': true};
final propKey = Test().getPropKey((p) => p.mapProp);

final setterResult = (Test()..mapProp = map)[propKey];
expect(setterResult, isA<JsMap>(), reason: 'setter should convert value');
expect(unjsifyMapProp(setterResult as JsMap), map);

expect(Test({propKey: jsifyMapProp(map)}).mapProp, map,
reason: 'getter should convert value');
});
});

test('@convertJsRefProp works as expected', () {
Expand Down Expand Up @@ -89,6 +103,9 @@ mixin TestProps on UiProps {
@convertJsMapProp
Map? mapProp;

@convertJsMapProp
Map<dynamic, dynamic>? mapPropWithGenerics;

@convertJsRefProp
dynamic refProp;
}
4 changes: 2 additions & 2 deletions test/vm_tests/builder/codegen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ main() {
late String foo;''';

setUpAndGenerate(OverReactSrc.abstractProps(backwardsCompatible: false, body: body).source);
verify(() => logger.severe(contains('Unsupported prop annotation combination for prop \'foo\': A prop annotated with `@convertJsMapProp` should be typed as `Map?`.')));
verify(() => logger.severe(contains('Unsupported prop annotation combination for prop \'foo\': A prop annotated with `@convertJsMapProp` must be typed as one of: `Map?`, `Map<dynamic, dynamic>?`')));
});

test('@convertJsRefProp of the wrong prop type', () {
Expand All @@ -1209,7 +1209,7 @@ main() {
Map? foo;''';

setUpAndGenerate(OverReactSrc.abstractProps(backwardsCompatible: false, body: body).source);
verify(() => logger.severe(contains('Unsupported prop annotation combination for prop \'foo\': A prop annotated with `@convertJsRefProp` should be typed as `dynamic`.')));
verify(() => logger.severe(contains('Unsupported prop annotation combination for prop \'foo\': A prop annotated with `@convertJsRefProp` must be typed as `dynamic`.')));
});
});
});
Expand Down