GeezerVison is showing github.com — fix: 兼容 isJSExpression 不包含函数类型的修改 · Juny0117/lowcode-engine@da7ff59
File treeExpand file treeCollapse file tree Open diff view settings modules/code-generator/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/rax/containerLifeCycle.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,7 @@ import { | 13 | 13 | IContainerInfo, | 14 | 14 | } from '../../../types'; | 15 | 15 | import { debug } from '../../../utils/debug'; | | 16 | +import { isJSExpressionFn } from '../../../utils/common'; | 16 | 17 |
| 17 | 18 | export interface PluginConfig { | 18 | 19 | fileType: string; | @@ -49,6 +50,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 49 | 50 | // 过滤掉非法数据(有些场景下会误传入空字符串或 null) | 50 | 51 | if ( | 51 | 52 | !isJSFunction(lifeCycles[lifeCycleName]) && | | 53 | + !isJSExpressionFn(lifeCycles[lifeCycleName]) && | 52 | 54 | !isJSExpression(lifeCycles[lifeCycleName]) | 53 | 55 | ) { | 54 | 56 | return; | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/rax/jsx.ts +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -75,8 +75,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 75 | 75 |
| 76 | 76 | // 注意:这里其实隐含了一个假设:schema 中的 componentName 应该是一个有效的 JS 标识符,而且是大写字母打头的 | 77 | 77 | // FIXME: 为了快速修复临时加的逻辑,需要用 pre-process 的方式替代处理。 | 78 | | - const mapComponentNameToAliasOrKeepIt = (componentName: string) => | 79 | | - componentsNameAliasMap.get(componentName) || componentName; | | 78 | + const mapComponentNameToAliasOrKeepIt = (componentName: string) => componentsNameAliasMap.get(componentName) || componentName; | 80 | 79 |
| 81 | 80 | // 然后过滤掉所有的别名 chunks | 82 | 81 | next.chunks = next.chunks.filter((chunk) => !isImportAliasDefineChunk(chunk)); | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/react/containerInjectDataSourceEngine.ts +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,6 +29,7 @@ import { generateCompositeType } from '../../../utils/compositeType'; | 29 | 29 | import { parseExpressionConvertThis2Context } from '../../../utils/expressionParser'; | 30 | 30 | import { isValidContainerType } from '../../../utils/schema'; | 31 | 31 | import { REACT_CHUNK_NAME } from './const'; | | 32 | +import { isJSExpressionFn } from '../../../utils/common'; | 32 | 33 |
| 33 | 34 | export interface PluginConfig { | 34 | 35 | fileType?: string; | @@ -37,6 +38,7 @@ export interface PluginConfig { | 37 | 38 | * 数据源配置 | 38 | 39 | */ | 39 | 40 | datasourceConfig?: { | | 41 | + | 40 | 42 | /** 数据源引擎的版本 */ | 41 | 43 | engineVersion?: string; | 42 | 44 |
| @@ -188,15 +190,15 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 188 | 190 | export default pluginFactory; | 189 | 191 |
| 190 | 192 | function wrapAsFunction(value: IPublicTypeCompositeValue, scope: IScope): IPublicTypeCompositeValue { | 191 | | - if (isJSExpression(value) || isJSFunction(value)) { | | 193 | + if (isJSExpression(value) || isJSFunction(value) || isJSExpressionFn(value)) { | 192 | 194 | return { | 193 | 195 | type: 'JSExpression', | 194 | | - value: `function(){ return ((${value.value}))}`, | | 196 | + value: `function(){ return ((${value.value}))}.bind(this)`, | 195 | 197 | }; | 196 | 198 | } | 197 | 199 |
| 198 | 200 | return { | 199 | 201 | type: 'JSExpression', | 200 | | - value: `function(){return((${generateCompositeType(value, scope)}))}`, | | 202 | + value: `function(){return((${generateCompositeType(value, scope)}))}.bind(this)`, | 201 | 203 | }; | 202 | 204 | } |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/project/i18n.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,7 +25,8 @@ const pluginFactory: BuilderComponentPluginFactory<unknown> = () => { | 25 | 25 | content: ` | 26 | 26 | const i18nConfig = ${i18nStr}; | 27 | 27 |
| 28 | | - let locale = typeof navigator === 'object' && typeof navigator.language === 'string' ? navigator.language : 'zh-CN'; | | 28 | + // let locale = typeof navigator === 'object' && typeof navigator.language === 'string' ? navigator.language : 'zh-CN'; | | 29 | + let locale = 'zh-CN'; | 29 | 30 |
| 30 | 31 | const getLocale = () => locale; | 31 | 32 |
| |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/utils/common.ts +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import type { IPublicTypeJSExpression, IPublicTypeJSFunction } from '@alilc/lowcode-types'; | 1 | 2 | import changeCase from 'change-case'; | 2 | 3 | import short from 'short-uuid'; | 3 | 4 |
| @@ -39,3 +40,7 @@ export function getStaticExprValue<T>(expr: string): T { | 39 | 40 | // eslint-disable-next-line no-new-func | 40 | 41 | return Function(`"use strict";return (${expr})`)(); | 41 | 42 | } | | 43 | + | | 44 | +export function isJSExpressionFn(data: any): data is IPublicTypeJSFunction { | | 45 | + return data?.type === 'JSExpression' && data?.extType === 'function'; | | 46 | +} |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/utils/compositeType.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,7 @@ import { generateExpression, generateFunction } from './jsExpression'; | 16 | 16 | import { generateJsSlot } from './jsSlot'; | 17 | 17 | import { executeFunctionStack } from './aopHelper'; | 18 | 18 | import { parseExpressionGetKeywords } from './expressionParser'; | | 19 | +import { isJSExpressionFn } from './common'; | 19 | 20 |
| 20 | 21 | interface ILegaoVariable { | 21 | 22 | type: 'variable'; | @@ -159,7 +160,7 @@ function generateUnknownType( | 159 | 160 | return generateExpression(value, scope); | 160 | 161 | } | 161 | 162 |
| 162 | | - if (isJSFunction(value)) { | | 163 | + if (isJSFunction(value) || isJSExpressionFn(value)) { | 163 | 164 | if (options.handlers?.function) { | 164 | 165 | return executeFunctionStack(value, scope, options.handlers.function, genFunction, options); | 165 | 166 | } | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/utils/dataSource.ts +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,14 +2,18 @@ import changeCase from 'change-case'; | 2 | 2 | import type { IProjectInfo } from '../types/intermediate'; | 3 | 3 |
| 4 | 4 | export interface DataSourceDependenciesConfig { | | 5 | + | 5 | 6 | /** 数据源引擎的版本 */ | 6 | 7 | engineVersion?: string; | | 8 | + | 7 | 9 | /** 数据源引擎的包名 */ | 8 | 10 | enginePackage?: string; | | 11 | + | 9 | 12 | /** 数据源 handlers 的版本 */ | 10 | 13 | handlersVersion?: { | 11 | 14 | [key: string]: string; | 12 | 15 | }; | | 16 | + | 13 | 17 | /** 数据源 handlers 的包名 */ | 14 | 18 | handlersPackages?: { | 15 | 19 | [key: string]: string; | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/utils/jsExpression.ts +12-6Lines changed: 12 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,7 @@ import * as t from '@babel/types'; | 5 | 5 | import { IPublicTypeJSExpression, IPublicTypeJSFunction, isJSExpression, isJSFunction } from '@alilc/lowcode-types'; | 6 | 6 | import { CodeGeneratorError, IScope } from '../types'; | 7 | 7 | import { transformExpressionLocalRef, ParseError } from './expressionParser'; | | 8 | +import { isJSExpressionFn } from './common'; | 8 | 9 |
| 9 | 10 | function parseFunction(content: string): t.FunctionExpression | null { | 10 | 11 | try { | @@ -79,7 +80,7 @@ function getBodyStatements(content: string) { | 79 | 80 | } | 80 | 81 |
| 81 | 82 | export function isJsCode(value: unknown): boolean { | 82 | | - return isJSExpression(value) || isJSFunction(value); | | 83 | + return isJSExpressionFn(value) || isJSFunction(value); | 83 | 84 | } | 84 | 85 |
| 85 | 86 | export function generateExpression(value: any, scope: IScope): string { | @@ -96,6 +97,10 @@ export function generateExpression(value: any, scope: IScope): string { | 96 | 97 | throw new CodeGeneratorError('Not a JSExpression'); | 97 | 98 | } | 98 | 99 |
| | 100 | +function getFunctionSource(cfg: IPublicTypeJSFunction): string { | | 101 | + return cfg.source || cfg.value || cfg.compiled; | | 102 | +} | | 103 | + | 99 | 104 | export function generateFunction( | 100 | 105 | value: any, | 101 | 106 | config: { | @@ -114,19 +119,20 @@ export function generateFunction( | 114 | 119 | ) { | 115 | 120 | if (isJsCode(value)) { | 116 | 121 | const functionCfg = value as IPublicTypeJSFunction; | | 122 | + const functionSource = getFunctionSource(functionCfg); | 117 | 123 | if (config.isMember) { | 118 | | - return transformFuncExpr2MethodMember(config.name || '', functionCfg.value); | | 124 | + return transformFuncExpr2MethodMember(config.name || '', functionSource); | 119 | 125 | } | 120 | 126 | if (config.isBlock) { | 121 | | - return getBodyStatements(functionCfg.value); | | 127 | + return getBodyStatements(functionSource); | 122 | 128 | } | 123 | 129 | if (config.isArrow) { | 124 | | - return getArrowFunction(functionCfg.value); | | 130 | + return getArrowFunction(functionSource); | 125 | 131 | } | 126 | 132 | if (config.isBindExpr) { | 127 | | - return `(${functionCfg.value}).bind(this)`; | | 133 | + return `(${functionSource}).bind(this)`; | 128 | 134 | } | 129 | | - return functionCfg.value; | | 135 | + return functionSource; | 130 | 136 | } | 131 | 137 |
| 132 | 138 | throw new CodeGeneratorError('Not a JSFunction or JSExpression'); | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/utils/schema.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,7 @@ import { | 13 | 13 | isJSFunction, | 14 | 14 | } from '@alilc/lowcode-types'; | 15 | 15 | import { CodeGeneratorError } from '../types/error'; | | 16 | +import { isJSExpressionFn } from './common'; | 16 | 17 |
| 17 | 18 | export function isContainerSchema(x: any): x is IPublicTypeContainerSchema { | 18 | 19 | return ( | @@ -128,6 +129,7 @@ export function handleSubNodes<T>( | 128 | 129 | // IPublicTypeCompositeObject | 129 | 130 | if ( | 130 | 131 | !isJSExpression(value) && | | 132 | + !isJSExpressionFn(value) && | 131 | 133 | !isJSFunction(value) && | 132 | 134 | typeof value === 'object' && | 133 | 135 | value !== null | |
|