Skip to content

Commit 33efde9

Browse files
authored
fix: invalid jsx attr name error (alibaba#2131)
1 parent 622806f commit 33efde9

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

modules/code-generator/src/utils/nodeToJSX.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { executeFunctionStack } from './aopHelper';
1919
import { encodeJsxStringNode } from './encodeJsxAttrString';
2020
import { unwrapJsExprQuoteInJsx } from './jsxHelpers';
2121
import { transformThis2Context } from '../core/jsx/handlers/transformThis2Context';
22+
import { isValidIdentifier } from './validate';
2223

2324
function mergeNodeGeneratorConfig(
2425
cfg1: NodeGeneratorConfig,
@@ -126,11 +127,13 @@ function generateAttrs(
126127
if (props) {
127128
if (!Array.isArray(props)) {
128129
Object.keys(props).forEach((propName: string) => {
129-
pieces = pieces.concat(generateAttr(propName, props[propName] as IPublicTypeCompositeValue, scope, config));
130+
if (isValidIdentifier(propName)) {
131+
pieces = pieces.concat(generateAttr(propName, props[propName] as IPublicTypeCompositeValue, scope, config));
132+
}
130133
});
131134
} else {
132135
props.forEach((prop) => {
133-
if (prop.name && !prop.spread) {
136+
if (prop.name && isValidIdentifier(prop.name) && !prop.spread) {
134137
pieces = pieces.concat(generateAttr(prop.name, prop.value, scope, config));
135138
}
136139

modules/code-generator/tests/plugins/jsx/__snapshots__/p0-condition-at-root.test.ts.snap

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,75 @@ Object {
281281
},
282282
}
283283
`;
284+
285+
exports[`condition at root invalid attr name should not be generated 1`] = `
286+
Object {
287+
"chunks": Array [
288+
Object {
289+
"content": "
290+
const __$$context = this._context || this;
291+
const { state } = __$$context;
292+
return <Page><Text a={1}>Hello world!</Text></Page>;
293+
",
294+
"fileType": "jsx",
295+
"linkAfter": Array [
296+
"ReactComponentClassRenderStart",
297+
"ReactComponentClassRenderPre",
298+
],
299+
"name": "ReactComponentClassRenderJSX",
300+
"type": "string",
301+
},
302+
Object {
303+
"content": "
304+
function __$$eval(expr) {
305+
try {
306+
return expr();
307+
} catch (error) {
308+
309+
}
310+
}
311+
312+
function __$$evalArray(expr) {
313+
const res = __$$eval(expr);
314+
return Array.isArray(res) ? res : [];
315+
}
316+
317+
318+
function __$$createChildContext(oldContext, ext) {
319+
const childContext = {
320+
...oldContext,
321+
...ext,
322+
};
323+
childContext.__proto__ = oldContext;
324+
return childContext;
325+
}
326+
",
327+
"fileType": "jsx",
328+
"linkAfter": Array [
329+
"CommonFileExport",
330+
],
331+
"name": "CommonCustomContent",
332+
"type": "string",
333+
},
334+
],
335+
"contextData": Object {},
336+
"depNames": Array [],
337+
"ir": Object {
338+
"children": Array [
339+
Object {
340+
"children": "Hello world!",
341+
"componentName": "Text",
342+
"props": Object {
343+
"a": 1,
344+
"a.b": 2,
345+
},
346+
},
347+
],
348+
"componentName": "Page",
349+
"condition": null,
350+
"containerType": "Page",
351+
"fileName": "test",
352+
"moduleName": "test",
353+
},
354+
}
355+
`;

modules/code-generator/tests/plugins/jsx/p0-condition-at-root.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,22 @@ describe('condition at root', () => {
8383
});
8484
expect(result).toMatchSnapshot();
8585
});
86+
87+
test('invalid attr name should not be generated', async () => {
88+
const containerIr: IContainerInfo = {
89+
containerType: 'Page',
90+
moduleName: 'test',
91+
componentName: 'Page',
92+
fileName: 'test',
93+
condition: null,
94+
children: [{ componentName: 'Text', children: 'Hello world!', props: { 'a': 1, 'a.b': 2 } }],
95+
};
96+
const result = await jsx()({
97+
ir: containerIr,
98+
contextData: {},
99+
chunks: [],
100+
depNames: [],
101+
});
102+
expect(result).toMatchSnapshot();
103+
})
86104
});

0 commit comments

Comments
 (0)