mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-01-15 10:48:17 +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 { IScope, CompositeValueGeneratorOptions, CodeGeneratorError } from '../types';
|
||||||
import { generateExpression, generateFunction } from './jsExpression';
|
import { generateExpression, generateFunction } from './jsExpression';
|
||||||
import { generateJsSlot } from './jsSlot';
|
import { generateJsSlot } from './jsSlot';
|
||||||
import { isValidIdentifier } from './validate';
|
import { isVaildMemberName } from './validate';
|
||||||
import { camelize } from './common';
|
|
||||||
import { executeFunctionStack } from './aopHelper';
|
import { executeFunctionStack } from './aopHelper';
|
||||||
|
|
||||||
function generateArray(value: CompositeArray, scope: IScope, options: CompositeValueGeneratorOptions = {}): string {
|
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 {
|
function generateObject(value: CompositeObject, scope: IScope, options: CompositeValueGeneratorOptions = {}): string {
|
||||||
const body = Object.keys(value)
|
const body = Object.keys(value)
|
||||||
.map((key) => {
|
.map((key) => {
|
||||||
let propName = key;
|
const propName = isVaildMemberName(key) ? key : `'${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 v = generateUnknownType(value[key], scope, options);
|
const v = generateUnknownType(value[key], scope, options);
|
||||||
return `${propName}: ${v}`;
|
return `${propName}: ${v}`;
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,3 +1,11 @@
|
|||||||
export const isValidIdentifier = (name: string) => {
|
export const isValidIdentifier = (name: string) => {
|
||||||
return /^[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*$/.test(name);
|
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