fix: fix bug of needless undefined in oneOfType and flatten oneOfType with only one level

This commit is contained in:
akirakai 2022-09-06 16:51:17 +08:00
parent 1f355c6a58
commit ff9c208c05
3 changed files with 791 additions and 2088 deletions

View File

@ -1,4 +1,4 @@
import { omit, pick, isNil, uniq } from 'lodash'; import { omit, pick, isNil, uniq, pull } from 'lodash';
import { safeEval, isEvaluable } from '../utils'; import { safeEval, isEvaluable } from '../utils';
import { debug } from '../core'; import { debug } from '../core';
@ -38,6 +38,7 @@ export function transformType(itemType: any) {
case 'element': case 'element':
case 'node': case 'node':
case 'void': case 'void':
case 'undefined':
break; break;
case 'func': case 'func':
if (params) { if (params) {
@ -205,6 +206,7 @@ function combineOneOfValues(propType) {
const newValue = []; const newValue = [];
let oneOfItem = null; let oneOfItem = null;
let firstBooleanIndex = -1; let firstBooleanIndex = -1;
propType.value.forEach((item) => { propType.value.forEach((item) => {
if (item?.type === 'oneOf') { if (item?.type === 'oneOf') {
if (!oneOfItem) { if (!oneOfItem) {
@ -228,7 +230,14 @@ function combineOneOfValues(propType) {
newValue.push(item); newValue.push(item);
} }
}); });
let result = propType; let result = propType;
if (!result.isRequired && result.value.includes('undefined')) {
pull(result.value, 'undefined');
pull(newValue, 'undefined');
}
const oneOfItemLength = oneOfItem?.value?.length; const oneOfItemLength = oneOfItem?.value?.length;
if (oneOfItemLength) { if (oneOfItemLength) {
newValue.push(oneOfItem); newValue.push(oneOfItem);
@ -247,6 +256,11 @@ function combineOneOfValues(propType) {
}; };
} }
result.value = uniq(result.value); result.value = uniq(result.value);
if (result.type === 'oneOfType' && result.value.length === 1) {
result = result.value[0];
}
return result; return result;
} }

View File

@ -294,6 +294,10 @@ function getDocgenTypeHelper(
return makeResult({ return makeResult({
name: 'any', name: 'any',
}); });
} else if (type.flags & TypeFlags.Undefined) {
return makeResult({
name: 'undefined',
});
} else if (type.flags & TypeFlags.Union && !isComplexType(type)) { } else if (type.flags & TypeFlags.Union && !isComplexType(type)) {
return makeResult({ return makeResult({
name: 'union', name: 'union',

File diff suppressed because it is too large Load Diff