Skip to content

Commit d02bb9a

Browse files
golopotljharb
authored andcommitted
[Refactor]: remove unused codes in util/propTypes
- remove useless handling of `instanceOf` and `oneOf` There is no special treatment of `Proptypes.{instanceOf, oneOf}` types so they might as well be handled as unrecognized types.
1 parent 4f3cd90 commit d02bb9a

File tree

2 files changed

+5
-39
lines changed

2 files changed

+5
-39
lines changed

lib/types.d.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ declare global {
2323
[k in string]: TypeDeclarationBuilder;
2424
};
2525

26-
type UnionTypeDefinitionChildren = unknown[];
2726
type UnionTypeDefinition = {
2827
type: 'union' | 'shape';
29-
children: UnionTypeDefinitionChildren | true;
28+
children: unknown[];
3029
};
3130
}

lib/util/propTypes.js

+4-37
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,9 @@ module.exports = function propTypesInstructions(context, components, utils) {
170170
/** @type {UnionTypeDefinition} */
171171
const unionTypeDefinition = {
172172
type: 'union',
173-
children: []
173+
children: annotation.types.map(type => buildTypeAnnotationDeclarationTypes(type, parentName, seen))
174174
};
175-
for (let i = 0, j = annotation.types.length; i < j; i++) {
176-
const type = buildTypeAnnotationDeclarationTypes(annotation.types[i], parentName, seen);
177-
// keep only complex type
178-
if (type.type) {
179-
if (type.children === true) {
180-
// every child is accepted for one type, abort type analysis
181-
unionTypeDefinition.children = true;
182-
return unionTypeDefinition;
183-
}
184-
}
185-
186-
/** @type {UnionTypeDefinitionChildren} */(unionTypeDefinition.children).push(type);
187-
}
188-
if (/** @type {UnionTypeDefinitionChildren} */(unionTypeDefinition.children).length === 0) {
175+
if (unionTypeDefinition.children.length === 0) {
189176
// no complex type found, simply accept everything
190177
return {};
191178
}
@@ -419,34 +406,14 @@ module.exports = function propTypesInstructions(context, components, utils) {
419406
/** @type {UnionTypeDefinition} */
420407
const unionTypeDefinition = {
421408
type: 'union',
422-
children: []
409+
children: argument.elements.map(element => buildReactDeclarationTypes(element, parentName))
423410
};
424-
for (let i = 0, j = argument.elements.length; i < j; i++) {
425-
const type = buildReactDeclarationTypes(argument.elements[i], parentName);
426-
// keep only complex type
427-
if (type.type) {
428-
if (type.children === true) {
429-
// every child is accepted for one type, abort type analysis
430-
unionTypeDefinition.children = true;
431-
return unionTypeDefinition;
432-
}
433-
}
434-
435-
/** @type {UnionTypeDefinitionChildren} */(unionTypeDefinition.children).push(type);
436-
}
437-
if (/** @type {UnionTypeDefinitionChildren} */(unionTypeDefinition.children).length === 0) {
411+
if (unionTypeDefinition.children.length === 0) {
438412
// no complex type found, simply accept everything
439413
return {};
440414
}
441415
return unionTypeDefinition;
442416
}
443-
case 'instanceOf':
444-
return {
445-
type: 'instance',
446-
// Accept all children because we can't know what type they are
447-
children: true
448-
};
449-
case 'oneOf':
450417
default:
451418
return {};
452419
}

0 commit comments

Comments
 (0)