Skip to content

Commit

Permalink
fix: fix bug of needless undefined in oneOfType and flatten oneOfType…
Browse files Browse the repository at this point in the history
… with only one level
  • Loading branch information
akirakai committed Sep 6, 2022
1 parent 1f355c6 commit ff9c208
Show file tree
Hide file tree
Showing 3 changed files with 746 additions and 2,043 deletions.
16 changes: 15 additions & 1 deletion modules/material-parser/src/parse/transform.ts
Original file line number Diff line number Diff line change
@@ -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 { debug } from '../core';

Expand Down Expand Up @@ -38,6 +38,7 @@ export function transformType(itemType: any) {
case 'element':
case 'node':
case 'void':
case 'undefined':
break;
case 'func':
if (params) {
Expand Down Expand Up @@ -205,6 +206,7 @@ function combineOneOfValues(propType) {
const newValue = [];
let oneOfItem = null;
let firstBooleanIndex = -1;

propType.value.forEach((item) => {
if (item?.type === 'oneOf') {
if (!oneOfItem) {
Expand All @@ -228,7 +230,14 @@ function combineOneOfValues(propType) {
newValue.push(item);
}
});

let result = propType;

if (!result.isRequired && result.value.includes('undefined')) {
pull(result.value, 'undefined');
pull(newValue, 'undefined');
}

const oneOfItemLength = oneOfItem?.value?.length;
if (oneOfItemLength) {
newValue.push(oneOfItem);
Expand All @@ -247,6 +256,11 @@ function combineOneOfValues(propType) {
};
}
result.value = uniq(result.value);

if (result.type === 'oneOfType' && result.value.length === 1) {
result = result.value[0];
}

return result;
}

Expand Down
4 changes: 4 additions & 0 deletions modules/material-parser/src/parse/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ function getDocgenTypeHelper(
return makeResult({
name: 'any',
});
} else if (type.flags & TypeFlags.Undefined) {
return makeResult({
name: 'undefined',
});
} else if (type.flags & TypeFlags.Union && !isComplexType(type)) {
return makeResult({
name: 'union',
Expand Down
Loading

0 comments on commit ff9c208

Please sign in to comment.