From 3125b34802f945298db4d7f89cc5a1a17513818d Mon Sep 17 00:00:00 2001 From: Aleksandr Veselov Date: Tue, 19 Aug 2025 15:28:16 +0300 Subject: [PATCH] Add diagnostics and source position API --- ui2abc/libarkts/src/arkts-api/index.ts | 1 + .../src/arkts-api/peers/DiagnosticKind.ts | 24 ++++++++++++++ .../src/arkts-api/utilities/public.ts | 31 +++++++++++++++++-- ui2abc/libarkts/src/index.ts | 1 + 4 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 ui2abc/libarkts/src/arkts-api/peers/DiagnosticKind.ts diff --git a/ui2abc/libarkts/src/arkts-api/index.ts b/ui2abc/libarkts/src/arkts-api/index.ts index e2e1c3c204..874d5f2443 100644 --- a/ui2abc/libarkts/src/arkts-api/index.ts +++ b/ui2abc/libarkts/src/arkts-api/index.ts @@ -32,6 +32,7 @@ export * from "./peers/Context" export { GlobalContext } from "./peers/Context" export * from "./peers/ExternalSource" export * from "./peers/Options" +export * from "./peers/DiagnosticKind" export * from "./node-utilities/ArkTsConfig" export * from "./node-utilities/Program" export * from "./peers/ImportPathManager" diff --git a/ui2abc/libarkts/src/arkts-api/peers/DiagnosticKind.ts b/ui2abc/libarkts/src/arkts-api/peers/DiagnosticKind.ts new file mode 100644 index 0000000000..5f93fb1397 --- /dev/null +++ b/ui2abc/libarkts/src/arkts-api/peers/DiagnosticKind.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 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 { KNativePointer } from "@koalaui/interop"; +import { ArktsObject } from "./ArktsObject" + + +export class DiagnosticKind extends ArktsObject { + constructor(peer: KNativePointer) { + super(peer); + } +} diff --git a/ui2abc/libarkts/src/arkts-api/utilities/public.ts b/ui2abc/libarkts/src/arkts-api/utilities/public.ts index 7e7ea36390..8df4598557 100644 --- a/ui2abc/libarkts/src/arkts-api/utilities/public.ts +++ b/ui2abc/libarkts/src/arkts-api/utilities/public.ts @@ -16,10 +16,10 @@ import { global } from "../static/global" import { isNumber, throwError, withWarning } from "../../utils" import { KNativePointer, nullptr, KInt, KUInt} from "@koalaui/interop" -import { passNode, unpackNodeArray, unpackNonNullableNode, passString, unpackString } from "./private" -import { Es2pandaContextState, Es2pandaModifierFlags, Es2pandaMethodDefinitionKind, Es2pandaAstNodeType } from "../../generated/Es2pandaEnums" +import { passNode, unpackNodeArray, unpackNonNullableNode, passString, unpackString, passStringArray } from "./private" +import { Es2pandaContextState, Es2pandaModifierFlags, Es2pandaMethodDefinitionKind, Es2pandaAstNodeType, Es2pandaPluginDiagnosticType } from "../../generated/Es2pandaEnums" import type { AstNode } from "../peers/AstNode" -import { SourcePosition } from "../../generated" +import { DiagnosticInfo, SourcePosition, SourceRange, SuggestionInfo } from "../../generated" import { type AnnotationUsage, ClassDefinition, @@ -47,6 +47,7 @@ import { Context } from "../peers/Context" import { NodeCache } from "../node-cache" import { factory } from "../factory/nodeFactory" import { traceGlobal } from "../../tracer" +import { DiagnosticKind } from "../peers/DiagnosticKind" /** * Improve: Replace or remove with better naming @@ -404,3 +405,27 @@ export function createTypeNodeFromTsType(node: AstNode): AstNode | undefined { export function createSourcePosition(index: KUInt, line: KUInt): SourcePosition { return new SourcePosition(global.generatedEs2panda._CreateSourcePosition(global.context, index, line)) } + +export function createSourceRange(start: SourcePosition, end: SourcePosition): SourceRange { + return new SourceRange(global.generatedEs2panda._CreateSourceRange(global.context, start.peer, end.peer)) +} + +export function createDiagnosticInfo(kind: DiagnosticKind, position: SourcePosition, ...args: string[]): DiagnosticInfo { + return new DiagnosticInfo(global.es2panda._CreateDiagnosticInfo(global.context, kind.peer, passStringArray(args), args.length, position.peer)) +} + +export function createSuggestionInfo(kind: DiagnosticKind, substitutionCode: string, title: string, range: SourceRange, ...args: string[]): SuggestionInfo { + return new SuggestionInfo(global.es2panda._CreateSuggestionInfo(global.context, kind.peer, passStringArray(args), args.length, substitutionCode, title, range.peer)) +} + +export function createDiagnosticKind(message: string, type: Es2pandaPluginDiagnosticType): DiagnosticKind { + return new DiagnosticKind(global.es2panda._CreateDiagnosticKind(global.context, message, type)); +} + +export function logDiagnostic(kind: DiagnosticKind, pos: SourcePosition, ...args: string[]): void { + global.es2panda._LogDiagnostic(global.context, kind.peer, passStringArray(args), args.length, pos.peer); +} + +export function logDiagnosticWithSuggestion(diagnosticInfo: DiagnosticInfo, suggestionInfo: SuggestionInfo): void { + global.generatedEs2panda._LogDiagnosticWithSuggestion(global.context, diagnosticInfo.peer, suggestionInfo.peer); +} diff --git a/ui2abc/libarkts/src/index.ts b/ui2abc/libarkts/src/index.ts index 2c45b5cac9..4c264e21b0 100644 --- a/ui2abc/libarkts/src/index.ts +++ b/ui2abc/libarkts/src/index.ts @@ -38,6 +38,7 @@ export { GlobalContext } from "./arkts-api/peers/Context" export * from "./arkts-api/peers/ExternalSource" export * from "./arkts-api/peers/ImportPathManager" export * from "./arkts-api/peers/Options" +export * from "./arkts-api/peers/DiagnosticKind" export { global as arktsGlobal } from "./arkts-api/static/global" export * from "./arkts-api/static/globalUtils" export * as arkts from "./arkts-api" -- Gitee