GeezerVison is showing github.com — fix: 🐛 解决 react 中 jsx 出码的时候对于循环数据漏包 __$evalArray 的问题 · swordsman-coder/lowcode-engine@3b9b177
File treeExpand file treeCollapse file tree Open diff view settings demo1/expected/demo-project/src/pages/Test demo2-utils-name-alias/expected/demo-project/src/pages/Aaaa demo3/expected/demo-project/src/pages/Test demo6-literal-condition/expected/demo-project/src/pages/Test demo8-datasource-prop/expected/demo-project/src/pages/Example demo9-datasource-engine/expected/demo-project/src/components/Index demo_10-jsslot/expected/demo-project/src/pages/Test demo_11-jsslot-2/expected/demo-project/src/pages/Test
plugins/jsx/__snapshots__
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/package.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 75 | 75 | "change-case": "^3.1.0", | 76 | 76 | "commander": "^6.1.0", | 77 | 77 | "debug": "^4.3.2", | | 78 | + "fp-ts": "^2.11.9", | 78 | 79 | "fs-extra": "9.x", | 79 | 80 | "glob": "^7.2.0", | 80 | 81 | "html-entities": "^2.3.2", | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/react/containerInjectUtils.ts +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -83,6 +83,23 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 83 | 83 | `, | 84 | 84 | linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsMethod]], | 85 | 85 | }); | | 86 | + } else { | | 87 | + // useRef 为 false 的时候是指没有组件在 props 中配置 ref 属性,但这个时候其实也可能有代码访问 this.$/$$ 所以还是加上个空的代码 | | 88 | + next.chunks.push({ | | 89 | + type: ChunkType.STRING, | | 90 | + fileType: cfg.fileType, | | 91 | + name: CLASS_DEFINE_CHUNK_NAME.InsMethod, | | 92 | + content: ` $ = () => null; `, | | 93 | + linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsMethod]], | | 94 | + }); | | 95 | + | | 96 | + next.chunks.push({ | | 97 | + type: ChunkType.STRING, | | 98 | + fileType: cfg.fileType, | | 99 | + name: CLASS_DEFINE_CHUNK_NAME.InsMethod, | | 100 | + content: ` $$ = () => []; `, | | 101 | + linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsMethod]], | | 102 | + }); | 86 | 103 | } | 87 | 104 |
| 88 | 105 | return next; | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/react/jsx.ts +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -47,9 +47,9 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 47 | 47 | // 这里会将内部的一些子上下文的访问(this.xxx)转换为 __$$context.xxx 的形式 | 48 | 48 | // 与 Rax 所不同的是,这里不会将最顶层的 this 转换掉 | 49 | 49 | const customHandlers: HandlerSet<string> = { | 50 | | - expression(input: JSExpression, scope: IScope) { | | 50 | + expression(input: JSExpression, scope: IScope, config) { | 51 | 51 | return transformJsExpr(generateExpression(input, scope), scope, { | 52 | | - dontWrapEval: !tolerateEvalErrors, | | 52 | + dontWrapEval: !(config?.tolerateEvalErrors ?? tolerateEvalErrors), | 53 | 53 | dontTransformThis2ContextAtRootScope: true, | 54 | 54 | }); | 55 | 55 | }, | @@ -71,6 +71,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 71 | 71 | const generatorPlugins: NodeGeneratorConfig = { | 72 | 72 | handlers: customHandlers, | 73 | 73 | tagMapping: (v) => nodeTypeMapping[v] || v, | | 74 | + tolerateEvalErrors, | 74 | 75 | }; | 75 | 76 |
| 76 | 77 | if (next.contextData.useRefApi) { | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/types/core.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -217,6 +217,7 @@ export interface HandlerSet<T> { | 217 | 217 | export interface CompositeValueGeneratorOptions { | 218 | 218 | handlers?: HandlerSet<string>; | 219 | 219 | nodeGenerator?: NodeGenerator<string>; | | 220 | + tolerateEvalErrors?: boolean; | 220 | 221 | } | 221 | 222 |
| 222 | 223 | /** | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/types/jsx.ts +12-1Lines changed: 12 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,7 +15,10 @@ export interface CodePiece { | 15 | 15 | type: PIECE_TYPE; | 16 | 16 | } | 17 | 17 |
| 18 | | -export interface AttrData { attrName: string; attrValue: CompositeValue } | | 18 | +export interface AttrData { | | 19 | + attrName: string; | | 20 | + attrValue: CompositeValue; | | 21 | +} | 19 | 22 | // 对 JSX 出码的理解,目前定制点包含 【包装】【标签名】【属性】 | 20 | 23 | export type AttrPlugin = BaseGenerator<AttrData, CodePiece[], NodeGeneratorConfig>; | 21 | 24 | export type NodePlugin = BaseGenerator<NodeSchema, CodePiece[], NodeGeneratorConfig>; | @@ -26,4 +29,12 @@ export interface NodeGeneratorConfig { | 26 | 29 | attrPlugins?: AttrPlugin[]; | 27 | 30 | nodePlugins?: NodePlugin[]; | 28 | 31 | self?: NodeGenerator<string>; | | 32 | + | | 33 | + /** | | 34 | + * 是否要容忍对 JSExpression 求值时的异常 | | 35 | + * 默认:true | | 36 | + * 注: 如果容忍异常,则会在求值时包裹 try-catch 块 -- 通过 __$$eval / __$$evalArray | | 37 | + * catch 到异常时默认会抛出一个 CustomEvent 事件里面包含异常信息和求值的表达式 | | 38 | + */ | | 39 | + tolerateEvalErrors?: boolean; | 29 | 40 | } |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/utils/jsExpression.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -79,10 +79,11 @@ export function isJsCode(value: unknown): boolean { | 79 | 79 |
| 80 | 80 | export function generateExpression(value: any, scope: IScope): string { | 81 | 81 | if (isJSExpression(value)) { | 82 | | - const exprVal = (value as JSExpression).value; | | 82 | + const exprVal = (value as JSExpression).value.trim(); | 83 | 83 | if (!exprVal) { | 84 | 84 | return 'null'; | 85 | 85 | } | | 86 | + | 86 | 87 | const afterProcessWithLocals = transformExpressionLocalRef(exprVal, scope); | 87 | 88 | return afterProcessWithLocals; | 88 | 89 | } | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/utils/nodeToJSX.ts +18-3Lines changed: 18 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import _ from 'lodash'; | | 2 | +import { pipe } from 'fp-ts/function'; | 2 | 3 | import { NodeSchema, isNodeSchema, NodeDataType, CompositeValue } from '@alilc/lowcode-types'; | 3 | 4 |
| 4 | 5 | import { | @@ -17,6 +18,7 @@ import { getStaticExprValue } from './common'; | 17 | 18 | import { executeFunctionStack } from './aopHelper'; | 18 | 19 | import { encodeJsxStringNode } from './encodeJsxAttrString'; | 19 | 20 | import { unwrapJsExprQuoteInJsx } from './jsxHelpers'; | | 21 | +import { transformThis2Context } from '../core/jsx/handlers/transformThis2Context'; | 20 | 22 |
| 21 | 23 | function mergeNodeGeneratorConfig( | 22 | 24 | cfg1: NodeGeneratorConfig, | @@ -255,16 +257,29 @@ export function generateReactLoopCtrl( | 255 | 257 | next?: NodePlugin, | 256 | 258 | ): CodePiece[] { | 257 | 259 | if (nodeItem.loop) { | | 260 | + const tolerateEvalErrors = config?.tolerateEvalErrors ?? true; | | 261 | + | 258 | 262 | const loopItemName = nodeItem.loopArgs?.[0] || 'item'; | 259 | 263 | const loopIndexName = nodeItem.loopArgs?.[1] || 'index'; | 260 | 264 |
| 261 | 265 | // 新建作用域 | 262 | 266 | const subScope = scope.createSubScope([loopItemName, loopIndexName]); | 263 | 267 | const pieces: CodePiece[] = next ? next(nodeItem, subScope, config) : []; | 264 | 268 |
| 265 | | - const loopDataExpr = generateCompositeType(nodeItem.loop, scope, { | 266 | | - handlers: config?.handlers, | 267 | | - }); | | 269 | + // 生成循环变量表达式 | | 270 | + const loopDataExpr = pipe( | | 271 | + nodeItem.loop, | | 272 | + // 将 JSExpression 转换为 JS 表达式代码: | | 273 | + (expr) => | | 274 | + generateCompositeType(expr, scope, { | | 275 | + handlers: config?.handlers, | | 276 | + tolerateEvalErrors: false, // 这个内部不需要包 try catch, 下面会统一加的 | | 277 | + }), | | 278 | + // 将 this.xxx 转换为 __$$context.xxx: | | 279 | + (expr) => transformThis2Context(expr, scope, { ignoreRootScope: true }), | | 280 | + // 如果要容忍错误,则包一层 try catch (基于助手函数 __$$evalArray) | | 281 | + (expr) => (tolerateEvalErrors ? `__$$evalArray(() => (${expr}))` : expr), | | 282 | + ); | 268 | 283 |
| 269 | 284 | pieces.unshift({ | 270 | 285 | value: `(${loopDataExpr}).map((${loopItemName}, ${loopIndexName}) => ((__$$context) => (`, | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/test-cases/react-app/demo1/expected/demo-project/src/pages/Test/index.jsx +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -152,7 +152,7 @@ class Test$$Page extends React.Component { | 152 | 152 | </Form.Item> | 153 | 153 | <div style={{ textAlign: "center" }}> | 154 | 154 | <Button.Group> | 155 | | - {["a", "b", "c"].map((item, index) => | | 155 | + {__$$evalArray(() => ["a", "b", "c"]).map((item, index) => | 156 | 156 | ((__$$context) => | 157 | 157 | !!__$$eval(() => index >= 1) && ( | 158 | 158 | <Button type="primary" style={{ margin: "0 5px 0 5px" }}> | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/test-cases/react-app/demo2-utils-name-alias/expected/demo-project/src/pages/Aaaa/index.jsx +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,6 +45,10 @@ class Aaaa$$Page extends React.Component { | 45 | 45 | this.state = {}; | 46 | 46 | } | 47 | 47 |
| | 48 | + $ = () => null; | | 49 | + | | 50 | + $$ = () => []; | | 51 | + | 48 | 52 | _defineDataSourceConfig() { | 49 | 53 | const _this = this; | 50 | 54 | return { | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/test-cases/react-app/demo3/expected/demo-project/src/pages/Test/index.jsx +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,6 +38,10 @@ class Test$$Page extends React.Component { | 38 | 38 | this.state = {}; | 39 | 39 | } | 40 | 40 |
| | 41 | + $ = () => null; | | 42 | + | | 43 | + $$ = () => []; | | 44 | + | 41 | 45 | componentDidMount() {} | 42 | 46 |
| 43 | 47 | render() { | |
|