Skip to content

Commit 4b59190

Browse files
JackLianliujuping
authored andcommitted
fix: fix lint issues for renderer-core/renderer/base
1 parent 5dd4625 commit 4b59190

File tree

3 files changed

+66
-37
lines changed

3 files changed

+66
-37
lines changed

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

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-console */
12
/* eslint-disable max-len */
23
/* eslint-disable react/prop-types */
34
import classnames from 'classnames';
@@ -47,7 +48,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
4748
Record<string, any>,
4849
any
4950
>;
50-
const createElement = runtime.createElement;
51+
const { createElement } = runtime;
5152
const Div = divFactory();
5253
const VisualDom = visualDomFactory();
5354
const AppContext = contextFactory();
@@ -97,6 +98,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
9798
this.__debug(`constructor - ${props?.__schema?.fileName}`);
9899
}
99100

101+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
100102
__beforeInit(_props: IBaseRendererProps) { }
101103

102104
__init(props: IBaseRendererProps) {
@@ -107,6 +109,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
107109
this.__initI18nAPIs();
108110
}
109111

112+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
110113
__afterInit(_props: IBaseRendererProps) { }
111114

112115
static getDerivedStateFromProps(props: IBaseRendererProps, state: any) {
@@ -120,35 +123,36 @@ export default function baseRendererFactory(): IBaseRenderComponent {
120123
}
121124

122125
if (typeof func === 'function') {
126+
// eslint-disable-next-line @typescript-eslint/ban-types
123127
return (func as Function)(props, state);
124128
}
125129
}
126130
return null;
127131
}
128132

129-
async getSnapshotBeforeUpdate() {
130-
this.__setLifeCycleMethods('getSnapshotBeforeUpdate', arguments);
133+
async getSnapshotBeforeUpdate(...args: any[]) {
134+
this.__setLifeCycleMethods('getSnapshotBeforeUpdate', args);
131135
this.__debug(`getSnapshotBeforeUpdate - ${this.props?.__schema?.fileName}`);
132136
}
133137

134-
async componentDidMount() {
138+
async componentDidMount(...args: any[]) {
135139
this.reloadDataSource();
136-
this.__setLifeCycleMethods('componentDidMount', arguments);
140+
this.__setLifeCycleMethods('componentDidMount', args);
137141
this.__debug(`componentDidMount - ${this.props?.__schema?.fileName}`);
138142
}
139143

140-
async componentDidUpdate(...args: any) {
144+
async componentDidUpdate(...args: any[]) {
141145
this.__setLifeCycleMethods('componentDidUpdate', args);
142146
this.__debug(`componentDidUpdate - ${this.props.__schema.fileName}`);
143147
}
144148

145-
async componentWillUnmount(...args: any) {
149+
async componentWillUnmount(...args: any[]) {
146150
this.__setLifeCycleMethods('componentWillUnmount', args);
147151
this.__debug(`componentWillUnmount - ${this.props?.__schema?.fileName}`);
148152
}
149153

150-
async componentDidCatch(e: any) {
151-
this.__setLifeCycleMethods('componentDidCatch', arguments);
154+
async componentDidCatch(e: any, ...args: any[]) {
155+
this.__setLifeCycleMethods('componentDidCatch', { e, ...args });
152156
console.warn(e);
153157
}
154158

@@ -165,7 +169,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
165169
this.forceUpdate();
166170
return resolve({});
167171
}
168-
this.setState(res, resolve);
172+
this.setState(res, resolve as () => void);
169173
})
170174
.catch((err: Error) => {
171175
if (this.__showPlaceholder) {
@@ -229,14 +233,15 @@ export default function baseRendererFactory(): IBaseRenderComponent {
229233
});
230234
this.__customMethodsList = customMethodsList;
231235
forEach(__schema.methods, (val: any, key: string) => {
232-
if (isJSExpression(val) || isJSFunction(val)) {
233-
val = this.parseExpression(val, this);
236+
let value = val;
237+
if (isJSExpression(value) || isJSFunction(value)) {
238+
value = this.parseExpression(value, this);
234239
}
235-
if (typeof val !== 'function') {
236-
console.error(`自定义函数${key}类型不符`, val);
240+
if (typeof value !== 'function') {
241+
console.error(`自定义函数${key}类型不符`, value);
237242
return;
238243
}
239-
this[key] = val.bind(this);
244+
this[key] = value.bind(this);
240245
});
241246
};
242247

@@ -304,7 +309,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
304309
this.forceUpdate();
305310
return resolve({});
306311
}
307-
this.setState(res, resolve);
312+
this.setState(res, resolve as () => void);
308313
})
309314
.catch((err: Error) => {
310315
if (this.__showPlaceholder) {
@@ -417,7 +422,9 @@ export default function baseRendererFactory(): IBaseRenderComponent {
417422
// self 为每个渲染组件构造的上下文,self是自上而下继承的
418423
// parentInfo 父组件的信息,包含schema和Comp
419424
// idx 若为循环渲染的循环Index
420-
__createVirtualDom = (schema: NodeData | NodeData[] | undefined, scope: any, parentInfo: IInfo, idx: string | number = ''): any => {
425+
__createVirtualDom = (originalSchema: NodeData | NodeData[] | undefined, originalScope: any, parentInfo: IInfo, idx: string | number = ''): any => {
426+
let scope = originalScope;
427+
let schema = originalSchema;
421428
const { engine } = this.context || {};
422429
try {
423430
if (!schema) return null;
@@ -588,7 +595,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
588595
}
589596

590597
let child = this.__getSchemaChildrenVirtualDom(schema, scope, Comp);
591-
const renderComp = (props: any) => engine.createElement(Comp, props, child);
598+
const renderComp = (innerProps: any) => engine.createElement(Comp, innerProps, child);
592599
// 设计模式下的特殊处理
593600
if (engine && [DESIGN_MODE.EXTEND, DESIGN_MODE.BORDER].includes(engine.props.designMode)) {
594601
// 对于overlay,dialog等组件为了使其在设计模式下显示,外层需要增加一个div容器
@@ -698,7 +705,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
698705
if (!Array.isArray(schema.loop)) return null;
699706
const itemArg = (schema.loopArgs && schema.loopArgs[0]) || 'item';
700707
const indexArg = (schema.loopArgs && schema.loopArgs[1]) || 'index';
701-
const loop: (JSONValue| CompositeValue)[] = schema.loop;
708+
const { loop } = schema;
702709
return loop.map((item: JSONValue | CompositeValue, i: number) => {
703710
const loopSelf: any = {
704711
[itemArg]: item,
@@ -722,7 +729,8 @@ export default function baseRendererFactory(): IBaseRenderComponent {
722729
return engine?.props?.designMode === 'design';
723730
}
724731

725-
__parseProps = (props: any, scope: any, path: string, info: IInfo): any => {
732+
__parseProps = (originalProps: any, scope: any, path: string, info: IInfo): any => {
733+
let props = originalProps;
726734
const { schema, Comp, componentInfo = {} } = info;
727735
const propInfo = getValue(componentInfo.props, path);
728736
// FIXME! 将这行逻辑外置,解耦,线上环境不要验证参数,调试环境可以有,通过传参自定义
@@ -737,14 +745,14 @@ export default function baseRendererFactory(): IBaseRenderComponent {
737745
if (isEmpty(params)) {
738746
return checkProps(this.__createVirtualDom(data, scope, ({ schema, Comp } as IInfo)));
739747
}
740-
return checkProps(function () {
748+
return checkProps((...argValues: any[]) => {
741749
const args: any = {};
742750
if (Array.isArray(params) && params.length) {
743751
params.forEach((item, idx) => {
744752
if (typeof item === 'string') {
745-
args[item] = arguments[idx];
753+
args[item] = argValues[idx];
746754
} else if (item && typeof item === 'object') {
747-
args[item.name] = arguments[idx];
755+
args[item.name] = argValues[idx];
748756
}
749757
});
750758
}
@@ -770,7 +778,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
770778
if (!isSchema(props) && !isJSSlot(props)) return checkProps(props);
771779
}
772780

773-
const handleLegaoI18n = (props: any) => props[props.use || 'zh_CN'];
781+
const handleLegaoI18n = (innerProps: any) => innerProps[innerProps.use || 'zh_CN'];
774782

775783
// 兼容乐高设计态 i18n 数据
776784
if (isI18nData(props)) {
@@ -810,13 +818,16 @@ export default function baseRendererFactory(): IBaseRenderComponent {
810818
&& propInfo?.props?.types?.indexOf('ReactNode') > -1
811819
&& propInfo?.props?.reactNodeProps?.type === 'function'
812820
);
821+
822+
let params = null;
823+
if (isReactNodeFunction) {
824+
params = propInfo?.props?.params;
825+
} else if (isMixinReactNodeFunction) {
826+
params = propInfo?.props?.reactNodeProps?.params;
827+
}
813828
return parseReactNode(
814829
props,
815-
isReactNodeFunction
816-
? propInfo.props.params
817-
: isMixinReactNodeFunction
818-
? propInfo.props.reactNodeProps.params
819-
: null,
830+
params,
820831
);
821832
}
822833
if (Array.isArray(props)) {
@@ -857,23 +868,22 @@ export default function baseRendererFactory(): IBaseRenderComponent {
857868
__debug = logger.log;
858869

859870
__renderContextProvider = (customProps?: object, children?: any) => {
860-
customProps = customProps || {};
861-
children = children || this.__createDom();
862871
return createElement(AppContext.Provider, {
863872
value: {
864873
...this.context,
865874
blockContext: this,
866-
...customProps,
875+
...(customProps || {}),
867876
},
868-
children,
877+
children: children || this.__createDom(),
869878
});
870879
};
871880

872881
__renderContextConsumer = (children: any) => {
873882
return createElement(AppContext.Consumer, {}, children);
874883
};
875884

876-
__getHocComp(Comp: any, schema: any, scope: any) {
885+
__getHocComp(OriginalComp: any, schema: any, scope: any) {
886+
let Comp = OriginalComp;
877887
this.componentHoc.forEach((ComponentConstruct: IComponentConstruct) => {
878888
Comp = ComponentConstruct(Comp || Div, {
879889
schema,
@@ -886,7 +896,8 @@ export default function baseRendererFactory(): IBaseRenderComponent {
886896
return Comp;
887897
}
888898

889-
__renderComp(Comp: any, ctxProps: object) {
899+
__renderComp(OriginalComp: any, ctxProps: object) {
900+
let Comp = OriginalComp;
890901
const { __schema } = this.props;
891902
const { __ctx } = this.props;
892903
const scope: any = {};
@@ -935,7 +946,8 @@ export default function baseRendererFactory(): IBaseRenderComponent {
935946
}, children);
936947
}
937948

938-
__checkSchema = (schema: NodeSchema | undefined, extraComponents: string | string[] = []) => {
949+
__checkSchema = (schema: NodeSchema | undefined, originalExtraComponents: string | string[] = []) => {
950+
let extraComponents = originalExtraComponents;
939951
if (typeof extraComponents === 'string') {
940952
extraComponents = [extraComponents];
941953
}

packages/renderer-core/tests/adapter/adapter.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// @ts-nocheck
22
import adapter, { Env } from '../../src/adapter';
3-
import { IRuntime, IRendererModules, IGeneralConstructor } from '../../src/types';
43

54

65

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const mockGetRenderers = jest.fn();
2+
jest.mock('../../src/adapter', () => {
3+
return {
4+
getRenderers: () => { return mockGetRenderers();}
5+
};
6+
});
7+
8+
import baseRendererFactory from '../../src/renderer/base';
9+
10+
describe('Base Render', () => {
11+
it('customBaseRenderer logic works', () => {
12+
mockGetRenderers.mockReturnValue({BaseRenderer: {}});
13+
const baseRenderer = baseRendererFactory();
14+
expect(mockGetRenderers).toBeCalledTimes(1);
15+
expect(baseRenderer).toStrictEqual({});
16+
mockGetRenderers.mockClear();
17+
});
18+
});

0 commit comments

Comments
 (0)