mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2025-12-11 18:42:56 +00:00
fix: invalid jsx attr name error (#2131)
This commit is contained in:
parent
622806f59c
commit
33efde964b
@ -19,6 +19,7 @@ import { executeFunctionStack } from './aopHelper';
|
|||||||
import { encodeJsxStringNode } from './encodeJsxAttrString';
|
import { encodeJsxStringNode } from './encodeJsxAttrString';
|
||||||
import { unwrapJsExprQuoteInJsx } from './jsxHelpers';
|
import { unwrapJsExprQuoteInJsx } from './jsxHelpers';
|
||||||
import { transformThis2Context } from '../core/jsx/handlers/transformThis2Context';
|
import { transformThis2Context } from '../core/jsx/handlers/transformThis2Context';
|
||||||
|
import { isValidIdentifier } from './validate';
|
||||||
|
|
||||||
function mergeNodeGeneratorConfig(
|
function mergeNodeGeneratorConfig(
|
||||||
cfg1: NodeGeneratorConfig,
|
cfg1: NodeGeneratorConfig,
|
||||||
@ -126,11 +127,13 @@ function generateAttrs(
|
|||||||
if (props) {
|
if (props) {
|
||||||
if (!Array.isArray(props)) {
|
if (!Array.isArray(props)) {
|
||||||
Object.keys(props).forEach((propName: string) => {
|
Object.keys(props).forEach((propName: string) => {
|
||||||
pieces = pieces.concat(generateAttr(propName, props[propName] as IPublicTypeCompositeValue, scope, config));
|
if (isValidIdentifier(propName)) {
|
||||||
|
pieces = pieces.concat(generateAttr(propName, props[propName] as IPublicTypeCompositeValue, scope, config));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
props.forEach((prop) => {
|
props.forEach((prop) => {
|
||||||
if (prop.name && !prop.spread) {
|
if (prop.name && isValidIdentifier(prop.name) && !prop.spread) {
|
||||||
pieces = pieces.concat(generateAttr(prop.name, prop.value, scope, config));
|
pieces = pieces.concat(generateAttr(prop.name, prop.value, scope, config));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -281,3 +281,75 @@ Object {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`condition at root invalid attr name should not be generated 1`] = `
|
||||||
|
Object {
|
||||||
|
"chunks": Array [
|
||||||
|
Object {
|
||||||
|
"content": "
|
||||||
|
const __$$context = this._context || this;
|
||||||
|
const { state } = __$$context;
|
||||||
|
return <Page><Text a={1}>Hello world!</Text></Page>;
|
||||||
|
",
|
||||||
|
"fileType": "jsx",
|
||||||
|
"linkAfter": Array [
|
||||||
|
"ReactComponentClassRenderStart",
|
||||||
|
"ReactComponentClassRenderPre",
|
||||||
|
],
|
||||||
|
"name": "ReactComponentClassRenderJSX",
|
||||||
|
"type": "string",
|
||||||
|
},
|
||||||
|
Object {
|
||||||
|
"content": "
|
||||||
|
function __$$eval(expr) {
|
||||||
|
try {
|
||||||
|
return expr();
|
||||||
|
} catch (error) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function __$$evalArray(expr) {
|
||||||
|
const res = __$$eval(expr);
|
||||||
|
return Array.isArray(res) ? res : [];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function __$$createChildContext(oldContext, ext) {
|
||||||
|
const childContext = {
|
||||||
|
...oldContext,
|
||||||
|
...ext,
|
||||||
|
};
|
||||||
|
childContext.__proto__ = oldContext;
|
||||||
|
return childContext;
|
||||||
|
}
|
||||||
|
",
|
||||||
|
"fileType": "jsx",
|
||||||
|
"linkAfter": Array [
|
||||||
|
"CommonFileExport",
|
||||||
|
],
|
||||||
|
"name": "CommonCustomContent",
|
||||||
|
"type": "string",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"contextData": Object {},
|
||||||
|
"depNames": Array [],
|
||||||
|
"ir": Object {
|
||||||
|
"children": Array [
|
||||||
|
Object {
|
||||||
|
"children": "Hello world!",
|
||||||
|
"componentName": "Text",
|
||||||
|
"props": Object {
|
||||||
|
"a": 1,
|
||||||
|
"a.b": 2,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"componentName": "Page",
|
||||||
|
"condition": null,
|
||||||
|
"containerType": "Page",
|
||||||
|
"fileName": "test",
|
||||||
|
"moduleName": "test",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|||||||
@ -83,4 +83,22 @@ describe('condition at root', () => {
|
|||||||
});
|
});
|
||||||
expect(result).toMatchSnapshot();
|
expect(result).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('invalid attr name should not be generated', async () => {
|
||||||
|
const containerIr: IContainerInfo = {
|
||||||
|
containerType: 'Page',
|
||||||
|
moduleName: 'test',
|
||||||
|
componentName: 'Page',
|
||||||
|
fileName: 'test',
|
||||||
|
condition: null,
|
||||||
|
children: [{ componentName: 'Text', children: 'Hello world!', props: { 'a': 1, 'a.b': 2 } }],
|
||||||
|
};
|
||||||
|
const result = await jsx()({
|
||||||
|
ir: containerIr,
|
||||||
|
contextData: {},
|
||||||
|
chunks: [],
|
||||||
|
depNames: [],
|
||||||
|
});
|
||||||
|
expect(result).toMatchSnapshot();
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user