Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FED-2675 Only validate required props in null-safe components #917

Merged
merged 9 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
11 changes: 11 additions & 0 deletions lib/src/builder/codegen/typed_map_impl_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ abstract class TypedMapImplGenerator extends BoilerplateDeclarationGenerator {
..writeln(' @override')
..writeln(
' String \$getPropKey(void Function(Map m) accessMap) => $topLevelGetPropKeyAliasName(accessMap, (map) => ${names.implName}(map));');

if (!nullSafety) {
buffer
..writeln()
..writeln(' @override')
..writeln(' // ignore: must_call_super')
..writeln(' validateRequiredProps() {')
..writeln(
' // Disable required prop validation, until this component is null safe, by not calling super.')
..writeln(' }');
}
}
final requiredPropNamesToSkipValidation = this.requiredPropNamesToSkipValidation;
if (requiredPropNamesToSkipValidation != null && requiredPropNamesToSkipValidation.isNotEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void main() {
group('on invocation', () {
test('via call()', () {
expect(() {
(ComponentTest()
(RequiredPropsTest()
..requiredNullable = true
)();
},
Expand All @@ -40,7 +40,7 @@ void main() {

test('via build()', () {
expect(() {
(ComponentTest()
(RequiredPropsTest()
..requiredNullable = true
).build();
},
Expand All @@ -54,7 +54,7 @@ void main() {

test('on mount', () {
expect(() {
rtl.render((ComponentTest()
rtl.render((RequiredPropsTest()
..requiredNullable = true
)());
},
Expand All @@ -69,14 +69,14 @@ void main() {
late rtl.RenderResult view;

expect(() {
view = rtl.render((ComponentTest()
view = rtl.render((RequiredPropsTest()
..requiredNonNullable = true
..requiredNullable = true
)());
}, returnsNormally);

expect(() {
view.rerender((ComponentTest()
view.rerender((RequiredPropsTest()
..requiredNullable = true
)());
},
Expand All @@ -90,7 +90,7 @@ void main() {

test('does not throw when the prop is set', () {
expect(() {
rtl.render((ComponentTest()
rtl.render((RequiredPropsTest()
..requiredNonNullable = true
..requiredNullable = true
)());
Expand All @@ -103,7 +103,7 @@ void main() {
group('on invocation', () {
test('via call()', () {
expect(() {
(ComponentTest()
(RequiredPropsTest()
..requiredNonNullable = true
)();
},
Expand All @@ -115,7 +115,7 @@ void main() {

test('via build()', () {
expect(() {
(ComponentTest()
(RequiredPropsTest()
..requiredNonNullable = true
).build();
},
Expand All @@ -128,7 +128,7 @@ void main() {

test('on mount', () {
expect(() {
rtl.render((ComponentTest()
rtl.render((RequiredPropsTest()
..requiredNonNullable = true
)());
},
Expand All @@ -142,14 +142,14 @@ void main() {
late rtl.RenderResult view;

expect(() {
view = rtl.render((ComponentTest()
view = rtl.render((RequiredPropsTest()
..requiredNonNullable = true
..requiredNullable = true
)());
}, returnsNormally);

expect(() {
view.rerender((ComponentTest()
view.rerender((RequiredPropsTest()
..requiredNonNullable = true
)());
},
Expand All @@ -162,7 +162,7 @@ void main() {

test('does not throw when the prop is set to null', () {
expect(() {
rtl.render((ComponentTest()
rtl.render((RequiredPropsTest()
..requiredNonNullable = true
..requiredNullable = null
)());
Expand All @@ -171,7 +171,7 @@ void main() {

test('does not throw when the prop is set', () {
expect(() {
rtl.render((ComponentTest()
rtl.render((RequiredPropsTest()
..requiredNonNullable = true
..requiredNullable = true
)());
Expand All @@ -181,7 +181,7 @@ void main() {

test('@disableRequiredPropValidation annotation turns off validation for specific props', () {
expect(() {
rtl.render((ComponentTest()
rtl.render((RequiredPropsTest()
..requiredNonNullable = true
..requiredNullable = true
)());
Expand All @@ -190,7 +190,7 @@ void main() {

test('disableRequiredPropValidation method turns off validation for component usage', () {
expect(() {
rtl.render((ComponentTest()
rtl.render((RequiredPropsTest()
..disableRequiredPropValidation()
)());
}, allOf(returnsNormally, logsNoPropTypeWarnings));
Expand Down Expand Up @@ -301,18 +301,18 @@ void main() {

test('(New boilerplate) validates required props: does not throw in dart2js', () {
expect(() {
rtl.render(ComponentTest()());
rtl.render(ComponentTest().build());
rtl.render(RequiredPropsTest()());
rtl.render(RequiredPropsTest().build());
},
returnsNormally);
}, tags: 'no-ddc');
}

// ignore: undefined_identifier, invalid_assignment
UiFactory<ComponentTestProps> ComponentTest = _$ComponentTest;
UiFactory<RequiredPropsTestProps> RequiredPropsTest = _$RequiredPropsTest;

@Props(disableRequiredPropValidation: {'testIgnoreOnMixin'})
mixin ComponentTestProps on UiProps {
mixin RequiredPropsTestProps on UiProps {
late bool requiredNonNullable;

late bool? requiredNullable;
Expand All @@ -332,7 +332,7 @@ mixin ComponentTestProps on UiProps {
late bool testIgnoreOnMixin;
}

class ComponentTestComponent extends UiComponent2<ComponentTestProps> {
class RequiredPropsTestComponent extends UiComponent2<RequiredPropsTestProps> {
@override
render() => Dom.div()();
}
Expand All @@ -347,7 +347,7 @@ mixin MultipleMixinsTestPropsMixin on UiProps {
}

@Props(disableRequiredPropValidation: {'testIgnoreOnMixin'})
class MultipleMixinsTestProps = UiProps with MultipleMixinsTestPropsMixin, ComponentTestProps;
class MultipleMixinsTestProps = UiProps with MultipleMixinsTestPropsMixin, RequiredPropsTestProps;


mixin WrapperTestPropsMixin on UiProps {
Expand All @@ -361,7 +361,7 @@ UiFactory<FunctionWrapperTestProps> FunctionWrapperTest = uiFunction(


@Props(disableRequiredPropValidation: {'thirdRequiredProp', 'secondRequiredProp', 'requiredNonNullable', 'requiredNullable', 'testIgnoreOnMixin'})
class FunctionWrapperTestProps = UiProps with WrapperTestPropsMixin, MultipleMixinsTestPropsMixin, ComponentTestProps;
class FunctionWrapperTestProps = UiProps with WrapperTestPropsMixin, MultipleMixinsTestPropsMixin, RequiredPropsTestProps;

@Factory()
// ignore: undefined_identifier, invalid_assignment
Expand Down Expand Up @@ -396,7 +396,7 @@ mixin ClassWrapperTest2PropsMixin on UiProps {
}

@Props(disableRequiredPropValidation: {'thirdRequiredProp', 'secondRequiredProp', 'requiredNonNullable', 'requiredNullable', 'testIgnoreOnMixin'})
class ClassWrapperTest2Props = UiProps with ClassWrapperTest2PropsMixin, MultipleMixinsTestPropsMixin, ComponentTestProps;
class ClassWrapperTest2Props = UiProps with ClassWrapperTest2PropsMixin, MultipleMixinsTestPropsMixin, RequiredPropsTestProps;

class ClassWrapperTest2Component extends UiComponent2<ClassWrapperTest2Props> {
@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ class _$$AnnotationErrorDefaultPropsProps
_$getPropKey$_$$AnnotationErrorDefaultPropsProps(
accessMap, (map) => _$$AnnotationErrorDefaultPropsProps(map));

@override
// ignore: must_call_super
validateRequiredProps() {
// Disable required prop validation, until this component is null safe, by not calling super.
}

@override
Set<String> get requiredPropNamesToSkipValidation => const {'id'};
}
Expand Down Expand Up @@ -183,6 +189,12 @@ class _$$AnnotationErrorProps extends _$AnnotationErrorProps
String $getPropKey(void Function(Map m) accessMap) =>
_$getPropKey$_$$AnnotationErrorProps(
accessMap, (map) => _$$AnnotationErrorProps(map));

@override
// ignore: must_call_super
validateRequiredProps() {
// Disable required prop validation, until this component is null safe, by not calling super.
}
}

/// An alias for [getPropKey] so it can be referenced within the props class impl
Expand Down Expand Up @@ -282,6 +294,12 @@ class _$$AnnotationErrorStatefulProps extends _$AnnotationErrorStatefulProps
String $getPropKey(void Function(Map m) accessMap) =>
_$getPropKey$_$$AnnotationErrorStatefulProps(
accessMap, (map) => _$$AnnotationErrorStatefulProps(map));

@override
// ignore: must_call_super
validateRequiredProps() {
// Disable required prop validation, until this component is null safe, by not calling super.
}
}

/// An alias for [getPropKey] so it can be referenced within the props class impl
Expand Down Expand Up @@ -436,6 +454,12 @@ class _$$AnnotationErrorStatefulDefaultPropsProps
_$getPropKey$_$$AnnotationErrorStatefulDefaultPropsProps(
accessMap, (map) => _$$AnnotationErrorStatefulDefaultPropsProps(map));

@override
// ignore: must_call_super
validateRequiredProps() {
// Disable required prop validation, until this component is null safe, by not calling super.
}

@override
Set<String> get requiredPropNamesToSkipValidation => const {'id'};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ abstract class _$$ComponentTestProps extends _$ComponentTestProps
_$getPropKey$_$$ComponentTestProps(
accessMap, (map) => _$$ComponentTestProps(map));

@override
// ignore: must_call_super
validateRequiredProps() {
// Disable required prop validation, until this component is null safe, by not calling super.
}

@override
Set<String> get requiredPropNamesToSkipValidation =>
const {'id', 'shouldSetPropsDirectly', 'shouldUseJsFactory'};
Expand Down Expand Up @@ -379,6 +385,12 @@ abstract class _$$IsErrorBoundaryProps extends _$IsErrorBoundaryProps
String $getPropKey(void Function(Map m) accessMap) =>
_$getPropKey$_$$IsErrorBoundaryProps(
accessMap, (map) => _$$IsErrorBoundaryProps(map));

@override
// ignore: must_call_super
validateRequiredProps() {
// Disable required prop validation, until this component is null safe, by not calling super.
}
}

/// An alias for [getPropKey] so it can be referenced within the props class impl
Expand Down Expand Up @@ -535,6 +547,12 @@ abstract class _$$IsNotErrorBoundaryProps extends _$IsNotErrorBoundaryProps
String $getPropKey(void Function(Map m) accessMap) =>
_$getPropKey$_$$IsNotErrorBoundaryProps(
accessMap, (map) => _$$IsNotErrorBoundaryProps(map));

@override
// ignore: must_call_super
validateRequiredProps() {
// Disable required prop validation, until this component is null safe, by not calling super.
}
}

/// An alias for [getPropKey] so it can be referenced within the props class impl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ abstract class _$$ComponentTestProps extends _$ComponentTestProps
String $getPropKey(void Function(Map m) accessMap) =>
_$getPropKey$_$$ComponentTestProps(
accessMap, (map) => _$$ComponentTestProps(map));

@override
// ignore: must_call_super
validateRequiredProps() {
// Disable required prop validation, until this component is null safe, by not calling super.
}
}

/// An alias for [getPropKey] so it can be referenced within the props class impl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ abstract class _$$DoNotGenerateAccessorTestProps
String $getPropKey(void Function(Map m) accessMap) =>
_$getPropKey$_$$DoNotGenerateAccessorTestProps(
accessMap, (map) => _$$DoNotGenerateAccessorTestProps(map));

@override
// ignore: must_call_super
validateRequiredProps() {
// Disable required prop validation, until this component is null safe, by not calling super.
}
}

/// An alias for [getPropKey] so it can be referenced within the props class impl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ abstract class _$$NamespacedAccessorTestProps
String $getPropKey(void Function(Map m) accessMap) =>
_$getPropKey$_$$NamespacedAccessorTestProps(
accessMap, (map) => _$$NamespacedAccessorTestProps(map));

@override
// ignore: must_call_super
validateRequiredProps() {
// Disable required prop validation, until this component is null safe, by not calling super.
}
}

/// An alias for [getPropKey] so it can be referenced within the props class impl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ abstract class _$$FooProps extends _$FooProps
String $getPropKey(void Function(Map m) accessMap) =>
_$getPropKey$_$$FooProps(accessMap, (map) => _$$FooProps(map));

@override
// ignore: must_call_super
validateRequiredProps() {
// Disable required prop validation, until this component is null safe, by not calling super.
}

@override
Set<String> get requiredPropNamesToSkipValidation => const {'_privateProp'};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ abstract class _$$ComponentTestProps extends _$ComponentTestProps
String $getPropKey(void Function(Map m) accessMap) =>
_$getPropKey$_$$ComponentTestProps(
accessMap, (map) => _$$ComponentTestProps(map));

@override
// ignore: must_call_super
validateRequiredProps() {
// Disable required prop validation, until this component is null safe, by not calling super.
}
}

/// An alias for [getPropKey] so it can be referenced within the props class impl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ abstract class _$$StatefulComponentTestProps
_$getPropKey$_$$StatefulComponentTestProps(
accessMap, (map) => _$$StatefulComponentTestProps(map));

@override
// ignore: must_call_super
validateRequiredProps() {
// Disable required prop validation, until this component is null safe, by not calling super.
}

@override
Set<String> get requiredPropNamesToSkipValidation =>
const {'setStateDirectly'};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ abstract class _$$FooProps extends _$FooProps
String $getPropKey(void Function(Map m) accessMap) =>
_$getPropKey$_$$FooProps(accessMap, (map) => _$$FooProps(map));

@override
// ignore: must_call_super
validateRequiredProps() {
// Disable required prop validation, until this component is null safe, by not calling super.
}

@override
Set<String> get requiredPropNamesToSkipValidation => const {'id'};
}
Expand Down
Loading
Loading