Merge branch hotfix/code-propname-fix into release/1.0.0

Title: fix: Object 类型数据属性名处理方法修复 

Link: https://code.aone.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/codereview/4068950
This commit is contained in:
mark.ck 2020-11-11 11:24:21 +08:00
commit 19537aad22
2 changed files with 10 additions and 19 deletions

View File

@ -13,8 +13,7 @@ import _ from 'lodash';
import { IScope, CompositeValueGeneratorOptions, CodeGeneratorError } from '../types';
import { generateExpression, generateFunction } from './jsExpression';
import { generateJsSlot } from './jsSlot';
import { isValidIdentifier } from './validate';
import { camelize } from './common';
import { isVaildMemberName } from './validate';
import { executeFunctionStack } from './aopHelper';
function generateArray(value: CompositeArray, scope: IScope, options: CompositeValueGeneratorOptions = {}): string {
@ -25,23 +24,7 @@ function generateArray(value: CompositeArray, scope: IScope, options: CompositeV
function generateObject(value: CompositeObject, scope: IScope, options: CompositeValueGeneratorOptions = {}): string {
const body = Object.keys(value)
.map((key) => {
let propName = key;
// TODO: 可以增加更多智能修复的方法
const fixMethods: Array<(v: string) => string> = [camelize];
// Try to fix propName
while (!isValidIdentifier(propName)) {
const fixMethod = fixMethods.pop();
if (fixMethod) {
try {
propName = fixMethod(propName);
} catch (error) {
throw new CodeGeneratorError(error.message);
}
} else {
throw new CodeGeneratorError(`Propname: ${key} is not a valid identifier.`);
}
}
const propName = isVaildMemberName(key) ? key : `'${key}'`;
const v = generateUnknownType(value[key], scope, options);
return `${propName}: ${v}`;
})

View File

@ -1,3 +1,11 @@
export const isValidIdentifier = (name: string) => {
return /^[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*$/.test(name);
};
export const isVaildMemberName = (name: string) => {
if (!isValidIdentifier(name)) {
return /^'[^']+'$/.test(name) || /^"[^"]+"$/.test(name);
}
return true;
};