mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-01-13 01:21:58 +00:00
fix: object property name fix logic
This commit is contained in:
parent
986ffe59b9
commit
dd69113249
@ -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}`;
|
||||
})
|
||||
|
||||
@ -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;
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user