|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +import {type ViewConfig} from '../Renderer/shims/ReactNativeTypes'; |
| 12 | + |
| 13 | +type Difference = { |
| 14 | + path: $ReadOnlyArray<string>, |
| 15 | + type: 'missing' | 'unequal' | 'unexpected', |
| 16 | +}; |
| 17 | + |
| 18 | +/** |
| 19 | + * During the migration from native view configs to static view configs, this is |
| 20 | + * used to validate that the two are equivalent. |
| 21 | + */ |
| 22 | +export function validate( |
| 23 | + name: string, |
| 24 | + nativeViewConfig: ViewConfig, |
| 25 | + staticViewConfig: ViewConfig, |
| 26 | +): ?string { |
| 27 | + const differences = []; |
| 28 | + accumulateDifferences( |
| 29 | + differences, |
| 30 | + [], |
| 31 | + { |
| 32 | + bubblingEventTypes: nativeViewConfig.bubblingEventTypes, |
| 33 | + directEventTypes: nativeViewConfig.directEventTypes, |
| 34 | + uiViewClassName: nativeViewConfig.uiViewClassName, |
| 35 | + validAttributes: nativeViewConfig.validAttributes, |
| 36 | + }, |
| 37 | + { |
| 38 | + bubblingEventTypes: staticViewConfig.bubblingEventTypes, |
| 39 | + directEventTypes: staticViewConfig.directEventTypes, |
| 40 | + uiViewClassName: staticViewConfig.uiViewClassName, |
| 41 | + validAttributes: staticViewConfig.validAttributes, |
| 42 | + }, |
| 43 | + ); |
| 44 | + if (differences.length === 0) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + return [ |
| 48 | + `StaticViewConfigValidator: Invalid static view config for '${name}'.`, |
| 49 | + '', |
| 50 | + ...differences.map(({path, type}) => { |
| 51 | + switch (type) { |
| 52 | + case 'missing': |
| 53 | + return `- '${path.join('.')}' is missing.`; |
| 54 | + case 'unequal': |
| 55 | + return `- '${path.join('.')}' is the wrong value.`; |
| 56 | + case 'unexpected': |
| 57 | + return `- '${path.join('.')}' is present but not expected to be.`; |
| 58 | + } |
| 59 | + }), |
| 60 | + '', |
| 61 | + ].join('\n'); |
| 62 | +} |
| 63 | + |
| 64 | +function accumulateDifferences( |
| 65 | + differences: Array<Difference>, |
| 66 | + path: Array<string>, |
| 67 | + nativeObject: {...}, |
| 68 | + staticObject: {...}, |
| 69 | +): void { |
| 70 | + for (const nativeKey in nativeObject) { |
| 71 | + const nativeValue = nativeObject[nativeKey]; |
| 72 | + |
| 73 | + if (!staticObject.hasOwnProperty(nativeKey)) { |
| 74 | + differences.push({path: [...path, nativeKey], type: 'missing'}); |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + const staticValue = staticObject[nativeKey]; |
| 79 | + |
| 80 | + const nativeValueIfObject = ifObject(nativeValue); |
| 81 | + if (nativeValueIfObject != null) { |
| 82 | + const staticValueIfObject = ifObject(staticValue); |
| 83 | + if (staticValueIfObject != null) { |
| 84 | + path.push(nativeKey); |
| 85 | + accumulateDifferences( |
| 86 | + differences, |
| 87 | + path, |
| 88 | + nativeValueIfObject, |
| 89 | + staticValueIfObject, |
| 90 | + ); |
| 91 | + path.pop(); |
| 92 | + continue; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (nativeValue !== staticValue) { |
| 97 | + differences.push({path: [...path, nativeKey], type: 'unequal'}); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + for (const staticKey in staticObject) { |
| 102 | + if (!nativeObject.hasOwnProperty(staticKey)) { |
| 103 | + differences.push({path: [...path, staticKey], type: 'unexpected'}); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +function ifObject(value: mixed): ?{...} { |
| 109 | + return typeof value === 'object' && !Array.isArray(value) ? value : null; |
| 110 | +} |
0 commit comments