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

View File

@ -75,8 +75,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
// 注意这里其实隐含了一个假设schema 中的 componentName 应该是一个有效的 JS 标识符,而且是大写字母打头的 // 注意这里其实隐含了一个假设schema 中的 componentName 应该是一个有效的 JS 标识符,而且是大写字母打头的
// FIXME: 为了快速修复临时加的逻辑,需要用 pre-process 的方式替代处理。 // FIXME: 为了快速修复临时加的逻辑,需要用 pre-process 的方式替代处理。
const mapComponentNameToAliasOrKeepIt = (componentName: string) => const mapComponentNameToAliasOrKeepIt = (componentName: string) => componentsNameAliasMap.get(componentName) || componentName;
componentsNameAliasMap.get(componentName) || componentName;
// 然后过滤掉所有的别名 chunks // 然后过滤掉所有的别名 chunks
next.chunks = next.chunks.filter((chunk) => !isImportAliasDefineChunk(chunk)); 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 { parseExpressionConvertThis2Context } from '../../../utils/expressionParser';
import { isValidContainerType } from '../../../utils/schema'; import { isValidContainerType } from '../../../utils/schema';
import { REACT_CHUNK_NAME } from './const'; import { REACT_CHUNK_NAME } from './const';
import { isJSExpressionFn } from '../../../utils/common';
export interface PluginConfig { export interface PluginConfig {
fileType?: string; fileType?: string;
@ -37,6 +38,7 @@ export interface PluginConfig {
* *
*/ */
datasourceConfig?: { datasourceConfig?: {
/** 数据源引擎的版本 */ /** 数据源引擎的版本 */
engineVersion?: string; engineVersion?: string;
@ -188,15 +190,15 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
export default pluginFactory; export default pluginFactory;
function wrapAsFunction(value: IPublicTypeCompositeValue, scope: IScope): IPublicTypeCompositeValue { function wrapAsFunction(value: IPublicTypeCompositeValue, scope: IScope): IPublicTypeCompositeValue {
if (isJSExpression(value) || isJSFunction(value)) { if (isJSExpression(value) || isJSFunction(value) || isJSExpressionFn(value)) {
return { return {
type: 'JSExpression', type: 'JSExpression',
value: `function(){ return ((${value.value}))}`, value: `function(){ return ((${value.value}))}.bind(this)`,
}; };
} }
return { return {
type: 'JSExpression', 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: ` content: `
const i18nConfig = ${i18nStr}; 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; const getLocale = () => locale;

View File

@ -1,3 +1,4 @@
import type { IPublicTypeJSExpression, IPublicTypeJSFunction } from '@alilc/lowcode-types';
import changeCase from 'change-case'; import changeCase from 'change-case';
import short from 'short-uuid'; import short from 'short-uuid';
@ -39,3 +40,7 @@ export function getStaticExprValue<T>(expr: string): T {
// eslint-disable-next-line no-new-func // eslint-disable-next-line no-new-func
return Function(`"use strict";return (${expr})`)(); 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 { generateJsSlot } from './jsSlot';
import { executeFunctionStack } from './aopHelper'; import { executeFunctionStack } from './aopHelper';
import { parseExpressionGetKeywords } from './expressionParser'; import { parseExpressionGetKeywords } from './expressionParser';
import { isJSExpressionFn } from './common';
interface ILegaoVariable { interface ILegaoVariable {
type: 'variable'; type: 'variable';
@ -159,7 +160,7 @@ function generateUnknownType(
return generateExpression(value, scope); return generateExpression(value, scope);
} }
if (isJSFunction(value)) { if (isJSFunction(value) || isJSExpressionFn(value)) {
if (options.handlers?.function) { if (options.handlers?.function) {
return executeFunctionStack(value, scope, options.handlers.function, genFunction, options); 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'; import type { IProjectInfo } from '../types/intermediate';
export interface DataSourceDependenciesConfig { export interface DataSourceDependenciesConfig {
/** 数据源引擎的版本 */ /** 数据源引擎的版本 */
engineVersion?: string; engineVersion?: string;
/** 数据源引擎的包名 */ /** 数据源引擎的包名 */
enginePackage?: string; enginePackage?: string;
/** 数据源 handlers 的版本 */ /** 数据源 handlers 的版本 */
handlersVersion?: { handlersVersion?: {
[key: string]: string; [key: string]: string;
}; };
/** 数据源 handlers 的包名 */ /** 数据源 handlers 的包名 */
handlersPackages?: { handlersPackages?: {
[key: string]: string; [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 { IPublicTypeJSExpression, IPublicTypeJSFunction, isJSExpression, isJSFunction } from '@alilc/lowcode-types';
import { CodeGeneratorError, IScope } from '../types'; import { CodeGeneratorError, IScope } from '../types';
import { transformExpressionLocalRef, ParseError } from './expressionParser'; import { transformExpressionLocalRef, ParseError } from './expressionParser';
import { isJSExpressionFn } from './common';
function parseFunction(content: string): t.FunctionExpression | null { function parseFunction(content: string): t.FunctionExpression | null {
try { try {
@ -79,7 +80,7 @@ function getBodyStatements(content: string) {
} }
export function isJsCode(value: unknown): boolean { export function isJsCode(value: unknown): boolean {
return isJSExpression(value) || isJSFunction(value); return isJSExpressionFn(value) || isJSFunction(value);
} }
export function generateExpression(value: any, scope: IScope): string { 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'); throw new CodeGeneratorError('Not a JSExpression');
} }
function getFunctionSource(cfg: IPublicTypeJSFunction): string {
return cfg.source || cfg.value || cfg.compiled;
}
export function generateFunction( export function generateFunction(
value: any, value: any,
config: { config: {
@ -114,19 +119,20 @@ export function generateFunction(
) { ) {
if (isJsCode(value)) { if (isJsCode(value)) {
const functionCfg = value as IPublicTypeJSFunction; const functionCfg = value as IPublicTypeJSFunction;
const functionSource = getFunctionSource(functionCfg);
if (config.isMember) { if (config.isMember) {
return transformFuncExpr2MethodMember(config.name || '', functionCfg.value); return transformFuncExpr2MethodMember(config.name || '', functionSource);
} }
if (config.isBlock) { if (config.isBlock) {
return getBodyStatements(functionCfg.value); return getBodyStatements(functionSource);
} }
if (config.isArrow) { if (config.isArrow) {
return getArrowFunction(functionCfg.value); return getArrowFunction(functionSource);
} }
if (config.isBindExpr) { 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'); throw new CodeGeneratorError('Not a JSFunction or JSExpression');

View File

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