diff --git a/ui2abc/libarkts/src-host/es2panda.ts b/ui2abc/libarkts/src-host/es2panda.ts index cfdf8aa4d5e195e305073ee1cf53e7e73423d3ad..2ec9164b0d3e243b071c19a904dcde2aaf3804ba 100644 --- a/ui2abc/libarkts/src-host/es2panda.ts +++ b/ui2abc/libarkts/src-host/es2panda.ts @@ -15,12 +15,12 @@ import * as fs from "node:fs" import * as path from "node:path" -import { checkSDK, arktsGlobal as global, ImportStorage, ETSModule, ProgramProvider, metaDatabase } from "@koalaui/libarkts" -import { CheckedBackFilter, ChainExpressionFilter, PluginContext, PluginContextImpl } from "@koalaui/libarkts" +import { checkSDK, arktsGlobal as global, ProgramProvider, metaDatabase } from "@koalaui/libarkts" +import { CheckedBackFilter, PluginContext, PluginContextImpl, commonPlugin } from "@koalaui/libarkts" import { Command } from "commander" import { filterSource, isNumber, throwError, withWarning } from "@koalaui/libarkts" import { Es2pandaContextState } from "@koalaui/libarkts" -import { AstNode, Config, Context, createETSModuleFromContext, inferVoidReturnType, proceedToState, ProgramTransformer, rebindSubtree, recheckSubtree, setBaseOverloads } from "@koalaui/libarkts" +import { AstNode, Config, Context, createETSModuleFromContext, proceedToState, ProgramTransformer, rebindSubtree, recheckSubtree } from "@koalaui/libarkts" function parseCommandLineArgs() { const commander = new Command() @@ -83,23 +83,8 @@ function insertPlugin( global.profiler.transformStarted() while (currentProgram) { - // IMPORTANT This transformations must be the same as in libarkts/plugin-utils, as if restart == false - // Please add changes to this code to plugin-utils too - // TODO refactor to use plugin-utils - - const ast = currentProgram.program.ast as ETSModule - const importStorage = new ImportStorage(currentProgram.program, state == Es2pandaContextState.ES2PANDA_STATE_PARSED) - stageSpecificPreFilters(ast, state) - - // console.log(`transform ${currentProgram.name}`) transform?.(currentProgram.program, { isMainProgram: currentProgram.program.peer == global.compilerContext.program.peer, name: currentProgram.name, stage: state }, context) - stageSpecificPostFilters(ast, state) - - setBaseOverloads(ast) - setAllParents(ast) - importStorage.update() - if (restart) { break } @@ -124,18 +109,6 @@ function insertPlugin( return script } -function stageSpecificPreFilters(script: AstNode, state: Es2pandaContextState) { - if (state == Es2pandaContextState.ES2PANDA_STATE_CHECKED) { - inferVoidReturnType(script) - } -} - -function stageSpecificPostFilters(script: AstNode, state: Es2pandaContextState) { - if (state == Es2pandaContextState.ES2PANDA_STATE_CHECKED) { - new ChainExpressionFilter().visitor(script) - } -} - function restartCompiler(source: string, configPath: string, filePath: string, stdlib: string, outputPath: string, verbose: boolean = true) { if (verbose) { console.log(`restarting with config ${configPath}, file ${filePath}`) @@ -283,10 +256,6 @@ function generateDeclFromCurrentContext(filePath: string): never { process.exit(0) } -function setAllParents(ast: AstNode) { - global.es2panda._AstNodeUpdateAll(global.context, ast.peer) -} - function loadPlugin(configDir: string, jsonPlugin: any) { const pluginPath = jsonPlugin.transform ?? throwError(`arktsconfig plugins objects should specify transform`) /** TODO: read and pass plugin options */ @@ -299,7 +268,7 @@ function loadPlugin(configDir: string, jsonPlugin: any) { function selectPlugins(configDir: string, plugins: any[], stage: string): ProgramTransformer[] | undefined { const selected = plugins .filter(it => (it.stage == stage)) - .map(it => loadPlugin(configDir, it)) + .map(it => commonPlugin(loadPlugin(configDir, it))) if (selected.length == 0) { return undefined } diff --git a/ui2abc/libarkts/src/arkts-api/CommonPlugin.ts b/ui2abc/libarkts/src/arkts-api/CommonPlugin.ts new file mode 100644 index 0000000000000000000000000000000000000000..bd5f13f41f4c4d8a9dd917d1ec6d9033970a92c8 --- /dev/null +++ b/ui2abc/libarkts/src/arkts-api/CommonPlugin.ts @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AstNode } from "./peers/AstNode" +import { Program } from "../generated" +import { CompilationOptions, PluginContext, ProgramTransformer } from "./plugins" +import { Es2pandaContextState } from "../generated/Es2pandaEnums" +import { ImportStorage } from "./ImportStorage" +import { setBaseOverloads } from "./SetBaseOverloads" +import { inferVoidReturnType } from "./InferVoidReturnType" +import { ChainExpressionFilter } from "./ChainExpressionFilter" +import { global } from "./static/global" + +export function commonPlugin( + plugin: ProgramTransformer +): ProgramTransformer { + return (program: Program, options: CompilationOptions, context: PluginContext) => { + // IMPORTANT This transformations must be the same as in libarkts/plugin-utils, as if restart == false + // Please add changes to this code to plugin-utils too + // TODO refactor to use plugin-utils + + if (context.parameter("restart")) { + plugin(program, options, context) + return + } + + const ast = program.ast + const importStorage = new ImportStorage(program, options.stage == Es2pandaContextState.ES2PANDA_STATE_PARSED) + stageSpecificPreFilters(ast, options.stage) + + plugin(program, options, context) + + stageSpecificPostFilters(ast, options.stage) + + setBaseOverloads(ast) + setAllParents(ast) + importStorage.update() + } +} + +function stageSpecificPreFilters(script: AstNode, state: Es2pandaContextState) { + if (state == Es2pandaContextState.ES2PANDA_STATE_CHECKED) { + inferVoidReturnType(script) + } +} + +function stageSpecificPostFilters(script: AstNode, state: Es2pandaContextState) { + if (state == Es2pandaContextState.ES2PANDA_STATE_CHECKED) { + new ChainExpressionFilter().visitor(script) + } +} + +function setAllParents(ast: AstNode) { + global.es2panda._AstNodeUpdateAll(global.context, ast.peer) +} diff --git a/ui2abc/libarkts/src/arkts-api/SetBaseOverloads.ts b/ui2abc/libarkts/src/arkts-api/SetBaseOverloads.ts index e0ddb219e2987f6b117ead5c9513f95067b7368a..f94e4fa1e759c89c40405014b3903be1924baae7 100644 --- a/ui2abc/libarkts/src/arkts-api/SetBaseOverloads.ts +++ b/ui2abc/libarkts/src/arkts-api/SetBaseOverloads.ts @@ -39,6 +39,6 @@ export class SetBaseOverloads extends AbstractVisitor { } } -export function setBaseOverloads(node: ETSModule) { +export function setBaseOverloads(node: AstNode) { new SetBaseOverloads().visitor(node) } diff --git a/ui2abc/libarkts/src/arkts-api/index.ts b/ui2abc/libarkts/src/arkts-api/index.ts index f64dbf1f6bf04648fcc73d74bc3e8b36643db272..e4eeae2c4ef0d00be7e2d212e1a4ecacb63bff71 100644 --- a/ui2abc/libarkts/src/arkts-api/index.ts +++ b/ui2abc/libarkts/src/arkts-api/index.ts @@ -28,6 +28,7 @@ export * from "./plugins" export * from "./ImportStorage" export * from "./InferVoidReturnType" export * from "./ProgramProvider" +export * from "./CommonPlugin" export * from "./peers/AstNode" export * from "./peers/Config" diff --git a/ui2abc/libarkts/src/index.ts b/ui2abc/libarkts/src/index.ts index f242ba396948061b69db03d54f9739aab6c7cc24..5c0cef0d9ad272386e61d0df5fd40c474f868f96 100644 --- a/ui2abc/libarkts/src/index.ts +++ b/ui2abc/libarkts/src/index.ts @@ -32,6 +32,7 @@ export * from "./arkts-api/ImportStorage" export * from "./arkts-api/InferVoidReturnType" export * from "./arkts-api/ProgramProvider" export * from "./arkts-api/node-utilities/Program" +export * from "./arkts-api/CommonPlugin" export * from "./arkts-api/peers/AstNode" export * from "./arkts-api/peers/Config"