diff --git a/packages/react-native-compatibility-check/src/ComparisonResult.js b/packages/react-native-compatibility-check/src/ComparisonResult.js index b131979a0b9..24b9df031e3 100644 --- a/packages/react-native-compatibility-check/src/ComparisonResult.js +++ b/packages/react-native-compatibility-check/src/ComparisonResult.js @@ -135,12 +135,36 @@ export type NullableComparisonResult = { export type ComparisonResult = | {status: 'matching'} | {status: 'skipped'} - | {status: 'nullableChange', nullableLog: NullableComparisonResult} - | {status: 'properties', propertyLog: PropertiesComparisonResult} - | {status: 'members', memberLog: MembersComparisonResult} - | {status: 'unionMembers', memberLog: UnionMembersComparisonResult} - | {status: 'functionChange', functionChangeLog: FunctionComparisonResult} - | {status: 'positionalTypeChange', changeLog: PositionalComparisonResult} + | { + status: 'nullableChange', + nullableLog: NullableComparisonResult, + errorLog?: TypeComparisonError, + } + | { + status: 'properties', + propertyLog: PropertiesComparisonResult, + errorLog?: TypeComparisonError, + } + | { + status: 'members', + memberLog: MembersComparisonResult, + errorLog?: TypeComparisonError, + } + | { + status: 'unionMembers', + memberLog: UnionMembersComparisonResult, + errorLog?: TypeComparisonError, + } + | { + status: 'functionChange', + functionChangeLog: FunctionComparisonResult, + errorLog?: TypeComparisonError, + } + | { + status: 'positionalTypeChange', + changeLog: PositionalComparisonResult, + errorLog?: TypeComparisonError, + } | {status: 'error', errorLog: TypeComparisonError}; export function isPropertyLogEmpty( diff --git a/packages/react-native-compatibility-check/src/ErrorFormatting.js b/packages/react-native-compatibility-check/src/ErrorFormatting.js index 39df6aad8fc..ddbed90d775 100644 --- a/packages/react-native-compatibility-check/src/ErrorFormatting.js +++ b/packages/react-native-compatibility-check/src/ErrorFormatting.js @@ -32,6 +32,7 @@ export function formatErrorMessage( ): string { switch (error.type) { case 'PropertyComparisonError': + const propertyPreviousError = error.previousError; const formattedProperties = error.mismatchedProperties.map( individualPropertyError => indentedLineStart(indent + 1) + @@ -42,7 +43,14 @@ export function formatErrorMessage( formatErrorMessage(individualPropertyError.fault, indent + 2) : ''), ); - return error.message + formattedProperties.join(''); + return ( + (propertyPreviousError != null + ? formatErrorMessage(propertyPreviousError, indent) + + indentedLineStart(indent + 1) + : '') + + error.message + + formattedProperties.join('') + ); case 'PositionalComparisonError': const formattedPositionalChanges = error.erroneousItems.map( ([index, type]) => @@ -188,7 +196,13 @@ function formatTypeAnnotation(annotation: CompleteTypeAnnotation): string { ? `'${annotation.value}'` : annotation.value; case 'UnionTypeAnnotation': - const validUnionType = parseValidUnionType(annotation); + let validUnionType; + try { + validUnionType = parseValidUnionType(annotation); + } catch (_e: mixed) { + // parseValidUnionType throws for unsupported union types + return 'Union'; + } switch (validUnionType) { case 'boolean': if ( diff --git a/packages/react-native-compatibility-check/src/TypeDiffing.js b/packages/react-native-compatibility-check/src/TypeDiffing.js index d808a6893b6..6d02574f5b8 100644 --- a/packages/react-native-compatibility-check/src/TypeDiffing.js +++ b/packages/react-native-compatibility-check/src/TypeDiffing.js @@ -569,12 +569,22 @@ export function compareObjectTypes( return { status: 'properties', propertyLog: {missingProperties: sortedOlderTypes}, + errorLog: typeAnnotationComparisonError( + 'Object has property changes', + objectTypeAnnotation(newerPropertyTypes), + objectTypeAnnotation(olderPropertyTypes), + ), }; } if (sortedOlderTypes.length === 0) { return { status: 'properties', propertyLog: {addedProperties: sortedNewerTypes}, + errorLog: typeAnnotationComparisonError( + 'Object has property changes', + objectTypeAnnotation(newerPropertyTypes), + objectTypeAnnotation(olderPropertyTypes), + ), }; } const result = comparePropertyArrays(sortedNewerTypes, sortedOlderTypes); @@ -602,14 +612,20 @@ export function compareObjectTypes( return makeError( typeAnnotationComparisonError( 'Object types do not match.', - // $FlowFixMe[incompatible-type] objectTypeAnnotation(newerPropertyTypes), - // $FlowFixMe[incompatible-type] objectTypeAnnotation(olderPropertyTypes), ), ); } - return {status: 'properties', propertyLog: result}; + return { + status: 'properties', + propertyLog: result, + errorLog: typeAnnotationComparisonError( + 'Object has property changes', + objectTypeAnnotation(newerPropertyTypes), + objectTypeAnnotation(olderPropertyTypes), + ), + }; } function objectTypeAnnotation( @@ -856,7 +872,15 @@ export function compareEnumDeclarationWithMembers( ); } - return {status: 'members', memberLog: result}; + return { + status: 'members', + memberLog: result, + errorLog: typeAnnotationComparisonError( + 'Enum has member changes', + newerDeclaration, + olderDeclaration, + ), + }; } function compareNullableChange( @@ -895,6 +919,11 @@ function compareNullableChange( newType: newerAnnotation, oldType: olderAnnotation, }, + errorLog: typeAnnotationComparisonError( + 'Nullable type has changes', + newerAnnotation, + olderAnnotation, + ), }; } const interiorLog = compareTypeAnnotation(newVoidRemoved, oldVoidRemoved); @@ -917,6 +946,11 @@ function compareNullableChange( newType: newerAnnotation, oldType: olderAnnotation, }, + errorLog: typeAnnotationComparisonError( + 'Nullable type has changes', + newerAnnotation, + olderAnnotation, + ), }; default: return { @@ -928,6 +962,12 @@ function compareNullableChange( newType: newerAnnotation, oldType: olderAnnotation, }, + errorLog: typeAnnotationComparisonError( + 'Nullable type has changes', + newerAnnotation, + olderAnnotation, + interiorLog.errorLog, + ), }; } } @@ -977,7 +1017,15 @@ export function compareUnionTypes( ); } - return {status: 'unionMembers', memberLog: result}; + return { + status: 'unionMembers', + memberLog: result, + errorLog: typeAnnotationComparisonError( + 'Union has member changes', + newerType, + olderType, + ), + }; } export function comparePromiseTypes( @@ -1128,6 +1176,11 @@ export function compareStringLiteralUnionTypes( return { status: 'positionalTypeChange', changeLog, + errorLog: typeAnnotationComparisonError( + 'String literal union has member changes', + newerType, + olderType, + ), }; case 'matching': return {status: 'matching'}; @@ -1206,7 +1259,15 @@ export function compareFunctionTypes( if (isFunctionLogEmpty(functionChanges)) { return {status: 'matching'}; } - return {status: 'functionChange', functionChangeLog: functionChanges}; + return { + status: 'functionChange', + functionChangeLog: functionChanges, + errorLog: typeAnnotationComparisonError( + 'Function has parameter or return type changes', + newerType, + olderType, + ), + }; } type ArrayComparisonResult = diff --git a/packages/react-native-compatibility-check/src/VersionDiffing.js b/packages/react-native-compatibility-check/src/VersionDiffing.js index 5a3405eabf7..e7f3ad43cfb 100644 --- a/packages/react-native-compatibility-check/src/VersionDiffing.js +++ b/packages/react-native-compatibility-check/src/VersionDiffing.js @@ -15,6 +15,7 @@ import type { NullableComparisonResult, PositionalComparisonResult, PropertiesComparisonResult, + TypeComparisonError, UnionMembersComparisonResult, } from './ComparisonResult'; import type { @@ -54,25 +55,50 @@ type checkerType = ( memberChange: ?MembersComparisonResult, unionMemberChange: ?UnionMembersComparisonResult, typeName: string, + pathContext: ?TypeComparisonError, ) => Array; +// Helper to wrap an error with path context (property, parameter, element, etc.) +function wrapErrorWithPathContext( + error: ErrorStore, + message: string, + pathContext: ?TypeComparisonError, + propertyName?: ?string, +): ErrorStore { + const wrappedError: TypeComparisonError = + pathContext?.type === 'TypeAnnotationComparisonError' || + pathContext?.type === 'TypeInformationComparisonError' + ? typeAnnotationComparisonError( + message, + pathContext.newerAnnotation, + pathContext.olderAnnotation, + error.errorInformation, + ) + : error.errorInformation; + + if (propertyName != null) { + return { + ...error, + errorInformation: propertyComparisonError( + 'Object contained a property with a type mismatch', + [{property: propertyName, fault: wrappedError}], + ), + }; + } + + return { + ...error, + errorInformation: wrappedError, + }; +} + function nestedPropertiesCheck( typeName: string, result: ComparisonResult, check: checkerType, inverseCheck: checkerType, + pathContext?: ?TypeComparisonError, ): Array { - const nestedMap = - (mid: string, end: string) => - /* $FlowFixMe[missing-local-annot] The type annotation(s) required by - * Flow's LTI update could not be added via codemod */ - ([propertyName, comparisonResult]) => - nestedPropertiesCheck( - typeName + mid + propertyName + end, - comparisonResult, - check, - inverseCheck, - ); switch (result.status) { case 'error': case 'matching': @@ -88,36 +114,65 @@ function nestedPropertiesCheck( null, null, typeName, + pathContext ?? null, ); if (result.propertyLog.nestedPropertyChanges) { - finalResult = combine( - finalResult, - result.propertyLog.nestedPropertyChanges.map(nestedMap('.', '')), + const nestedErrors = result.propertyLog.nestedPropertyChanges.flatMap( + ([propertyName, nestedResult]) => { + // Recurse with the same typeName (don't extend it) + const errors = nestedPropertiesCheck( + typeName, + nestedResult, + check, + inverseCheck, + nestedResult.errorLog ?? null, + ); + // Wrap each error with the property path context + return errors.map(error => + wrapErrorWithPathContext( + error, + 'has conflicting type changes', + nestedResult.errorLog, + propertyName, + ), + ); + }, ); + finalResult = finalResult.concat(nestedErrors); } if (result.propertyLog.madeOptional) { const furtherNestedProps = result.propertyLog.madeOptional.filter( optionalProp => optionalProp.furtherChanges, ); if (furtherNestedProps && furtherNestedProps.length > 0) { - const localNestedMap = nestedMap('.', ''); - const mappedProps = furtherNestedProps.map(optionalProp => { + const nestedErrors = furtherNestedProps.flatMap(optionalProp => { if (optionalProp.furtherChanges) { - return localNestedMap([ - optionalProp.property, + const errors = nestedPropertiesCheck( + typeName, optionalProp.furtherChanges, - ]); + check, + inverseCheck, + optionalProp.furtherChanges.errorLog ?? null, + ); + return errors.map(error => + wrapErrorWithPathContext( + error, + 'has conflicting type changes', + optionalProp.furtherChanges?.errorLog, + optionalProp.property, + ), + ); } throw new Error('Internal error, filter failed'); }); - finalResult = combine(finalResult, mappedProps); + finalResult = finalResult.concat(nestedErrors); } } return finalResult; case 'members': - return check(null, null, null, result.memberLog, null, typeName); + return check(null, null, null, result.memberLog, null, typeName, null); case 'unionMembers': - return check(null, null, null, null, result.memberLog, typeName); + return check(null, null, null, null, result.memberLog, typeName, null); case 'functionChange': let returnTypeResult: Array = []; if (result.functionChangeLog.returnType) { @@ -126,21 +181,32 @@ function nestedPropertiesCheck( result.functionChangeLog.returnType, check, inverseCheck, + result.functionChangeLog.returnType.errorLog ?? null, ); } if (result.functionChangeLog.parameterTypes) { - return combine( - returnTypeResult, - result.functionChangeLog.parameterTypes.nestedChanges.map( - ([_oldParameterNumber, newParameterNumber, comparisonResult]) => - nestedPropertiesCheck( - typeName + ' parameter ' + newParameterNumber, - comparisonResult, + const parameterErrors = + result.functionChangeLog.parameterTypes.nestedChanges.flatMap( + ([_oldParameterNumber, newParameterNumber, nestedResult]) => { + // Recurse with the same typeName (don't extend it) + const errors = nestedPropertiesCheck( + typeName, + nestedResult, inverseCheck, check, - ), - ), - ); + nestedResult.errorLog ?? null, + ); + // Wrap each error with the parameter path context + return errors.map(error => + wrapErrorWithPathContext( + error, + `Parameter at index ${newParameterNumber} did not match`, + nestedResult.errorLog, + ), + ); + }, + ); + return returnTypeResult.concat(parameterErrors); } return returnTypeResult; case 'positionalTypeChange': @@ -152,16 +218,27 @@ function nestedPropertiesCheck( null, null, typeName, + null, ); - return combine( - currentPositionalCheck, - changeLog.nestedChanges.map(([_oldIndex, newIndex, nestedChange]) => - nestedMap( - ' element ', - ' of ' + changeLog.typeKind, - )([newIndex.toString(), nestedChange]), - ), + const positionalNestedErrors = changeLog.nestedChanges.flatMap( + ([_oldIndex, newIndex, nestedResult]) => { + const errors = nestedPropertiesCheck( + typeName, + nestedResult, + check, + inverseCheck, + nestedResult.errorLog ?? null, + ); + return errors.map(error => + wrapErrorWithPathContext( + error, + `Element ${newIndex} of ${changeLog.typeKind} did not match`, + nestedResult.errorLog, + ), + ); + }, ); + return currentPositionalCheck.concat(positionalNestedErrors); case 'nullableChange': const currentCheck = check( null, @@ -170,6 +247,7 @@ function nestedPropertiesCheck( null, null, typeName, + null, ); if (result.nullableLog.interiorLog) { const interiorLog = result.nullableLog.interiorLog; @@ -180,9 +258,15 @@ function nestedPropertiesCheck( case 'functionChange': case 'positionalTypeChange': case 'nullableChange': - return combine(currentCheck, [ - nestedPropertiesCheck(typeName, interiorLog, check, inverseCheck), - ]); + return currentCheck.concat( + nestedPropertiesCheck( + typeName, + interiorLog, + check, + inverseCheck, + interiorLog.errorLog ?? null, + ), + ); default: throw new Error( 'Internal error: nested with error or skipped status', @@ -201,6 +285,7 @@ function checkOptionalityAndSetError( properties: ReadonlyArray>, msg: string, errorCode: ErrorCode, + pathContext: ?TypeComparisonError, ): Array { const requiredProperties = properties.filter( objectTypeProperty => !objectTypeProperty.optional, @@ -215,6 +300,7 @@ function checkOptionalityAndSetError( requiredProperties.map(property => ({ property: property.name, })), + pathContext ?? undefined, ), }, ]; @@ -232,6 +318,7 @@ function checkForUnsafeRemovedProperties( _memberChange: ?MembersComparisonResult, _unionMemberChange: ?UnionMembersComparisonResult, typeName: string, + pathContext: ?TypeComparisonError, ): Array { if (propertyChange && propertyChange.missingProperties) { return checkOptionalityAndSetError( @@ -239,6 +326,7 @@ function checkForUnsafeRemovedProperties( propertyChange.missingProperties, removedPropertiesMessage, 'removedProps', + pathContext, ); } return []; @@ -253,6 +341,7 @@ function checkForUnsafeAddedProperties( _memberChange: ?MembersComparisonResult, _unionMemberChange: ?UnionMembersComparisonResult, typeName: string, + pathContext: ?TypeComparisonError, ): Array { if (propertyChange && propertyChange.addedProperties) { return checkOptionalityAndSetError( @@ -260,6 +349,7 @@ function checkForUnsafeAddedProperties( propertyChange.addedProperties, addedPropertiesMessage, 'addedProps', + pathContext, ); } return []; @@ -274,6 +364,7 @@ function checkForUnSafeMadeStrictProperties( _memberChange: ?MembersComparisonResult, _unionMemberChange: ?UnionMembersComparisonResult, typeName: string, + pathContext: ?TypeComparisonError, ): Array { if ( propertyChange && @@ -285,6 +376,7 @@ function checkForUnSafeMadeStrictProperties( propertyChange.madeStrict.map(property => ({ property: property.property, })), + pathContext ?? undefined, ); return [ { @@ -306,6 +398,7 @@ function checkForUnSafeMadeOptionalProperties( _memberChange: ?MembersComparisonResult, _unionMemberChange: ?UnionMembersComparisonResult, typeName: string, + pathContext: ?TypeComparisonError, ): Array { if ( propertyChange && @@ -317,6 +410,7 @@ function checkForUnSafeMadeOptionalProperties( propertyChange.madeOptional.map(property => ({ property: property.property, })), + pathContext ?? undefined, ); return [ { @@ -338,6 +432,7 @@ function checkForUnsafeRemovedEnumItems( memberChange: ?MembersComparisonResult, _unionMemberChange: ?UnionMembersComparisonResult, typeName: string, + pathContext: ?TypeComparisonError, ): Array { if (memberChange?.missingMembers && memberChange?.missingMembers.length > 0) { return [ @@ -349,6 +444,7 @@ function checkForUnsafeRemovedEnumItems( memberChange.missingMembers.map(member => ({ member: member.name, })), + pathContext ?? undefined, ), }, ]; @@ -366,6 +462,7 @@ function checkForUnsafeAddedEnumItems( memberChange: ?MembersComparisonResult, _unionMemberChange: ?UnionMembersComparisonResult, typeName: string, + pathContext: ?TypeComparisonError, ): Array { if (memberChange?.addedMembers && memberChange?.addedMembers.length > 0) { return [ @@ -377,6 +474,7 @@ function checkForUnsafeAddedEnumItems( memberChange.addedMembers.map(member => ({ member: member.name, })), + pathContext ?? undefined, ), }, ]; @@ -427,6 +525,7 @@ function checkForUnsafeRemovedUnionItems( memberChange: ?MembersComparisonResult, unionMemberChange: ?UnionMembersComparisonResult, typeName: string, + pathContext: ?TypeComparisonError, ): Array { if ( unionMemberChange?.missingMembers && @@ -441,6 +540,7 @@ function checkForUnsafeRemovedUnionItems( unionMemberChange.missingMembers.map(member => ({ member: getMemberLabel(member), })), + pathContext ?? undefined, ), }, ]; @@ -458,6 +558,7 @@ function checkForUnsafeAddedUnionItems( memberChange: ?MembersComparisonResult, unionMemberChange: ?UnionMembersComparisonResult, typeName: string, + pathContext: ?TypeComparisonError, ): Array { if ( unionMemberChange?.addedMembers && @@ -472,6 +573,7 @@ function checkForUnsafeAddedUnionItems( unionMemberChange.addedMembers.map(member => ({ member: getMemberLabel(member), })), + pathContext ?? undefined, ), }, ]; @@ -489,6 +591,7 @@ function checkForUnsafeRemovedIntersectionItems( _memberChange: ?MembersComparisonResult, _unionMemberChange: ?UnionMembersComparisonResult, typeName: string, + pathContext: ?TypeComparisonError, ): Array { if ( positionalChange && @@ -503,6 +606,7 @@ function checkForUnsafeRemovedIntersectionItems( errorInformation: positionalComparisonError( removedIntersectionMessage, positionalChange.removedElements, + pathContext ?? undefined, ), }, ]; @@ -519,6 +623,7 @@ function checkForUnsafeAddedIntersectionItems( _memberChange: ?MembersComparisonResult, _unionMemberChange: ?UnionMembersComparisonResult, typeName: string, + pathContext: ?TypeComparisonError, ): Array { if ( positionalChange && @@ -533,6 +638,7 @@ function checkForUnsafeAddedIntersectionItems( errorInformation: positionalComparisonError( addedIntersectionMessage, positionalChange.addedElements, + pathContext ?? undefined, ), }, ]; @@ -551,6 +657,7 @@ function checkForUnsafeNullableToNativeChange( _memberChange: ?MembersComparisonResult, _unionMemberChange: ?UnionMembersComparisonResult, typeName: string, + pathContext: ?TypeComparisonError, ): Array { if ( nullableChange && @@ -568,6 +675,7 @@ function checkForUnsafeNullableToNativeChange( : typeNullableChangeMessage, nullableChange.newType, nullableChange.oldType, + pathContext ?? undefined, ), }, ]; @@ -586,6 +694,7 @@ function checkForUnsafeNullableFromNativeChange( _memberChange: ?MembersComparisonResult, _unionMemberChange: ?UnionMembersComparisonResult, typeName: string, + pathContext: ?TypeComparisonError, ): Array { if ( nullableChange && @@ -603,6 +712,7 @@ function checkForUnsafeNullableFromNativeChange( : typeNonNullableChangeMessage, nullableChange.newType, nullableChange.oldType, + pathContext ?? undefined, ), }, ]; @@ -618,6 +728,7 @@ function chainPropertiesChecks(checks: Array): checkerType { memberChange: ?MembersComparisonResult, unionMemberChange: ?UnionMembersComparisonResult, typeName: string, + pathContext: ?TypeComparisonError, ) => checks.reduce( (errorStore, checker) => @@ -629,58 +740,54 @@ function chainPropertiesChecks(checks: Array): checkerType { memberChange, unionMemberChange, typeName, + pathContext, ), ), [], ); } -function combine( - singleton: Array, - arrayOf: Array>, -) { - if (arrayOf.length > 0) { - return arrayOf.reduce( - (finalErrorArray, current) => finalErrorArray.concat(current), - singleton, - ); - } - return singleton; -} - function compareFunctionTypesInContext( typeName: string, functionLog: FunctionComparisonResult, + functionErrorLog: ?TypeComparisonError, check: checkerType, inversecheck: checkerType, result: Array, ) { if (functionLog.returnType) { - result = combine(result, [ + result = result.concat( nestedPropertiesCheck( typeName, functionLog.returnType, check, inversecheck, ), - ]); + ); } if ( functionLog.parameterTypes && functionLog.parameterTypes.nestedChanges.length > 0 ) { - result = combine( - result, - functionLog.parameterTypes.nestedChanges.map( - ([_oldPropertyNum, newPropertyNum, comparisonResult]) => - nestedPropertiesCheck( - typeName + ' parameter ' + newPropertyNum, - comparisonResult, - inversecheck, - check, + const parameterErrors = functionLog.parameterTypes.nestedChanges.flatMap( + ([_oldPropertyNum, newPropertyNum, nestedResult]) => { + const errors = nestedPropertiesCheck( + typeName, + nestedResult, + inversecheck, + check, + ); + // Wrap each error with the parameter path context + return errors.map(error => + wrapErrorWithPathContext( + error, + `Parameter at index ${newPropertyNum} did not match`, + nestedResult.errorLog, ), - ), + ); + }, ); + result = result.concat(parameterErrors); } return result; } @@ -756,6 +863,7 @@ export function assessComparisonResult( memberChange, null, typeName, + difference.errorLog, ); const fromNativeErrorResult = checksForTypesFlowingFromNative( @@ -765,6 +873,7 @@ export function assessComparisonResult( memberChange, null, typeName, + difference.errorLog, ); switch (oldDirection) { @@ -800,6 +909,7 @@ export function assessComparisonResult( null, memberChange, typeName, + difference.errorLog, ); const fromNativeErrorResult = checksForTypesFlowingFromNative( @@ -809,6 +919,7 @@ export function assessComparisonResult( null, memberChange, typeName, + difference.errorLog, ); switch (oldDirection) { @@ -890,6 +1001,7 @@ export function assessComparisonResult( propertyErrors = compareFunctionTypesInContext( typeName, functionLog, + difference.errorLog, checksForTypesFlowingToNative, checksForTypesFlowingFromNative, propertyErrors, @@ -899,6 +1011,7 @@ export function assessComparisonResult( propertyErrors = compareFunctionTypesInContext( typeName, functionLog, + difference.errorLog, checksForTypesFlowingFromNative, checksForTypesFlowingToNative, propertyErrors, @@ -908,6 +1021,7 @@ export function assessComparisonResult( propertyErrors = compareFunctionTypesInContext( typeName, functionLog, + difference.errorLog, checksForTypesFlowingToNative, checksForTypesFlowingFromNative, propertyErrors, @@ -915,6 +1029,7 @@ export function assessComparisonResult( propertyErrors = compareFunctionTypesInContext( typeName, functionLog, + difference.errorLog, checksForTypesFlowingFromNative, checksForTypesFlowingToNative, propertyErrors, @@ -943,18 +1058,26 @@ export function assessComparisonResult( null, null, typeName, + null, ); - const toNativeResult = combine( - toNativeBase, - changes.map(([_oldIndex, newIndex, comparisonResult]) => - nestedPropertiesCheck( - `${typeName} element ${newIndex} of ${changeLog.typeKind}`, - comparisonResult, + const toNativeNestedErrors = changes.flatMap( + ([_oldIndex, newIndex, nestedResult]) => { + const errors = nestedPropertiesCheck( + typeName, + nestedResult, checksForTypesFlowingToNative, checksForTypesFlowingFromNative, - ), - ), + ); + return errors.map(error => + wrapErrorWithPathContext( + error, + `Element ${newIndex} of ${changeLog.typeKind} did not match`, + nestedResult.errorLog, + ), + ); + }, ); + const toNativeResult = toNativeBase.concat(toNativeNestedErrors); const fromNativeBase = checksForTypesFlowingFromNative( null, changeLog, @@ -962,17 +1085,27 @@ export function assessComparisonResult( null, null, typeName, + null, ); - const fromNativeResult = combine( - fromNativeBase, - changes.map(([_oldIndex, newIndex, comparisonResult]) => - nestedPropertiesCheck( - `${typeName} element ${newIndex} of ${changeLog.typeKind}`, - comparisonResult, + const fromNativeNestedErrors = changes.flatMap( + ([_oldIndex, newIndex, nestedResult]) => { + const errors = nestedPropertiesCheck( + typeName, + nestedResult, checksForTypesFlowingFromNative, checksForTypesFlowingToNative, - ), - ), + ); + return errors.map(error => + wrapErrorWithPathContext( + error, + `Element ${newIndex} of ${changeLog.typeKind} did not match`, + nestedResult.errorLog, + ), + ); + }, + ); + const fromNativeResult = fromNativeBase.concat( + fromNativeNestedErrors, ); switch (oldDirection) { case 'toNative': @@ -1003,6 +1136,7 @@ export function assessComparisonResult( null, null, typeName, + difference.errorLog, ).forEach(error => incompatibleChanges.add(error)); break; case 'fromNative': @@ -1013,6 +1147,7 @@ export function assessComparisonResult( null, null, typeName, + difference.errorLog, ).forEach(error => incompatibleChanges.add(error)); break; case 'both': @@ -1020,6 +1155,7 @@ export function assessComparisonResult( 'Type may not change nullability, due to flowing to and from native', newType, oldType, + difference.errorLog, ); incompatibleChanges.add({ typeName, diff --git a/packages/react-native-compatibility-check/src/__tests__/TypeDiffing-test.js b/packages/react-native-compatibility-check/src/__tests__/TypeDiffing-test.js index 96e98f108b6..0dbda9c3d50 100644 --- a/packages/react-native-compatibility-check/src/__tests__/TypeDiffing-test.js +++ b/packages/react-native-compatibility-check/src/__tests__/TypeDiffing-test.js @@ -242,17 +242,22 @@ describe('components', () => { {}, {}, ), - ).toEqual({ - status: 'unionMembers', - memberLog: { - addedMembers: [ - { - type: 'StringLiteralTypeAnnotation', - value: 'baz', - }, - ], - }, - }); + ).toEqual( + expect.objectContaining({ + status: 'unionMembers', + memberLog: { + addedMembers: [ + { + type: 'StringLiteralTypeAnnotation', + value: 'baz', + }, + ], + }, + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), + }), + ); }); it('array of an object to an extra key not compatible', () => { @@ -515,6 +520,9 @@ describe('compareTypes objects', () => { propertyLog: expect.objectContaining({ addedProperties: expect.any(Object), }), + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), }), ); }); @@ -533,6 +541,9 @@ describe('compareTypes objects', () => { propertyLog: expect.objectContaining({ missingProperties: expect.any(Object), }), + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), }), ); }); @@ -610,6 +621,9 @@ describe('compareTypes objects', () => { propertyLog: expect.objectContaining({ madeStrict: expect.any(Object), }), + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), }), ); }); @@ -628,6 +642,9 @@ describe('compareTypes objects', () => { propertyLog: expect.objectContaining({ madeOptional: expect.any(Object), }), + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), }), ); }); @@ -646,10 +663,18 @@ describe('compareTypes objects', () => { propertyLog: expect.objectContaining({ madeStrict: expect.objectContaining({ '0': expect.objectContaining({ - furtherChange: expect.objectContaining({status: 'properties'}), + furtherChange: expect.objectContaining({ + status: 'properties', + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), + }), }), }), }), + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), }), ); }); @@ -668,10 +693,18 @@ describe('compareTypes objects', () => { propertyLog: expect.objectContaining({ madeOptional: expect.objectContaining({ '0': expect.objectContaining({ - furtherChange: expect.objectContaining({status: 'properties'}), + furtherChange: expect.objectContaining({ + status: 'properties', + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), + }), }), }), }), + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), }), ); }); @@ -1015,6 +1048,9 @@ describe('compareTypes unions', () => { memberLog: expect.objectContaining({ missingMembers: expect.any(Array), }), + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), }), ); }); @@ -1162,6 +1198,7 @@ describe('compareTypes on generic types', () => { propertyLog: expect.objectContaining({ missingProperties: expect.any(Object), }), + errorLog: expect.any(Object), }), ); }); @@ -1198,17 +1235,22 @@ describe('compareTypes on string literal unions', () => { nativeTypeDiffingTypesAliases, nativeTypeDiffingTypesAliases, ), - ).toEqual({ - status: 'unionMembers', - memberLog: { - missingMembers: [ - { - type: 'StringLiteralTypeAnnotation', - value: 'd', - }, - ], - }, - }); + ).toEqual( + expect.objectContaining({ + status: 'unionMembers', + memberLog: { + missingMembers: [ + { + type: 'StringLiteralTypeAnnotation', + value: 'd', + }, + ], + }, + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), + }), + ); }); it('fails on unions with differing elements', () => { @@ -1219,23 +1261,28 @@ describe('compareTypes on string literal unions', () => { nativeTypeDiffingTypesAliases, nativeTypeDiffingTypesAliases, ), - ).toEqual({ - status: 'unionMembers', - memberLog: { - addedMembers: [ - { - type: 'StringLiteralTypeAnnotation', - value: 'b', - }, - ], - missingMembers: [ - { - type: 'StringLiteralTypeAnnotation', - value: 'x', - }, - ], - }, - }); + ).toEqual( + expect.objectContaining({ + status: 'unionMembers', + memberLog: { + addedMembers: [ + { + type: 'StringLiteralTypeAnnotation', + value: 'b', + }, + ], + missingMembers: [ + { + type: 'StringLiteralTypeAnnotation', + value: 'x', + }, + ], + }, + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), + }), + ); }); }); @@ -1266,6 +1313,9 @@ describe('compareTypes on nullables', () => { optionsReduced: true, interiorLog: expect.objectContaining({status: 'matching'}), }), + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), }), ); }); @@ -1285,6 +1335,9 @@ describe('compareTypes on nullables', () => { optionsReduced: false, interiorLog: expect.objectContaining({status: 'matching'}), }), + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), }), ); }); @@ -1302,7 +1355,15 @@ describe('compareTypes on nullables', () => { status: 'nullableChange', nullableLog: expect.objectContaining({ optionsReduced: true, - interiorLog: expect.objectContaining({status: 'properties'}), + interiorLog: expect.objectContaining({ + status: 'properties', + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), + }), + }), + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', }), }), ); @@ -1386,6 +1447,9 @@ describe('compareTypes on enums', () => { }), ]), }), + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), }), ); }); @@ -1413,6 +1477,9 @@ describe('compareTypes on enums', () => { }), ]), }), + errorLog: expect.objectContaining({ + type: 'TypeAnnotationComparisonError', + }), }), ); }); diff --git a/packages/react-native-compatibility-check/src/__tests__/VersionDiffing-test.js b/packages/react-native-compatibility-check/src/__tests__/VersionDiffing-test.js index 4bd3775c964..37988dd1e03 100644 --- a/packages/react-native-compatibility-check/src/__tests__/VersionDiffing-test.js +++ b/packages/react-native-compatibility-check/src/__tests__/VersionDiffing-test.js @@ -294,13 +294,24 @@ describe('buildSchemaDiff', () => { incompatibleSpecs: expect.arrayContaining([ expect.objectContaining({ changeInformation: expect.objectContaining({ - incompatibleChanges: expect.objectContaining({ - '0': expect.objectContaining({ + incompatibleChanges: expect.arrayContaining([ + expect.objectContaining({ + errorCode: 'removedProps', errorInformation: expect.objectContaining({ - message: removedPropertiesMessage, + mismatchedProperties: expect.arrayContaining([ + expect.objectContaining({ + fault: expect.objectContaining({ + previousError: expect.objectContaining({ + previousError: expect.objectContaining({ + message: removedPropertiesMessage, + }), + }), + }), + }), + ]), }), }), - }), + ]), }), }), ]), @@ -322,13 +333,24 @@ describe('buildSchemaDiff', () => { incompatibleSpecs: expect.arrayContaining([ expect.objectContaining({ changeInformation: expect.objectContaining({ - incompatibleChanges: expect.objectContaining({ - '0': expect.objectContaining({ + incompatibleChanges: expect.arrayContaining([ + expect.objectContaining({ + errorCode: 'optionalProps', errorInformation: expect.objectContaining({ - message: tooOptionalPropertiesMessage, + mismatchedProperties: expect.arrayContaining([ + expect.objectContaining({ + fault: expect.objectContaining({ + previousError: expect.objectContaining({ + previousError: expect.objectContaining({ + message: tooOptionalPropertiesMessage, + }), + }), + }), + }), + ]), }), }), - }), + ]), }), }), ]), @@ -353,13 +375,24 @@ describe('buildSchemaDiff', () => { incompatibleSpecs: expect.arrayContaining([ expect.objectContaining({ changeInformation: expect.objectContaining({ - incompatibleChanges: expect.objectContaining({ - '0': expect.objectContaining({ + incompatibleChanges: expect.arrayContaining([ + expect.objectContaining({ + errorCode: 'nullableOfNonNull', errorInformation: expect.objectContaining({ - message: typeNullableChangeMessage, + mismatchedProperties: expect.arrayContaining([ + expect.objectContaining({ + fault: expect.objectContaining({ + previousError: expect.objectContaining({ + previousError: expect.objectContaining({ + message: typeNullableChangeMessage, + }), + }), + }), + }), + ]), }), }), - }), + ]), }), }), ]), @@ -381,13 +414,24 @@ describe('buildSchemaDiff', () => { incompatibleSpecs: expect.arrayContaining([ expect.objectContaining({ changeInformation: expect.objectContaining({ - incompatibleChanges: expect.objectContaining({ - '0': expect.objectContaining({ + incompatibleChanges: expect.arrayContaining([ + expect.objectContaining({ + errorCode: 'optionalProps', errorInformation: expect.objectContaining({ - message: tooOptionalPropertiesMessage, + mismatchedProperties: expect.arrayContaining([ + expect.objectContaining({ + fault: expect.objectContaining({ + previousError: expect.objectContaining({ + previousError: expect.objectContaining({ + message: tooOptionalPropertiesMessage, + }), + }), + }), + }), + ]), }), }), - }), + ]), }), }), ]), @@ -449,7 +493,17 @@ describe('buildSchemaDiff', () => { expect.objectContaining({ errorCode: 'addedEnumCases', errorInformation: expect.objectContaining({ - message: addedEnumMessage, + mismatchedProperties: expect.arrayContaining([ + expect.objectContaining({ + fault: expect.objectContaining({ + previousError: expect.objectContaining({ + previousError: expect.objectContaining({ + message: addedEnumMessage, + }), + }), + }), + }), + ]), }), }), ]), @@ -577,7 +631,23 @@ describe('buildSchemaDiff', () => { expect.objectContaining({ errorCode: 'removedEnumCases', errorInformation: expect.objectContaining({ - message: removedEnumMessage, + mismatchedProperties: expect.arrayContaining([ + expect.objectContaining({ + fault: expect.objectContaining({ + previousError: expect.objectContaining({ + mismatchedProperties: expect.arrayContaining([ + expect.objectContaining({ + fault: expect.objectContaining({ + previousError: expect.objectContaining({ + message: removedEnumMessage, + }), + }), + }), + ]), + }), + }), + }), + ]), }), }), ]), @@ -1607,8 +1677,17 @@ describe('RN NativeModule getConstants type diffing', () => { changeInformation: expect.objectContaining({ incompatibleChanges: expect.arrayContaining([ expect.objectContaining({ + errorCode: 'addedProps', errorInformation: expect.objectContaining({ - message: addedPropertiesMessage, + mismatchedProperties: expect.arrayContaining([ + expect.objectContaining({ + fault: expect.objectContaining({ + previousError: expect.objectContaining({ + message: addedPropertiesMessage, + }), + }), + }), + ]), }), }), ]), diff --git a/packages/react-native-compatibility-check/src/__tests__/__snapshots__/ErrorFormatting-test.js.snap b/packages/react-native-compatibility-check/src/__tests__/__snapshots__/ErrorFormatting-test.js.snap index 2b41e74f3f7..4d753124705 100644 --- a/packages/react-native-compatibility-check/src/__tests__/__snapshots__/ErrorFormatting-test.js.snap +++ b/packages/react-native-compatibility-check/src/__tests__/__snapshots__/ErrorFormatting-test.js.snap @@ -195,8 +195,12 @@ Object { "incompatibleSpecs": Array [ Object { "errorCode": "addedUnionCases", - "message": "NativeComponent.sizes: Union added items, but native will not expect/support them - -- Member \\"huge\\"", + "message": "NativeComponent: Object contained a property with a type mismatch + -- sizes: has conflicting type changes + --new: (small | large | huge) + --old: (small | large) + Union added items, but native will not expect/support them + -- Member \\"huge\\"", }, ], }, @@ -236,8 +240,12 @@ Object { "incompatibleSpecs": Array [ Object { "errorCode": "addedUnionCases", - "message": "NativeComponent.size: Union added items, but native will not expect/support them - -- Member \\"huge\\"", + "message": "NativeComponent: Object contained a property with a type mismatch + -- size: has conflicting type changes + --new: (small | huge | large) + --old: (small | large) + Union added items, but native will not expect/support them + -- Member \\"huge\\"", }, ], }, @@ -344,8 +352,18 @@ Object { "incompatibleSpecs": Array [ Object { "errorCode": "optionalProps", - "message": "NativeModuleTest.exampleFunction parameter 0: Property made optional, but native requires it - -- a2", + "message": "NativeModuleTest: Object contained a property with a type mismatch + -- exampleFunction: has conflicting type changes + --new: (a: {a1: string, a2?: number, a3: string}, b: number)=>void + --old: (a: {a1: string, a2: number}, b: number)=>void + Parameter at index 0 did not match + --new: {a1: string, a2?: number, a3: string} + --old: {a1: string, a2: number} + Object has property changes + --new: {a1: string, a2?: number, a3: string} + --old: {a1: string, a2: number} + Property made optional, but native requires it + -- a2", }, ], }, @@ -391,8 +409,18 @@ Object { "incompatibleSpecs": Array [ Object { "errorCode": "removedProps", - "message": "NativeModuleTest.exampleFunction parameter 0: Object removed required properties expected by native - -- a3", + "message": "NativeModuleTest: Object contained a property with a type mismatch + -- exampleFunction: has conflicting type changes + --new: (a: {a1: string, a2: number}, b: number)=>void + --old: (a: {a1: string, a2: number, a3: string}, b: number)=>void + Parameter at index 0 did not match + --new: {a1: string, a2: number} + --old: {a1: string, a2: number, a3: string} + Object has property changes + --new: {a1: string, a2: number} + --old: {a1: string, a2: number, a3: string} + Object removed required properties expected by native + -- a3", }, ], }, @@ -409,8 +437,18 @@ Object { "incompatibleSpecs": Array [ Object { "errorCode": "optionalProps", - "message": "NativeModuleTest.exampleFunction parameter 0: Property made optional, but native requires it - -- a2", + "message": "NativeModuleTest: Object contained a property with a type mismatch + -- exampleFunction: has conflicting type changes + --new: (a: {a1: string, a2?: number, a3: string}, b: number)=>void + --old: (a: {a1: string, a2: number, a3: string}, b: number)=>void + Parameter at index 0 did not match + --new: {a1: string, a2?: number, a3: string} + --old: {a1: string, a2: number, a3: string} + Object has property changes + --new: {a1: string, a2?: number, a3: string} + --old: {a1: string, a2: number, a3: string} + Property made optional, but native requires it + -- a2", }, ], }, @@ -427,9 +465,16 @@ Object { "incompatibleSpecs": Array [ Object { "errorCode": "nullableOfNonNull", - "message": "NativeModuleTest.exampleFunction parameter 0: Type made nullable, but native requires it - --new: ?{a1: string, a2?: number, a3: string} - --old: {a1: string, a2?: number, a3: string}", + "message": "NativeModuleTest: Object contained a property with a type mismatch + -- exampleFunction: has conflicting type changes + --new: (a: ?{a1: string, a2?: number, a3: string}, b: number)=>void + --old: (a: {a1: string, a2?: number, a3: string}, b: number)=>void + Parameter at index 0 did not match + --new: ?{a1: string, a2?: number, a3: string} + --old: {a1: string, a2?: number, a3: string} + Type made nullable, but native requires it + --new: ?{a1: string, a2?: number, a3: string} + --old: {a1: string, a2?: number, a3: string}", }, ], }, @@ -472,8 +517,15 @@ Object { "incompatibleSpecs": Array [ Object { "errorCode": "addedEnumCases", - "message": "NativeModuleTest.exampleFunction parameter 0: Enum added items, but native will not expect/support them - -- Member D", + "message": "NativeModuleTest: Object contained a property with a type mismatch + -- exampleFunction: has conflicting type changes + --new: (a: Enum, b: number)=>void + --old: (a: Enum, b: number)=>void + Parameter at index 0 did not match + --new: Enum {A = 1, B = 2, C = 3, D = 4} + --old: Enum {A = 1, B = 2, C = 3} + Enum added items, but native will not expect/support them + -- Member D", }, ], }, @@ -490,8 +542,16 @@ Object { "incompatibleSpecs": Array [ Object { "errorCode": "removedEnumCases", - "message": "NativeModuleTest.getConstants.exampleConstant: Enum removed items, but native may still provide them - -- Member D", + "message": "NativeModuleTest: Object contained a property with a type mismatch + -- getConstants: has conflicting type changes + --new: ()=>{exampleConstant: Enum} + --old: ()=>{exampleConstant: Enum} + Object contained a property with a type mismatch + -- exampleConstant: has conflicting type changes + --new: Enum {A = 1, B = 2, C = 3} + --old: Enum {A = 1, B = 2, C = 3, D = 4} + Enum removed items, but native may still provide them + -- Member D", }, ], }, @@ -710,8 +770,15 @@ Object { "incompatibleSpecs": Array [ Object { "errorCode": "addedUnionCases", - "message": "NativeModuleTest.exampleFunction parameter 0: Union added items, but native will not expect/support them - -- Member \\"d\\"", + "message": "NativeModuleTest: Object contained a property with a type mismatch + -- exampleFunction: has conflicting type changes + --new: (a: (a | b | c | d), b: number)=>void + --old: (a: (a | b | c), b: number)=>void + Parameter at index 0 did not match + --new: (a | b | c | d) + --old: (a | b | c) + Union added items, but native will not expect/support them + -- Member \\"d\\"", }, ], }, @@ -754,9 +821,16 @@ Object { "incompatibleSpecs": Array [ Object { "errorCode": "addedUnionCases", - "message": "NativeModuleTest.exampleFunction parameter 0: Union added items, but native will not expect/support them - -- Member \\"b\\" - -- Member \\"c\\"", + "message": "NativeModuleTest: Object contained a property with a type mismatch + -- exampleFunction: has conflicting type changes + --new: (a: (a | b | c), b: number)=>void + --old: (a: (a | '0' | '1' | 'a long string'), b: number)=>void + Parameter at index 0 did not match + --new: (a | b | c) + --old: (a | '0' | '1' | 'a long string') + Union added items, but native will not expect/support them + -- Member \\"b\\" + -- Member \\"c\\"", }, ], }, @@ -773,8 +847,16 @@ Object { "incompatibleSpecs": Array [ Object { "errorCode": "removedUnionCases", - "message": "NativeModuleTest.getConstants.exampleConstant: Union removed items, but native may still provide them - -- Member \\"d\\"", + "message": "NativeModuleTest: Object contained a property with a type mismatch + -- getConstants: has conflicting type changes + --new: ()=>{exampleConstant: (a | b | c)} + --old: ()=>{exampleConstant: (a | b | c | d)} + Object contained a property with a type mismatch + -- exampleConstant: has conflicting type changes + --new: (a | b | c) + --old: (a | b | c | d) + Union removed items, but native may still provide them + -- Member \\"d\\"", }, ], }, @@ -817,8 +899,15 @@ Object { "incompatibleSpecs": Array [ Object { "errorCode": "addedUnionCases", - "message": "NativeModuleTest.exampleFunction parameter 0: Union added items, but native will not expect/support them - -- Member TypeAliasTypeAnnotation", + "message": "NativeModuleTest: Object contained a property with a type mismatch + -- exampleFunction: has conflicting type changes + --new: (a: Union, b: number)=>void + --old: (a: Union, b: number)=>void + Parameter at index 0 did not match + --new: Union + --old: Union + Union added items, but native will not expect/support them + -- Member TypeAliasTypeAnnotation", }, ], },