fix: 兼容 isJSExpression 不包含函数类型的修改

This commit is contained in:
LeoYuan 袁力皓 2023-02-08 14:49:52 +08:00
parent 574e348c1e
commit da7ff59066
9 changed files with 35 additions and 13 deletions

View File

@ -13,6 +13,7 @@ import {
IContainerInfo,
} from '../../../types';
import { debug } from '../../../utils/debug';
import { isJSExpressionFn } from '../../../utils/common';
export interface PluginConfig {
fileType: string;
@ -49,6 +50,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
// 过滤掉非法数据(有些场景下会误传入空字符串或 null)
if (
!isJSFunction(lifeCycles[lifeCycleName]) &&
!isJSExpressionFn(lifeCycles[lifeCycleName]) &&
!isJSExpression(lifeCycles[lifeCycleName])
) {
return;

View File

@ -75,8 +75,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
// 注意这里其实隐含了一个假设schema 中的 componentName 应该是一个有效的 JS 标识符,而且是大写字母打头的
// FIXME: 为了快速修复临时加的逻辑,需要用 pre-process 的方式替代处理。
const mapComponentNameToAliasOrKeepIt = (componentName: string) =>
componentsNameAliasMap.get(componentName) || componentName;
const mapComponentNameToAliasOrKeepIt = (componentName: string) => componentsNameAliasMap.get(componentName) || componentName;
// 然后过滤掉所有的别名 chunks
next.chunks = next.chunks.filter((chunk) => !isImportAliasDefineChunk(chunk));

View File

@ -29,6 +29,7 @@ import { generateCompositeType } from '../../../utils/compositeType';
import { parseExpressionConvertThis2Context } from '../../../utils/expressionParser';
import { isValidContainerType } from '../../../utils/schema';
import { REACT_CHUNK_NAME } from './const';
import { isJSExpressionFn } from '../../../utils/common';
export interface PluginConfig {
fileType?: string;
@ -37,6 +38,7 @@ export interface PluginConfig {
*
*/
datasourceConfig?: {
/** 数据源引擎的版本 */
engineVersion?: string;
@ -188,15 +190,15 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
export default pluginFactory;
function wrapAsFunction(value: IPublicTypeCompositeValue, scope: IScope): IPublicTypeCompositeValue {
if (isJSExpression(value) || isJSFunction(value)) {
if (isJSExpression(value) || isJSFunction(value) || isJSExpressionFn(value)) {
return {
type: 'JSExpression',
value: `function(){ return ((${value.value}))}`,
value: `function(){ return ((${value.value}))}.bind(this)`,
};
}
return {
type: 'JSExpression',
value: `function(){return((${generateCompositeType(value, scope)}))}`,
value: `function(){return((${generateCompositeType(value, scope)}))}.bind(this)`,
};
}

View File

@ -25,7 +25,8 @@ const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
content: `
const i18nConfig = ${i18nStr};
let locale = typeof navigator === 'object' && typeof navigator.language === 'string' ? navigator.language : 'zh-CN';
// let locale = typeof navigator === 'object' && typeof navigator.language === 'string' ? navigator.language : 'zh-CN';
let locale = 'zh-CN';
const getLocale = () => locale;

View File

@ -1,3 +1,4 @@
import type { IPublicTypeJSExpression, IPublicTypeJSFunction } from '@alilc/lowcode-types';
import changeCase from 'change-case';
import short from 'short-uuid';
@ -39,3 +40,7 @@ export function getStaticExprValue<T>(expr: string): T {
// eslint-disable-next-line no-new-func
return Function(`"use strict";return (${expr})`)();
}
export function isJSExpressionFn(data: any): data is IPublicTypeJSFunction {
return data?.type === 'JSExpression' && data?.extType === 'function';
}

View File

@ -16,6 +16,7 @@ import { generateExpression, generateFunction } from './jsExpression';
import { generateJsSlot } from './jsSlot';
import { executeFunctionStack } from './aopHelper';
import { parseExpressionGetKeywords } from './expressionParser';
import { isJSExpressionFn } from './common';
interface ILegaoVariable {
type: 'variable';
@ -159,7 +160,7 @@ function generateUnknownType(
return generateExpression(value, scope);
}
if (isJSFunction(value)) {
if (isJSFunction(value) || isJSExpressionFn(value)) {
if (options.handlers?.function) {
return executeFunctionStack(value, scope, options.handlers.function, genFunction, options);
}

View File

@ -2,14 +2,18 @@ import changeCase from 'change-case';
import type { IProjectInfo } from '../types/intermediate';
export interface DataSourceDependenciesConfig {
/** 数据源引擎的版本 */
engineVersion?: string;
/** 数据源引擎的包名 */
enginePackage?: string;
/** 数据源 handlers 的版本 */
handlersVersion?: {
[key: string]: string;
};
/** 数据源 handlers 的包名 */
handlersPackages?: {
[key: string]: string;

View File

@ -5,6 +5,7 @@ import * as t from '@babel/types';
import { IPublicTypeJSExpression, IPublicTypeJSFunction, isJSExpression, isJSFunction } from '@alilc/lowcode-types';
import { CodeGeneratorError, IScope } from '../types';
import { transformExpressionLocalRef, ParseError } from './expressionParser';
import { isJSExpressionFn } from './common';
function parseFunction(content: string): t.FunctionExpression | null {
try {
@ -79,7 +80,7 @@ function getBodyStatements(content: string) {
}
export function isJsCode(value: unknown): boolean {
return isJSExpression(value) || isJSFunction(value);
return isJSExpressionFn(value) || isJSFunction(value);
}
export function generateExpression(value: any, scope: IScope): string {
@ -96,6 +97,10 @@ export function generateExpression(value: any, scope: IScope): string {
throw new CodeGeneratorError('Not a JSExpression');
}
function getFunctionSource(cfg: IPublicTypeJSFunction): string {
return cfg.source || cfg.value || cfg.compiled;
}
export function generateFunction(
value: any,
config: {
@ -114,19 +119,20 @@ export function generateFunction(
) {
if (isJsCode(value)) {
const functionCfg = value as IPublicTypeJSFunction;
const functionSource = getFunctionSource(functionCfg);
if (config.isMember) {
return transformFuncExpr2MethodMember(config.name || '', functionCfg.value);
return transformFuncExpr2MethodMember(config.name || '', functionSource);
}
if (config.isBlock) {
return getBodyStatements(functionCfg.value);
return getBodyStatements(functionSource);
}
if (config.isArrow) {
return getArrowFunction(functionCfg.value);
return getArrowFunction(functionSource);
}
if (config.isBindExpr) {
return `(${functionCfg.value}).bind(this)`;
return `(${functionSource}).bind(this)`;
}
return functionCfg.value;
return functionSource;
}
throw new CodeGeneratorError('Not a JSFunction or JSExpression');

View File

@ -13,6 +13,7 @@ import {
isJSFunction,
} from '@alilc/lowcode-types';
import { CodeGeneratorError } from '../types/error';
import { isJSExpressionFn } from './common';
export function isContainerSchema(x: any): x is IPublicTypeContainerSchema {
return (
@ -128,6 +129,7 @@ export function handleSubNodes<T>(
// IPublicTypeCompositeObject
if (
!isJSExpression(value) &&
!isJSExpressionFn(value) &&
!isJSFunction(value) &&
typeof value === 'object' &&
value !== null