Skip to content

Commit 8ef2152

Browse files
committed
feat(render-core): update logger to console
1 parent 1020f98 commit 8ef2152

File tree

11 files changed

+66
-31
lines changed

11 files changed

+66
-31
lines changed

packages/designer/src/builtin-simulator/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './host';
22
export * from './host-view';
33
export * from './renderer';
44
export * from './live-editing/live-editing';
5+
export { LowcodeTypes } from './utils/parse-metadata';

packages/designer/src/builtin-simulator/utils/parse-metadata.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ function define(propType: any = PropTypes.any, lowcodeType: string | object = {}
4646
return lowcodeCheckType;
4747
}
4848

49-
const LowcodeTypes: any = {
49+
export const LowcodeTypes: any = {
5050
...PropTypes,
5151
define,
5252
};
5353

5454
(window as any).PropTypes = LowcodeTypes;
55-
(window as any).React.PropTypes = LowcodeTypes;
55+
if ((window as any).React) {
56+
(window as any).React.PropTypes = LowcodeTypes;
57+
}
5658

5759
// override primitive type checkers
5860
primitiveTypes.forEach((type) => {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../../babel.config');

packages/renderer-core/src/hoc/leaf.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { isReactComponent, cloneEnumerableProperty } from '@alilc/lowcode-utils'
44
import { debounce } from '../utils/common';
55
import adapter from '../adapter';
66
import * as types from '../types/index';
7+
import logger from '../utils/logger';
78

89
export interface IComponentHocInfo {
910
schema: any;
@@ -183,7 +184,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
183184
}
184185

185186
if (!isReactComponent(Comp)) {
186-
console.error(`${schema.componentName} component may be has errors: `, Comp);
187+
logger.error(`${schema.componentName} component may be has errors: `, Comp);
187188
}
188189

189190
initRerenderEvent({

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import PropTypes from 'prop-types';
22
import baseRendererFactory from './base';
33
import { isEmpty } from '../utils';
44
import { IRendererAppHelper, IBaseRendererProps, IBaseRenderComponent } from '../types';
5+
import logger from '../utils/logger';
56

67
export default function addonRendererFactory(): IBaseRenderComponent {
78
const BaseRenderer = baseRendererFactory();
@@ -32,7 +33,7 @@ export default function addonRendererFactory(): IBaseRenderComponent {
3233
const schema = props.__schema || {};
3334
this.state = this.__parseData(schema.state || {});
3435
if (isEmpty(props.config) || !props.config?.addonKey) {
35-
console.warn('lce addon has wrong config');
36+
logger.warn('lce addon has wrong config');
3637
this.setState({
3738
__hasError: true,
3839
});

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ export function executeLifeCycleMethod(context: any, schema: IPublicTypeNodeSche
5656
}
5757

5858
if (typeof fn !== 'function') {
59-
console.error(`生命周期${method}类型不符`, fn);
59+
logger.error(`生命周期${method}类型不符`, fn);
6060
return;
6161
}
6262

6363
try {
6464
return fn.apply(context, args);
6565
} catch (e) {
66-
console.error(`[${schema.componentName}]生命周期${method}出错`, e);
66+
logger.error(`[${schema.componentName}]生命周期${method}出错`, e);
6767
}
6868
}
6969

@@ -208,7 +208,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
208208

209209
async componentDidCatch(...args: any[]) {
210210
this.__executeLifeCycleMethod('componentDidCatch', args);
211-
console.warn(args);
211+
logger.warn(args);
212212
}
213213

214214
reloadDataSource = () => new Promise((resolve, reject) => {
@@ -278,7 +278,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
278278
value = this.__parseExpression(value, this);
279279
}
280280
if (typeof value !== 'function') {
281-
console.error(`custom method ${key} can not be parsed to a valid function`, value);
281+
logger.error(`custom method ${key} can not be parsed to a valid function`, value);
282282
return;
283283
}
284284
this[key] = value.bind(this);
@@ -369,7 +369,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
369369
this.setLocale = (loc: string) => {
370370
const setLocaleFn = this.appHelper?.utils?.i18n?.setLocale;
371371
if (!setLocaleFn || typeof setLocaleFn !== 'function') {
372-
console.warn('initI18nAPIs Failed, i18n only works when appHelper.utils.i18n.setLocale() exists');
372+
logger.warn('initI18nAPIs Failed, i18n only works when appHelper.utils.i18n.setLocale() exists');
373373
return undefined;
374374
}
375375
return setLocaleFn(loc);
@@ -527,7 +527,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
527527
: {};
528528

529529
if (!Comp) {
530-
console.error(`${schema.componentName} component is not found in components list! component list is:`, components || this.props.__container?.components);
530+
logger.error(`${schema.componentName} component is not found in components list! component list is:`, components || this.props.__container?.components);
531531
return engine.createElement(
532532
engine.getNotFoundComponent(),
533533
{
@@ -749,7 +749,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
749749

750750
__createLoopVirtualDom = (schema: IPublicTypeNodeSchema, scope: any, parentInfo: INodeInfo, idx: number | string) => {
751751
if (isFileSchema(schema)) {
752-
console.warn('file type not support Loop');
752+
logger.warn('file type not support Loop');
753753
return null;
754754
}
755755
if (!Array.isArray(schema.loop)) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { IBaseRenderComponent } from '../types';
2+
import logger from '../utils/logger';
23
import baseRendererFactory from './base';
34

45
export default function tempRendererFactory(): IBaseRenderComponent {
@@ -41,7 +42,7 @@ export default function tempRendererFactory(): IBaseRenderComponent {
4142
}
4243

4344
async componentDidCatch(e: any) {
44-
console.warn(e);
45+
logger.warn(e);
4546
this.__debug(`componentDidCatch - ${this.props.__schema.fileName}`);
4647
}
4748

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,13 @@ export function transformArrayToMap(arr: any[], key: string, overwrite = true) {
183183
return res;
184184
}
185185

186-
export function checkPropTypes(value: any, name: string, rule: any, componentName: string) {
186+
export function checkPropTypes(value: any, name: string, rule: any, componentName: string): boolean {
187187
let ruleFunction = rule;
188188
if (typeof rule === 'string') {
189189
ruleFunction = new Function(`"use strict"; const PropTypes = arguments[0]; return ${rule}`)(PropTypes2);
190190
}
191191
if (!ruleFunction || typeof ruleFunction !== 'function') {
192-
console.warn('checkPropTypes should have a function type rule argument');
192+
logger.warn('checkPropTypes should have a function type rule argument');
193193
return true;
194194
}
195195
const err = ruleFunction(
@@ -203,7 +203,7 @@ export function checkPropTypes(value: any, name: string, rule: any, componentNam
203203
ReactPropTypesSecret,
204204
);
205205
if (err) {
206-
console.warn(err);
206+
logger.warn(err);
207207
}
208208
return !err;
209209
}

packages/renderer-core/src/utils/data-helper.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export class DataHelper {
186186
}
187187
const { headers, ...otherProps } = otherOptionsObj || {};
188188
if (!req) {
189-
console.warn(`getDataSource API named ${id} not exist`);
189+
logger.warn(`getDataSource API named ${id} not exist`);
190190
return;
191191
}
192192

@@ -215,15 +215,15 @@ export class DataHelper {
215215
try {
216216
callbackFn && callbackFn(res && res[id]);
217217
} catch (e) {
218-
console.error('load请求回调函数报错', e);
218+
logger.error('load请求回调函数报错', e);
219219
}
220220
return res && res[id];
221221
})
222222
.catch((err) => {
223223
try {
224224
callbackFn && callbackFn(null, err);
225225
} catch (e) {
226-
console.error('load请求回调函数报错', e);
226+
logger.error('load请求回调函数报错', e);
227227
}
228228
return err;
229229
});
@@ -300,9 +300,9 @@ export class DataHelper {
300300
return dataHandlerFun.call(this.host, data, error);
301301
} catch (e) {
302302
if (id) {
303-
console.error(`[${id}]单个请求数据处理函数运行出错`, e);
303+
logger.error(`[${id}]单个请求数据处理函数运行出错`, e);
304304
} else {
305-
console.error('请求数据处理函数运行出错', e);
305+
logger.error('请求数据处理函数运行出错', e);
306306
}
307307
}
308308
}

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @ts-nocheck
1+
import factoryWithTypeCheckers from 'prop-types/factoryWithTypeCheckers';
22
import {
33
isSchema,
44
isFileSchema,
@@ -18,9 +18,14 @@ import {
1818
parseThisRequiredExpression,
1919
parseI18n,
2020
parseData,
21+
checkPropTypes,
2122
} from '../../src/utils/common';
2223
import logger from '../../src/utils/logger';
2324

25+
var ReactIs = require('react-is');
26+
27+
const PropTypes = factoryWithTypeCheckers(ReactIs.isElement, true);
28+
2429
describe('test isSchema', () => {
2530
it('should be false when empty value is passed', () => {
2631
expect(isSchema(null)).toBeFalsy();
@@ -461,4 +466,37 @@ describe('test parseData ', () => {
461466
expect(result.__privateKey).toBeUndefined();
462467

463468
});
469+
});
470+
471+
describe('checkPropTypes', () => {
472+
it('should validate correctly with valid prop type', () => {
473+
expect(checkPropTypes(123, 'age', PropTypes.number, 'TestComponent')).toBe(true);
474+
expect(checkPropTypes('123', 'age', PropTypes.string, 'TestComponent')).toBe(true);
475+
});
476+
477+
it('should log a warning and return false with invalid prop type', () => {
478+
expect(checkPropTypes(123, 'age', PropTypes.string, 'TestComponent')).toBe(false);
479+
expect(checkPropTypes('123', 'age', PropTypes.number, 'TestComponent')).toBe(false);
480+
});
481+
482+
it('should handle custom rule functions correctly', () => {
483+
const customRule = (props, propName) => {
484+
if (props[propName] !== 123) {
485+
return new Error('Invalid value');
486+
}
487+
};
488+
const result = checkPropTypes(123, 'customProp', customRule, 'TestComponent');
489+
expect(result).toBe(true);
490+
});
491+
492+
493+
it('should interpret and validate a rule given as a string', () => {
494+
const result = checkPropTypes(123, 'age', 'PropTypes.number', 'TestComponent');
495+
expect(result).toBe(true);
496+
});
497+
498+
it('should log a warning for invalid rule type', () => {
499+
const result = checkPropTypes(123, 'age', 123, 'TestComponent');
500+
expect(result).toBe(true);
501+
});
464502
});

0 commit comments

Comments
 (0)