GeezerVison is showing github.com — fix: invalid jsx attr name error (#2131) · newangle121/lowcode-engine@33efde9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/utils/nodeToJSX.ts +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,6 +19,7 @@ import { executeFunctionStack } from './aopHelper'; | 19 | 19 | import { encodeJsxStringNode } from './encodeJsxAttrString'; | 20 | 20 | import { unwrapJsExprQuoteInJsx } from './jsxHelpers'; | 21 | 21 | import { transformThis2Context } from '../core/jsx/handlers/transformThis2Context'; | | 22 | +import { isValidIdentifier } from './validate'; | 22 | 23 |
| 23 | 24 | function mergeNodeGeneratorConfig( | 24 | 25 | cfg1: NodeGeneratorConfig, | @@ -126,11 +127,13 @@ function generateAttrs( | 126 | 127 | if (props) { | 127 | 128 | if (!Array.isArray(props)) { | 128 | 129 | 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 | + } | 130 | 133 | }); | 131 | 134 | } else { | 132 | 135 | props.forEach((prop) => { | 133 | | - if (prop.name && !prop.spread) { | | 136 | + if (prop.name && isValidIdentifier(prop.name) && !prop.spread) { | 134 | 137 | pieces = pieces.concat(generateAttr(prop.name, prop.value, scope, config)); | 135 | 138 | } | 136 | 139 |
| |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/tests/plugins/jsx/__snapshots__/p0-condition-at-root.test.ts.snap +72Lines changed: 72 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -281,3 +281,75 @@ Object { | 281 | 281 | }, | 282 | 282 | } | 283 | 283 | `; | | 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 | +`; |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/tests/plugins/jsx/p0-condition-at-root.test.ts +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -83,4 +83,22 @@ describe('condition at root', () => { | 83 | 83 | }); | 84 | 84 | expect(result).toMatchSnapshot(); | 85 | 85 | }); | | 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 | + }) | 86 | 104 | }); |
|