Skip to content

Commit 0e65f02

Browse files
liujupingJackLian
authored andcommitted
feat(utils): move checkPropTypes to utils module
1 parent cd67a8c commit 0e65f02

File tree

13 files changed

+305
-299
lines changed

13 files changed

+305
-299
lines changed

packages/renderer-core/src/renderer/base.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import classnames from 'classnames';
55
import { create as createDataSourceEngine } from '@alilc/lowcode-datasource-engine/interpret';
66
import { IPublicTypeNodeSchema, IPublicTypeNodeData, IPublicTypeJSONValue, IPublicTypeCompositeValue } from '@alilc/lowcode-types';
7-
import { isI18nData, isJSExpression, isJSFunction } from '@alilc/lowcode-utils';
7+
import { checkPropTypes, isI18nData, isJSExpression, isJSFunction } from '@alilc/lowcode-utils';
88
import adapter from '../adapter';
99
import divFactory from '../components/Div';
1010
import visualDomFactory from '../components/VisualDom';
@@ -21,7 +21,6 @@ import {
2121
isFileSchema,
2222
transformArrayToMap,
2323
transformStringToFunction,
24-
checkPropTypes,
2524
getI18n,
2625
getFileCssName,
2726
capitalizeFirstLetter,

packages/renderer-core/src/utils/common.ts

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@ import { isI18nData, isJSExpression } from '@alilc/lowcode-utils';
66
import { isEmpty } from 'lodash';
77
import IntlMessageFormat from 'intl-messageformat';
88
import pkg from '../../package.json';
9-
import * as ReactIs from 'react-is';
10-
import { default as ReactPropTypesSecret } from 'prop-types/lib/ReactPropTypesSecret';
11-
import { default as factoryWithTypeCheckers } from 'prop-types/factoryWithTypeCheckers';
129

1310
(window as any).sdkVersion = pkg.version;
1411

1512
export { pick, isEqualWith as deepEqual, cloneDeep as clone, isEmpty, throttle, debounce } from 'lodash';
1613

17-
const PropTypes2 = factoryWithTypeCheckers(ReactIs.isElement, true);
18-
1914
const EXPRESSION_TYPE = {
2015
JSEXPRESSION: 'JSExpression',
2116
JSFUNCTION: 'JSFunction',
@@ -183,77 +178,6 @@ export function transformArrayToMap(arr: any[], key: string, overwrite = true) {
183178
return res;
184179
}
185180

186-
export function isBasicType(propType: IPublicTypePropType): propType is IPublicTypeBasicType {
187-
if (!propType) {
188-
return false;
189-
}
190-
return typeof propType === 'string';
191-
}
192-
193-
export function isRequiredType(propType: IPublicTypePropType): propType is IPublicTypeRequiredType {
194-
if (!propType) {
195-
return false;
196-
}
197-
return typeof propType === 'object' && propType.type && ['array', 'bool', 'func', 'number', 'object', 'string', 'node', 'element', 'any'].includes(propType.type);
198-
}
199-
200-
export function transformPropTypesRuleToString(rule: IPublicTypePropType): string {
201-
if (!rule) {
202-
return 'PropTypes.any';
203-
}
204-
205-
if (typeof rule === 'string') {
206-
return `PropTypes.${rule}`;
207-
}
208-
209-
if (isRequiredType(rule)) {
210-
const { type, isRequired } = rule;
211-
return `PropTypes.${type}${isRequired ? '.isRequired' : ''}`;
212-
}
213-
214-
const { type, value } = rule;
215-
switch (type) {
216-
case 'oneOf':
217-
return `PropTypes.oneOf([${value.map((item: any) => `"${item}"`).join(',')}])`;
218-
case 'oneOfType':
219-
return `PropTypes.oneOfType([${value.map((item: any) => transformPropTypesRuleToString(item)).join(', ')}])`;
220-
case 'arrayOf':
221-
case 'objectOf':
222-
return `PropTypes.${type}(${transformPropTypesRuleToString(value)})`;
223-
case 'shape':
224-
case 'exact':
225-
return `PropTypes.${type}({${value.map((item: any) => `${item.name}: ${transformPropTypesRuleToString(item.propType)}`).join(',')}})`;
226-
}
227-
}
228-
229-
export function checkPropTypes(value: any, name: string, rule: any, componentName: string): boolean {
230-
let ruleFunction = rule;
231-
if (typeof rule === 'object') {
232-
ruleFunction = new Function(`"use strict"; const PropTypes = arguments[0]; return ${transformPropTypesRuleToString(rule)}`)(PropTypes2);
233-
}
234-
if (typeof rule === 'string') {
235-
ruleFunction = new Function(`"use strict"; const PropTypes = arguments[0]; return ${rule}`)(PropTypes2);
236-
}
237-
if (!ruleFunction || typeof ruleFunction !== 'function') {
238-
logger.warn('checkPropTypes should have a function type rule argument');
239-
return true;
240-
}
241-
const err = ruleFunction(
242-
{
243-
[name]: value,
244-
},
245-
name,
246-
componentName,
247-
'prop',
248-
null,
249-
ReactPropTypesSecret,
250-
);
251-
if (err) {
252-
logger.warn(err);
253-
}
254-
return !err;
255-
}
256-
257181
/**
258182
* transform string to a function
259183
* @param str function in string form

packages/renderer-core/tests/utils/common.test.ts

Lines changed: 0 additions & 219 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import factoryWithTypeCheckers from 'prop-types/factoryWithTypeCheckers';
21
import {
32
isSchema,
43
isFileSchema,
@@ -18,17 +17,9 @@ import {
1817
parseThisRequiredExpression,
1918
parseI18n,
2019
parseData,
21-
checkPropTypes,
22-
transformPropTypesRuleToString,
23-
isRequiredType,
24-
isBasicType,
2520
} from '../../src/utils/common';
2621
import logger from '../../src/utils/logger';
2722

28-
var ReactIs = require('react-is');
29-
30-
const PropTypes = factoryWithTypeCheckers(ReactIs.isElement, true);
31-
3223
describe('test isSchema', () => {
3324
it('should be false when empty value is passed', () => {
3425
expect(isSchema(null)).toBeFalsy();
@@ -470,213 +461,3 @@ describe('test parseData ', () => {
470461

471462
});
472463
});
473-
474-
describe('test isBasicType ', () => {
475-
it('should work', () => {
476-
expect(isBasicType(null)).toBeFalsy();
477-
expect(isBasicType(undefined)).toBeFalsy();
478-
expect(isBasicType({})).toBeFalsy();
479-
expect(isBasicType({ type: 'any other type' })).toBeFalsy();
480-
expect(isBasicType('string')).toBeTruthy();
481-
});
482-
});
483-
484-
describe('test isRequiredType', () => {
485-
it('should work', () => {
486-
expect(isRequiredType(null)).toBeFalsy();
487-
expect(isRequiredType(undefined)).toBeFalsy();
488-
expect(isRequiredType({})).toBeFalsy();
489-
expect(isRequiredType({ type: 'any other type' })).toBeFalsy();
490-
expect(isRequiredType('string')).toBeFalsy();
491-
expect(isRequiredType({ type: 'string' })).toBeTruthy();
492-
expect(isRequiredType({ type: 'string', isRequired: true })).toBeTruthy();
493-
});
494-
})
495-
496-
describe('checkPropTypes', () => {
497-
it('should validate correctly with valid prop type', () => {
498-
expect(checkPropTypes(123, 'age', PropTypes.number, 'TestComponent')).toBe(true);
499-
expect(checkPropTypes('123', 'age', PropTypes.string, 'TestComponent')).toBe(true);
500-
});
501-
502-
it('should log a warning and return false with invalid prop type', () => {
503-
expect(checkPropTypes(123, 'age', PropTypes.string, 'TestComponent')).toBe(false);
504-
expect(checkPropTypes('123', 'age', PropTypes.number, 'TestComponent')).toBe(false);
505-
});
506-
507-
it('should handle custom rule functions correctly', () => {
508-
const customRule = (props, propName) => {
509-
if (props[propName] !== 123) {
510-
return new Error('Invalid value');
511-
}
512-
};
513-
const result = checkPropTypes(123, 'customProp', customRule, 'TestComponent');
514-
expect(result).toBe(true);
515-
});
516-
517-
518-
it('should interpret and validate a rule given as a string', () => {
519-
const result = checkPropTypes(123, 'age', 'PropTypes.number', 'TestComponent');
520-
expect(result).toBe(true);
521-
});
522-
523-
it('should log a warning for invalid rule type', () => {
524-
const result = checkPropTypes(123, 'age', 123, 'TestComponent');
525-
expect(result).toBe(true);
526-
});
527-
528-
// oneOf
529-
it('should validate correctly with valid oneOf prop type', () => {
530-
const rule = {
531-
type: 'oneOf',
532-
value: ['News', 'Photos'],
533-
}
534-
expect(transformPropTypesRuleToString(rule)).toBe(`PropTypes.oneOf(["News","Photos"])`);
535-
expect(checkPropTypes('News', 'type', rule, 'TestComponent')).toBe(true);
536-
expect(checkPropTypes('Others', 'type', rule, 'TestComponent')).toBe(false);
537-
});
538-
539-
// oneOfType
540-
it('should validate correctly with valid oneOfType prop type', () => {
541-
const rule = {
542-
type: 'oneOfType',
543-
value: ['string', 'number', {
544-
type: 'array',
545-
isRequired: true,
546-
}],
547-
};
548-
expect(transformPropTypesRuleToString(rule)).toBe('PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.array.isRequired])');
549-
expect(checkPropTypes(['News', 'Photos'], 'type', rule, 'TestComponent')).toBe(true);
550-
expect(checkPropTypes('News', 'type', rule, 'TestComponent')).toBe(true);
551-
expect(checkPropTypes(123, 'type', rule, 'TestComponent')).toBe(true);
552-
expect(checkPropTypes({}, 'type', rule, 'TestComponent')).toBe(false);
553-
});
554-
555-
// arrayOf
556-
it('should validate correctly with valid arrayOf prop type', () => {
557-
const rule = {
558-
type: 'arrayOf',
559-
value: {
560-
type: 'string',
561-
isRequired: true,
562-
},
563-
};
564-
expect(transformPropTypesRuleToString(rule)).toBe('PropTypes.arrayOf(PropTypes.string.isRequired)');
565-
expect(checkPropTypes(['News', 'Photos'], 'type', rule, 'TestComponent')).toBe(true);
566-
expect(checkPropTypes(['News', 123], 'type', rule, 'TestComponent')).toBe(false);
567-
});
568-
569-
// objectOf
570-
it('should validate correctly with valid objectOf prop type', () => {
571-
const rule = {
572-
type: 'objectOf',
573-
value: {
574-
type: 'string',
575-
isRequired: true,
576-
},
577-
};
578-
expect(transformPropTypesRuleToString(rule)).toBe('PropTypes.objectOf(PropTypes.string.isRequired)');
579-
expect(checkPropTypes({ a: 'News', b: 'Photos' }, 'type', rule, 'TestComponent')).toBe(true);
580-
expect(checkPropTypes({ a: 'News', b: 123 }, 'type', rule, 'TestComponent')).toBe(false);
581-
});
582-
583-
// shape
584-
it('should validate correctly with valid shape prop type', () => {
585-
const rule = {
586-
type: 'shape',
587-
value: [
588-
{
589-
name: 'a',
590-
propType: {
591-
type: 'string',
592-
isRequired: true,
593-
},
594-
},
595-
{
596-
name: 'b',
597-
propType: {
598-
type: 'number',
599-
isRequired: true,
600-
},
601-
},
602-
],
603-
};
604-
expect(transformPropTypesRuleToString(rule)).toBe('PropTypes.shape({a: PropTypes.string.isRequired,b: PropTypes.number.isRequired})');
605-
expect(checkPropTypes({ a: 'News', b: 123 }, 'type', rule, 'TestComponent')).toBe(true);
606-
expect(checkPropTypes({ a: 'News', b: 'Photos' }, 'type', rule, 'TestComponent')).toBe(false);
607-
608-
// isRequired
609-
const rule2 = {
610-
type: 'shape',
611-
value: [
612-
{
613-
name: 'a',
614-
propType: {
615-
type: 'string',
616-
isRequired: true,
617-
},
618-
},
619-
{
620-
name: 'b',
621-
propType: {
622-
type: 'number',
623-
isRequired: false,
624-
},
625-
},
626-
],
627-
};
628-
expect(transformPropTypesRuleToString(rule2)).toBe('PropTypes.shape({a: PropTypes.string.isRequired,b: PropTypes.number})');
629-
expect(checkPropTypes({ a: 'News', b: 123 }, 'type', rule2, 'TestComponent')).toBe(true);
630-
expect(checkPropTypes({ b: 123 }, 'type', rule2, 'TestComponent')).toBe(false);
631-
});
632-
633-
// exact
634-
it('should validate correctly with valid exact prop type', () => {
635-
const rule = {
636-
type: 'exact',
637-
value: [
638-
{
639-
name: 'a',
640-
propType: {
641-
type: 'string',
642-
isRequired: true,
643-
},
644-
},
645-
{
646-
name: 'b',
647-
propType: {
648-
type: 'number',
649-
isRequired: true,
650-
},
651-
},
652-
],
653-
};
654-
expect(transformPropTypesRuleToString(rule)).toBe('PropTypes.exact({a: PropTypes.string.isRequired,b: PropTypes.number.isRequired})');
655-
expect(checkPropTypes({ a: 'News', b: 123 }, 'type', rule, 'TestComponent')).toBe(true);
656-
expect(checkPropTypes({ a: 'News', b: 'Photos' }, 'type', rule, 'TestComponent')).toBe(false);
657-
658-
// isRequired
659-
const rule2 = {
660-
type: 'exact',
661-
value: [
662-
{
663-
name: 'a',
664-
propType: {
665-
type: 'string',
666-
isRequired: true,
667-
},
668-
},
669-
{
670-
name: 'b',
671-
propType: {
672-
type: 'number',
673-
isRequired: false,
674-
},
675-
},
676-
],
677-
};
678-
expect(transformPropTypesRuleToString(rule2)).toBe('PropTypes.exact({a: PropTypes.string.isRequired,b: PropTypes.number})');
679-
expect(checkPropTypes({ a: 'News', b: 123 }, 'type', rule2, 'TestComponent')).toBe(true);
680-
expect(checkPropTypes({ b: 123 }, 'type', rule2, 'TestComponent')).toBe(false);
681-
});
682-
});

packages/utils/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"@alilc/lowcode-types": "1.3.1",
1818
"lodash": "^4.17.21",
1919
"mobx": "^6.3.0",
20+
"prop-types": "^15.8.1",
2021
"react": "^16"
2122
},
2223
"devDependencies": {

0 commit comments

Comments
 (0)