GeezerVison is showing github.com — feat(utils): move checkPropTypes to utils module · DavidAlphaFox/lowcode-engine@0e65f02
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/renderer-core/src/renderer/base.tsx +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | import classnames from 'classnames'; | 5 | 5 | import { create as createDataSourceEngine } from '@alilc/lowcode-datasource-engine/interpret'; | 6 | 6 | 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'; | 8 | 8 | import adapter from '../adapter'; | 9 | 9 | import divFactory from '../components/Div'; | 10 | 10 | import visualDomFactory from '../components/VisualDom'; | @@ -21,7 +21,6 @@ import { | 21 | 21 | isFileSchema, | 22 | 22 | transformArrayToMap, | 23 | 23 | transformStringToFunction, | 24 | | - checkPropTypes, | 25 | 24 | getI18n, | 26 | 25 | getFileCssName, | 27 | 26 | capitalizeFirstLetter, | |
Collapse file Copy file name to clipboardExpand all lines: packages/renderer-core/src/utils/common.ts -76Lines changed: 0 additions & 76 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,16 +6,11 @@ import { isI18nData, isJSExpression } from '@alilc/lowcode-utils'; | 6 | 6 | import { isEmpty } from 'lodash'; | 7 | 7 | import IntlMessageFormat from 'intl-messageformat'; | 8 | 8 | 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'; | 12 | 9 |
| 13 | 10 | (window as any).sdkVersion = pkg.version; | 14 | 11 |
| 15 | 12 | export { pick, isEqualWith as deepEqual, cloneDeep as clone, isEmpty, throttle, debounce } from 'lodash'; | 16 | 13 |
| 17 | | -const PropTypes2 = factoryWithTypeCheckers(ReactIs.isElement, true); | 18 | | - | 19 | 14 | const EXPRESSION_TYPE = { | 20 | 15 | JSEXPRESSION: 'JSExpression', | 21 | 16 | JSFUNCTION: 'JSFunction', | @@ -183,77 +178,6 @@ export function transformArrayToMap(arr: any[], key: string, overwrite = true) { | 183 | 178 | return res; | 184 | 179 | } | 185 | 180 |
| 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 | | - | 257 | 181 | /** | 258 | 182 | * transform string to a function | 259 | 183 | * @param str function in string form | |
Collapse file Copy file name to clipboardExpand all lines: packages/renderer-core/tests/utils/common.test.ts -219Lines changed: 0 additions & 219 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import factoryWithTypeCheckers from 'prop-types/factoryWithTypeCheckers'; | 2 | 1 | import { | 3 | 2 | isSchema, | 4 | 3 | isFileSchema, | @@ -18,17 +17,9 @@ import { | 18 | 17 | parseThisRequiredExpression, | 19 | 18 | parseI18n, | 20 | 19 | parseData, | 21 | | - checkPropTypes, | 22 | | - transformPropTypesRuleToString, | 23 | | - isRequiredType, | 24 | | - isBasicType, | 25 | 20 | } from '../../src/utils/common'; | 26 | 21 | import logger from '../../src/utils/logger'; | 27 | 22 |
| 28 | | -var ReactIs = require('react-is'); | 29 | | - | 30 | | -const PropTypes = factoryWithTypeCheckers(ReactIs.isElement, true); | 31 | | - | 32 | 23 | describe('test isSchema', () => { | 33 | 24 | it('should be false when empty value is passed', () => { | 34 | 25 | expect(isSchema(null)).toBeFalsy(); | @@ -470,213 +461,3 @@ describe('test parseData ', () => { | 470 | 461 |
| 471 | 462 | }); | 472 | 463 | }); | 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 | | -}); |
Collapse file Copy file name to clipboardExpand all lines: packages/utils/package.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 17 | 17 | "@alilc/lowcode-types": "1.3.1", | 18 | 18 | "lodash": "^4.17.21", | 19 | 19 | "mobx": "^6.3.0", | | 20 | + "prop-types": "^15.8.1", | 20 | 21 | "react": "^16" | 21 | 22 | }, | 22 | 23 | "devDependencies": { | |
|