GeezerVison is showing github.com — fix: 修正 react 框架出码中在严格模式对 methods 和 context 的处理 · jackingod/lowcode-engine@b1a6100
File treeExpand file treeCollapse file tree Open diff view settings modules/code-generator/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/react/containerInjectContext.ts +34-5Lines changed: 34 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,6 +10,7 @@ import { | 10 | 10 | ICodeStruct, | 11 | 11 | IContainerInfo, | 12 | 12 | } from '../../../types'; | | 13 | +import { DEFAULT_LINK_AFTER } from '../../../const'; | 13 | 14 |
| 14 | 15 | export interface PluginConfig { | 15 | 16 | fileType: string; | @@ -30,28 +31,56 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 30 | 31 | const scope = Scope.createRootScope(); | 31 | 32 |
| 32 | 33 | const { inStrictMode } = next.contextData; | 33 | | - if (inStrictMode) { | | 34 | + if (!inStrictMode) { | | 35 | + // 非严格模式下,上下文就是自己 | 34 | 36 | next.chunks.push({ | 35 | 37 | type: ChunkType.STRING, | 36 | 38 | fileType: cfg.fileType, | 37 | 39 | name: CLASS_DEFINE_CHUNK_NAME.InsVar, | 38 | 40 | content: ` | 39 | | - _context = this._createContext(); | | 41 | + _context = this; | 40 | 42 | `, | 41 | 43 | linkAfter: [CLASS_DEFINE_CHUNK_NAME.Start], | 42 | 44 | }); | 43 | | - // TODO: createContext...... | 44 | 45 | } else { | 45 | | - // 非严格模式下,上下文就是自己 | | 46 | + // 严格模式下的上下文只保留协议中规定的那些 | 46 | 47 | next.chunks.push({ | 47 | 48 | type: ChunkType.STRING, | 48 | 49 | fileType: cfg.fileType, | 49 | 50 | name: CLASS_DEFINE_CHUNK_NAME.InsVar, | 50 | 51 | content: ` | 51 | | - _context = this; | | 52 | + _context = this._createContext(); | 52 | 53 | `, | 53 | 54 | linkAfter: [CLASS_DEFINE_CHUNK_NAME.Start], | 54 | 55 | }); | | 56 | + | | 57 | + next.chunks.push({ | | 58 | + type: ChunkType.STRING, | | 59 | + fileType: cfg.fileType, | | 60 | + name: CLASS_DEFINE_CHUNK_NAME.InsPrivateMethod, | | 61 | + content: ` | | 62 | + _createContext() { | | 63 | + const self = this; | | 64 | + const context = { | | 65 | + get state() { return self.state; }, | | 66 | + setState(newState, callback) { self.setState(newState, callback); }, | | 67 | + get dataSourceMap() { return self._dataSourceEngine.dataSourceMap || {}; }, | | 68 | + async reloadDataSource() { await self._dataSourceEngine.reloadDataSource(); }, | | 69 | + get utils() { return self.utils; }, | | 70 | + get page() { return context; }, | | 71 | + get component() { return context; }, | | 72 | + get props() { return self.props; }, | | 73 | + get constants() { return self.constants; }, | | 74 | + get $() { return self.$ }, | | 75 | + get $$() { return self.$$ }, | | 76 | + ...this._methods, | | 77 | + }; | | 78 | + | | 79 | + return context; | | 80 | + } | | 81 | + `, | | 82 | + linkAfter: DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsPrivateMethod], | | 83 | + }); | 55 | 84 | } | 56 | 85 |
| 57 | 86 | return next; | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/react/containerMethod.ts +29-9Lines changed: 29 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,6 +11,7 @@ import { | 11 | 11 | ICodeStruct, | 12 | 12 | IContainerInfo, | 13 | 13 | } from '../../../types'; | | 14 | +import { isValidIdentifier } from '../../../utils/validate'; | 14 | 15 |
| 15 | 16 | export interface PluginConfig { | 16 | 17 | fileType: string; | @@ -28,18 +29,37 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 28 | 29 | }; | 29 | 30 |
| 30 | 31 | const ir = next.ir as IContainerInfo; | | 32 | + const { inStrictMode } = next.contextData; | 31 | 33 |
| 32 | | - if (ir.methods) { | 33 | | - const { methods } = ir; | 34 | | - const chunks = Object.keys(methods).map<ICodeChunk>((methodName) => ({ | | 34 | + if (!ir.methods) { | | 35 | + return next; | | 36 | + } | | 37 | + | | 38 | + // 将这些 methods 都定义到 class 上 | | 39 | + const { methods } = ir; | | 40 | + const chunks = Object.keys(methods).map<ICodeChunk>((methodName) => ({ | | 41 | + type: ChunkType.STRING, | | 42 | + fileType: cfg.fileType, | | 43 | + name: CLASS_DEFINE_CHUNK_NAME.InsMethod, | | 44 | + content: generateFunction(methods[methodName], { name: methodName, isMember: true }), | | 45 | + linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsMethod]], | | 46 | + })); | | 47 | + next.chunks.push(...chunks); | | 48 | + | | 49 | + // 严格模式下需要将这些方法都挂到上下文中 | | 50 | + if (inStrictMode) { | | 51 | + next.chunks.push({ | 35 | 52 | type: ChunkType.STRING, | 36 | 53 | fileType: cfg.fileType, | 37 | | - name: CLASS_DEFINE_CHUNK_NAME.InsMethod, | 38 | | - content: generateFunction(methods[methodName], { name: methodName, isMember: true }), | 39 | | - linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsMethod]], | 40 | | - })); | 41 | | - | 42 | | - next.chunks.push(...chunks); | | 54 | + name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent, | | 55 | + content: Object.keys(ir.methods) | | 56 | + .map((methodName) => | | 57 | + isValidIdentifier(methodName) ? `.${methodName}` : `[${JSON.stringify(methodName)}]`, | | 58 | + ) | | 59 | + .map((method) => `this._context${method} = this${method};`) | | 60 | + .join('\n'), | | 61 | + linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.ConstructorContent]], | | 62 | + }); | 43 | 63 | } | 44 | 64 |
| 45 | 65 | return next; | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/solutions/icejs.ts +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,8 +22,15 @@ import icejs from '../plugins/project/framework/icejs'; | 22 | 22 |
| 23 | 23 | import { prettier } from '../postprocessor'; | 24 | 24 |
| 25 | | -export default function createIceJsProjectBuilder(): IProjectBuilder { | | 25 | +export type IceJsProjectBuilderOptions = { | | 26 | + inStrictMode?: boolean; | | 27 | +}; | | 28 | + | | 29 | +export default function createIceJsProjectBuilder( | | 30 | + options?: IceJsProjectBuilderOptions, | | 31 | +): IProjectBuilder { | 26 | 32 | return createProjectBuilder({ | | 33 | + inStrictMode: options?.inStrictMode, | 27 | 34 | template: icejs.template, | 28 | 35 | plugins: { | 29 | 36 | components: [ | |
|