From 7e37f8dd16f0ab5371b6174f1bcf7c6c0a884305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=89=A7=E6=AF=85?= Date: Thu, 12 Nov 2020 20:48:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20schema=20=E4=B8=AD?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=20state=20=E7=9A=84=E5=AE=9A=E4=B9=89,=20?= =?UTF-8?q?=E5=87=BA=E7=A0=81=E5=90=8E=E7=9A=84=20Rax/React=20=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E5=BA=94=E6=9C=89=E4=B8=AA=E9=BB=98=E8=AE=A4=E7=9A=84?= =?UTF-8?q?=E7=A9=BA=E7=9A=84=20state?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../component/rax/containerInitState.ts | 48 +++++++++---------- .../component/react/containerInitState.ts | 45 ++++++++--------- .../demo-project/src/pages/Home/index.jsx | 2 + .../demo-project/src/pages/Home/index.jsx | 2 + 4 files changed, 48 insertions(+), 49 deletions(-) diff --git a/packages/code-generator/src/plugins/component/rax/containerInitState.ts b/packages/code-generator/src/plugins/component/rax/containerInitState.ts index 9974281bb..8410105de 100644 --- a/packages/code-generator/src/plugins/component/rax/containerInitState.ts +++ b/packages/code-generator/src/plugins/component/rax/containerInitState.ts @@ -32,33 +32,31 @@ const pluginFactory: BuilderComponentPluginFactory = (config?) => const ir = next.ir as IContainerInfo; const scope = Scope.createRootScope(); - if (ir.state) { - const state = ir.state; - const fields = Object.keys(state).map((stateName) => { - // TODO: 这里用什么 handlers? - const value = generateCompositeType(state[stateName], scope); - return `${stateName}: ${value}`; - }); + const state = ir.state || {}; + const fields = Object.keys(state).map((stateName) => { + // TODO: 这里用什么 handlers? + const value = generateCompositeType(state[stateName], scope); + return `${stateName}: ${value}`; + }); - if (cfg.implementType === 'inConstructor') { - next.chunks.push({ - type: ChunkType.STRING, - fileType: cfg.fileType, - name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent, - content: `this.state = { ${fields.join(',')} };`, - linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.ConstructorContent]], - }); - } else if (cfg.implementType === 'insMember') { - next.chunks.push({ - type: ChunkType.STRING, - fileType: cfg.fileType, - name: CLASS_DEFINE_CHUNK_NAME.InsVar, - content: `state = { ${fields.join(',')} };`, - linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsVar]], - }); - } - // TODO: hooks state?? + if (cfg.implementType === 'inConstructor') { + next.chunks.push({ + type: ChunkType.STRING, + fileType: cfg.fileType, + name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent, + content: `this.state = { ${fields.join(',')} };`, + linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.ConstructorContent]], + }); + } else if (cfg.implementType === 'insMember') { + next.chunks.push({ + type: ChunkType.STRING, + fileType: cfg.fileType, + name: CLASS_DEFINE_CHUNK_NAME.InsVar, + content: `state = { ${fields.join(',')} };`, + linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsVar]], + }); } + // TODO: hooks state?? return next; }; diff --git a/packages/code-generator/src/plugins/component/react/containerInitState.ts b/packages/code-generator/src/plugins/component/react/containerInitState.ts index ddfaa3677..c4625a304 100644 --- a/packages/code-generator/src/plugins/component/react/containerInitState.ts +++ b/packages/code-generator/src/plugins/component/react/containerInitState.ts @@ -32,32 +32,29 @@ const pluginFactory: BuilderComponentPluginFactory = (config?) => const ir = next.ir as IContainerInfo; const scope = Scope.createRootScope(); - if (ir.state) { - const { state } = ir; - const fields = Object.keys(state).map((stateName) => { - const value = generateCompositeType(state[stateName], scope); - return `${stateName}: ${value},`; + const state = ir.state || {}; + const fields = Object.keys(state).map((stateName) => { + const value = generateCompositeType(state[stateName], scope); + return `${stateName}: ${value},`; + }); + + if (cfg.implementType === 'inConstructor') { + next.chunks.push({ + type: ChunkType.STRING, + fileType: cfg.fileType, + name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent, + content: `this.state = { ${fields.join('')} };`, + linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.ConstructorContent]], + }); + } else if (cfg.implementType === 'insMember') { + next.chunks.push({ + type: ChunkType.STRING, + fileType: cfg.fileType, + name: CLASS_DEFINE_CHUNK_NAME.InsVar, + content: `state = { ${fields.join('')} };`, + linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsVar]], }); - - if (cfg.implementType === 'inConstructor') { - next.chunks.push({ - type: ChunkType.STRING, - fileType: cfg.fileType, - name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent, - content: `this.state = { ${fields.join('')} };`, - linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.ConstructorContent]], - }); - } else if (cfg.implementType === 'insMember') { - next.chunks.push({ - type: ChunkType.STRING, - fileType: cfg.fileType, - name: CLASS_DEFINE_CHUNK_NAME.InsVar, - content: `state = { ${fields.join('')} };`, - linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsVar]], - }); - } } - return next; }; return plugin; diff --git a/packages/code-generator/test-cases/rax-app/demo1/expected/demo-project/src/pages/Home/index.jsx b/packages/code-generator/test-cases/rax-app/demo1/expected/demo-project/src/pages/Home/index.jsx index fbd8ec489..ef9d5e786 100644 --- a/packages/code-generator/test-cases/rax-app/demo1/expected/demo-project/src/pages/Home/index.jsx +++ b/packages/code-generator/test-cases/rax-app/demo1/expected/demo-project/src/pages/Home/index.jsx @@ -20,6 +20,8 @@ import __$$projectUtils from '../../utils'; import './index.css'; class Home$$Page extends Component { + state = {}; + _methods = this._defineMethods(); _context = this._createContext(); diff --git a/packages/code-generator/test-cases/rax-app/demo5/expected/demo-project/src/pages/Home/index.jsx b/packages/code-generator/test-cases/rax-app/demo5/expected/demo-project/src/pages/Home/index.jsx index fbbe04789..f8dac19f7 100644 --- a/packages/code-generator/test-cases/rax-app/demo5/expected/demo-project/src/pages/Home/index.jsx +++ b/packages/code-generator/test-cases/rax-app/demo5/expected/demo-project/src/pages/Home/index.jsx @@ -20,6 +20,8 @@ import __$$projectUtils from '../../utils'; import './index.css'; class Home$$Page extends Component { + state = {}; + _methods = this._defineMethods(); _context = this._createContext();