fix: object property name fix logic

This commit is contained in:
春希 2020-11-11 11:12:14 +08:00
parent 986ffe59b9
commit dd69113249
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;
};