From 9dd6acaf79b3c94b896a4c19a69f0dd2cb212598 Mon Sep 17 00:00:00 2001 From: dongchao Date: Mon, 7 Jul 2025 17:16:51 +0800 Subject: [PATCH] Achieve record scene in declgen_ets2ts Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICB1LU Signed-off-by: dongchao Change-Id: Ibf08d204b511bb4ec99206187b44a5e6b42c299e --- ets2panda/declgen_ets2ts/declgenEts2Ts.cpp | 12 +++++++++++ ets2panda/declgen_ets2ts/declgenEts2Ts.h | 2 ++ .../build_system/src/build/declgen_worker.ts | 14 +++++++++++-- .../driver/build_system/src/pre_define.ts | 7 +++++++ ets2panda/driver/build_system/src/utils.ts | 21 ++++++++++++++++++- ets2panda/public/es2panda_lib.cpp | 4 +++- 6 files changed, 56 insertions(+), 4 deletions(-) diff --git a/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp b/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp index 7b37223644..1059328f51 100644 --- a/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp +++ b/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp @@ -292,6 +292,11 @@ void TSDeclGen::GenImportDeclarations() } } +void TSDeclGen::GenImportRecordDeclarations(const std::string &source) +{ + OutDts("import { Record } from \"", source, "\";\n"); +} + template void TSDeclGen::GenSeparated(const T &container, const CB &cb, const char *separator, bool isReExport, bool isDtsExport) { @@ -2230,6 +2235,13 @@ bool GenerateTsDeclarations(checker::ETSChecker *checker, const ark::es2panda::p std::string combineEts = importOutputEts + outputEts; std::string combinedDEts = importOutputDEts + outputDEts; + if (!declBuilder.GetDeclgenOptions().recordFile.empty()) { + declBuilder.ResetDtsOutput(); + declBuilder.GenImportRecordDeclarations(); + std::string recordImportOutputDEts = declBuilder.GetDtsOutput(); + combinedDEts = recordImportOutputDEts + combinedDEts; + } + if (!declBuilder.GetDeclgenOptions().outputDeclEts.empty()) { auto outDtsPath = declBuilder.GetDeclgenOptions().outputDeclEts; if (!WriteToFile(outDtsPath, combinedDEts, checker)) { diff --git a/ets2panda/declgen_ets2ts/declgenEts2Ts.h b/ets2panda/declgen_ets2ts/declgenEts2Ts.h index 850722c6f0..b8ddf4f311 100644 --- a/ets2panda/declgen_ets2ts/declgenEts2Ts.h +++ b/ets2panda/declgen_ets2ts/declgenEts2Ts.h @@ -31,6 +31,7 @@ struct DeclgenOptions { bool isolated = false; std::string outputDeclEts; std::string outputEts; + std::string recordFile; }; // Consume program after checker stage and generate out_path typescript file with declarations @@ -65,6 +66,7 @@ public: bool Generate(); void GenImportDeclarations(); + void GenImportRecordDeclarations(const std::string &source) std::string GetDtsOutput() const { diff --git a/ets2panda/driver/build_system/src/build/declgen_worker.ts b/ets2panda/driver/build_system/src/build/declgen_worker.ts index 872eb49db1..a739e071c0 100644 --- a/ets2panda/driver/build_system/src/build/declgen_worker.ts +++ b/ets2panda/driver/build_system/src/build/declgen_worker.ts @@ -18,11 +18,13 @@ import { BuildConfig } from '../types'; import { Logger } from '../logger'; import * as fs from 'fs'; import * as path from 'path'; -import { changeFileExtension, ensurePathExists } from '../utils'; +import { changeFileExtension, createFileIfNotExists, ensurePathExists } from '../utils'; import { DECL_ETS_SUFFIX, TS_SUFFIX, - KOALA_WRAPPER_PATH_FROM_SDK + KOALA_WRAPPER_PATH_FROM_SDK, + STATIC_RECORD_FILE, + STATIC_RECORD_FILE_CONTENT } from '../pre_define'; import { PluginDriver, PluginHook } from '../plugins/plugins_driver'; @@ -67,6 +69,14 @@ process.on('message', (message: { ensurePathExists(declEtsOutputPath); ensurePathExists(etsOutputPath); + const staticRecordPath = path.join( + moduleInfo.declgenV1OutPath as string, + STATIC_RECORD_FILE + ) + const staticRecordRelativePath = path.relative(staticRecordPath, declEtsOutputPath); + console.log("dongchao"); + createFileIfNotExists(staticRecordPath, STATIC_RECORD_FILE_CONTENT); + arktsGlobal.filePath = fileInfo.filePath; arktsGlobal.config = arkts.Config.create([ '_', diff --git a/ets2panda/driver/build_system/src/pre_define.ts b/ets2panda/driver/build_system/src/pre_define.ts index 3879de18ee..c693475f23 100644 --- a/ets2panda/driver/build_system/src/pre_define.ts +++ b/ets2panda/driver/build_system/src/pre_define.ts @@ -19,6 +19,7 @@ export const LINKER_INPUT_FILE: string = 'fileInfo.txt'; export const DEPENDENCY_INPUT_FILE: string = 'dependencyFileInfo.txt'; export const DEPENDENCY_JSON_FILE: string = 'dependency.json'; export const PROJECT_BUILD_CONFIG_FILE: string = 'projectionConfig.json'; +export const STATIC_RECORD_FILE: string = 'RECORD.d.ts'; export const DECL_ETS_SUFFIX: string = '.d.ets'; export const ETS_SUFFIX: string = '.ets'; @@ -46,3 +47,9 @@ export const NATIVE_MODULE: Set = new Set( ['system.app', 'ohos.app', 'system.router', 'system.curves', 'ohos.curves', 'system.matrix4', 'ohos.matrix4']); export const ARKTS_MODULE_NAME: string = 'arkts'; + +export const STATIC_RECORD_FILE_CONTENT: string = `// generated for static Record +export type Record = { + [P in K]: T; +}; +`; diff --git a/ets2panda/driver/build_system/src/utils.ts b/ets2panda/driver/build_system/src/utils.ts index 429d5c3498..c884c16509 100644 --- a/ets2panda/driver/build_system/src/utils.ts +++ b/ets2panda/driver/build_system/src/utils.ts @@ -147,4 +147,23 @@ export function isSubPathOf(targetPath: string, parentDir: string): boolean { const resolvedParent = toUnixPath(path.resolve(parentDir)); const resolvedTarget = toUnixPath(path.resolve(targetPath)); return resolvedTarget === resolvedParent || resolvedTarget.startsWith(resolvedParent + '/'); -} \ No newline at end of file +} + +export function createFileIfNotExists(filePath: string, content: string): boolean { + try { + const normalizedPath = path.normalize(filePath); + if (fs.existsSync(normalizedPath)) { + return false; + } + + const dir = path.dirname(normalizedPath); + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }); + } + + fs.writeFileSync(normalizedPath, content, { encoding: 'utf-8' }); + return true; + } catch (error) { + return false; + } +} diff --git a/ets2panda/public/es2panda_lib.cpp b/ets2panda/public/es2panda_lib.cpp index 0b0638b0f7..9cc6f40ecc 100644 --- a/ets2panda/public/es2panda_lib.cpp +++ b/ets2panda/public/es2panda_lib.cpp @@ -1243,7 +1243,8 @@ extern "C" es2panda_AstNode **AllDeclarationsByNameFromProgram([[maybe_unused]] extern "C" __attribute__((unused)) int GenerateTsDeclarationsFromContext(es2panda_Context *ctx, const char *outputDeclEts, const char *outputEts, bool exportAll, - bool isolated) + bool isolated, + const char *recordFile) { auto *ctxImpl = reinterpret_cast(ctx); auto *checker = reinterpret_cast(ctxImpl->GetChecker()); @@ -1253,6 +1254,7 @@ extern "C" __attribute__((unused)) int GenerateTsDeclarationsFromContext(es2pand declgenOptions.outputDeclEts = outputDeclEts ? outputDeclEts : ""; declgenOptions.outputEts = outputEts ? outputEts : ""; declgenOptions.isolated = isolated; + declgenOptions.recordFile = recordFile ? recordFile : ""; return ark::es2panda::declgen_ets2ts::GenerateTsDeclarations(checker, ctxImpl->parserProgram, declgenOptions) ? 0 : 1; -- Gitee