diff --git a/arkoala-arkts/libarkts/.gitlab-ci.yml b/arkoala-arkts/libarkts/.gitlab-ci.yml index baadfeb5d79ddc155ce57b612ff313d01bbdf54a..2c909b9de8c117ab88a2d28f1367b11c0be308d5 100644 --- a/arkoala-arkts/libarkts/.gitlab-ci.yml +++ b/arkoala-arkts/libarkts/.gitlab-ci.yml @@ -29,7 +29,6 @@ build plugin-api: test plugin-api: stage: test - allow_failure: true interruptible: true extends: .linux-vm-shell-task before_script: diff --git a/arkoala-arkts/libarkts/compatible/src/AbstractVisitor.ts b/arkoala-arkts/libarkts/compatible/src/AbstractVisitor.ts index 7d8dddbaa0a7a01c3d76f0593f0fbdd60feacb9b..0fdac256ad94387345cd8d1534b7e9b44e973cab 100644 --- a/arkoala-arkts/libarkts/compatible/src/AbstractVisitor.ts +++ b/arkoala-arkts/libarkts/compatible/src/AbstractVisitor.ts @@ -12,12 +12,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* -import * as arkts from "../../src/arkts" + +import * as ts from "../../src/api" export abstract class AbstractVisitor { constructor( - public ctx?: arkts.TransformationContext + public ctx?: ts.TransformationContext ) {} indentation = 0 @@ -29,15 +29,14 @@ export abstract class AbstractVisitor { return result } - abstract visitor(node: arkts.Node): arkts.Node + abstract visitor(node: ts.Node): ts.Node - visitEachChild(node: T): T { + visitEachChild(node: T): T { return this.withIndentation(() => - arkts.visitEachChild( + ts.visitEachChild( node, it => this.visitor(it) ) ) } } -*/ \ No newline at end of file diff --git a/arkoala-arkts/libarkts/compatible/src/analysis-visitor.ts b/arkoala-arkts/libarkts/compatible/src/analysis-visitor.ts index 41910f71f880ae8099bd32ce4f0565835175724a..f7c1f48a37d7dcfcb54b55a6953d0d51c9fb3d67 100644 --- a/arkoala-arkts/libarkts/compatible/src/analysis-visitor.ts +++ b/arkoala-arkts/libarkts/compatible/src/analysis-visitor.ts @@ -13,7 +13,7 @@ * limitations under the License. */ /* -import * as arkts from "../../src/arkts" +import * as ts from "../../src/api" import { AbstractVisitor } from "./AbstractVisitor"; import { Rewrite } from './transformation-context'; @@ -52,7 +52,7 @@ export class AnalysisVisitor extends AbstractVisitor { // public typechecker: arkts.TypeChecker, // public sourceFile: arkts.SourceFile, public rewrite: Rewrite, - ctx?: arkts.TransformationContext + ctx?: ts.TransformationContext ) { super(ctx) } @@ -63,7 +63,7 @@ export class AnalysisVisitor extends AbstractVisitor { this.tracer.trace(msg) } - traceAnnotation(nodeName: string, kind: FunctionKind, node: arkts.Node) { + traceAnnotation(nodeName: string, kind: FunctionKind, node: ts.Node) { if (kind) { this.trace(`${nodeName} HAS annotation: ${asString(node)} to ${FunctionKind[kind]}`) } else { @@ -72,55 +72,55 @@ export class AnalysisVisitor extends AbstractVisitor { } // TODO: get real annotation - getFunctionKindByIdent(node: arkts.Identifier | undefined, traceAnnotation: string): FunctionKind { + getFunctionKindByIdent(node: ts.Identifier | undefined, traceAnnotation: string): FunctionKind { if (node === undefined) { return FunctionKind.REGULAR } - if (!arkts.isIdentifier(node)) { - arkts.throwError('node expected to be Identifier') + if (!ts.isIdentifier(node)) { + ts.throwError('node expected to be Identifier') } const kind = parseComment(node.text) this.traceAnnotation(traceAnnotation, kind, node) return kind } - updateFunctionTableByKind(node: arkts.SignatureDeclarationBase, kind: FunctionKind): void { + updateFunctionTableByKind(node: ts.SignatureDeclarationBase, kind: FunctionKind): void { if (kind === FunctionKind.REGULAR) { return } this.rewrite.functionTable.set(node, kind) } - updateFunctionTableByIdent(node: arkts.SignatureDeclarationBase, ident: arkts.Identifier | undefined, traceMessage: string): void { + updateFunctionTableByIdent(node: ts.SignatureDeclarationBase, ident: ts.Identifier | undefined, traceMessage: string): void { if (ident === undefined) { return } this.updateFunctionTableByKind(node, this.getFunctionKindByIdent(ident, traceMessage)) } - updateVariableTableByKind(node: arkts.VariableLikeDeclaration, kind: FunctionKind): void { + updateVariableTableByKind(node: ts.VariableLikeDeclaration, kind: FunctionKind): void { if (kind === FunctionKind.REGULAR) { return } this.rewrite.variableTable.set(node, kind) } - updateVariableTableByIdent(node: arkts.VariableLikeDeclaration, ident: arkts.Identifier, traceMessage: string): void { + updateVariableTableByIdent(node: ts.VariableLikeDeclaration, ident: ts.Identifier, traceMessage: string): void { this.updateVariableTableByKind(node, this.getFunctionKindByIdent(ident, traceMessage)) } - updateCallTableByKind(node: arkts.CallExpression, kind: FunctionKind): void { + updateCallTableByKind(node: ts.CallExpression, kind: FunctionKind): void { if (kind === FunctionKind.REGULAR) { return } this.rewrite.callTable.set(node, kind) } - updateCallTableByIdent(node: arkts.CallExpression, ident: arkts.Identifier, traceMessage: string): void { + updateCallTableByIdent(node: ts.CallExpression, ident: ts.Identifier, traceMessage: string): void { this.updateCallTableByKind(node, this.getFunctionKindByIdent(ident, traceMessage)) } - isMemoEntry(node: arkts.CallExpression): boolean { + isMemoEntry(node: ts.CallExpression): boolean { const enclosingFunction = findFunctionDeclaration(node) if (enclosingFunction === undefined) { return false @@ -128,10 +128,10 @@ export class AnalysisVisitor extends AbstractVisitor { return this.getFunctionKindByIdent(enclosingFunction.name, "isMemoEntry") === FunctionKind.MEMO } - processCallExpression(node: arkts.CallExpression): void { - if (arkts.isIdentifier(node.expression)) { + processCallExpression(node: ts.CallExpression): void { + if (ts.isIdentifier(node.expression)) { this.updateCallTableByIdent(node, node.expression, "CallExpression") - } else if (arkts.isPropertyAccessExpression(node.expression)) { + } else if (ts.isPropertyAccessExpression(node.expression)) { // TODO: fix maybe (copied from compiler-plugin) const kind = this.getFunctionKindByIdent(node.expression.name, "CallExpression") if (kind !== FunctionKind.REGULAR) { @@ -144,111 +144,111 @@ export class AnalysisVisitor extends AbstractVisitor { } } - processFunctionDeclaration(node: arkts.FunctionDeclaration): void { + processFunctionDeclaration(node: ts.FunctionDeclaration): void { this.updateFunctionTableByIdent(node, node.name, "FunctionDeclaration") } - processMethodDeclaration(node: arkts.MethodDeclaration): void { + processMethodDeclaration(node: ts.MethodDeclaration): void { this.updateFunctionTableByIdent(node, node.name, "MethodDeclaration") } // TODO: unavailable now (ArrowFunction doesn't have a name) - processArrowFunction(node: arkts.ArrowFunction): void { + processArrowFunction(node: ts.ArrowFunction): void { // this.updateFunctionTableByIdent(node, node.name, "ArrowFunction") return } - processFunctionExpression(node: arkts.FunctionExpression): void { + processFunctionExpression(node: ts.FunctionExpression): void { this.updateFunctionTableByIdent(node, node.name, "MethodDeclaration") } - processParameter(node: arkts.ParameterDeclaration): void { + processParameter(node: ts.ParameterDeclaration): void { this.updateVariableTableByIdent(node, node.name, "ParameterDeclaration") } - processVariableDeclaration(node: arkts.VariableDeclaration): void { + processVariableDeclaration(node: ts.VariableDeclaration): void { this.updateVariableTableByIdent(node, node.name, "ParameterDeclaration") } - processVariableDeclarationList(node: arkts.VariableDeclarationList): void { + processVariableDeclarationList(node: ts.VariableDeclarationList): void { node.declarations.forEach(declaration => { this.processVariableDeclaration(declaration) }) } - processPropertyDeclaration(node: arkts.PropertyDeclaration): void { + processPropertyDeclaration(node: ts.PropertyDeclaration): void { // TODO: support non Identifier properties - this.updateVariableTableByIdent(node, node.name as arkts.Identifier, "PropertyDeclaration") + this.updateVariableTableByIdent(node, node.name as ts.Identifier, "PropertyDeclaration") } - processPropertySignature(node: arkts.PropertySignature): void { + processPropertySignature(node: ts.PropertySignature): void { if (node.name === undefined) { return } // TODO: support non Identifier properties - this.updateVariableTableByIdent(node, node.name as arkts.Identifier, "PropertySignature") + this.updateVariableTableByIdent(node, node.name as ts.Identifier, "PropertySignature") } - processFunctionTypeNode(node: arkts.FunctionTypeNode): void { + processFunctionTypeNode(node: ts.FunctionTypeNode): void { if (node.name === undefined) { return } // TODO: support non Identifier properties - this.updateFunctionTableByIdent(node, node.name as arkts.Identifier, "FunctionTypeNode") + this.updateFunctionTableByIdent(node, node.name as ts.Identifier, "FunctionTypeNode") } - processMethodSignature(node: arkts.MethodSignature): void { + processMethodSignature(node: ts.MethodSignature): void { if (node.name === undefined) { return } // TODO: support non Identifier properties - this.updateFunctionTableByIdent(node, node.name as arkts.Identifier, "MethodSignature") + this.updateFunctionTableByIdent(node, node.name as ts.Identifier, "MethodSignature") } - processGetAccessorDeclaration(node: arkts.GetAccessorDeclaration): void { + processGetAccessorDeclaration(node: ts.GetAccessorDeclaration): void { this.updateFunctionTableByIdent(node, node.name, "GetAccessorDeclaration") } - processSetAccessorDeclaration(node: arkts.SetAccessorDeclaration): void { + processSetAccessorDeclaration(node: ts.SetAccessorDeclaration): void { this.updateFunctionTableByIdent(node, node.name, "SetAccessorDeclaration") } - visitor(node: arkts.Node): arkts.Node { + visitor(node: ts.Node): ts.Node { // TODO: comparison shouldn't work now - if (arkts.getOriginalNode(node) !== node) { + if (ts.getOriginalNode(node) !== node) { throw new Error("Analysis phase is expected to work on original nodes") } - if (arkts.isCallExpression(node)) { + if (ts.isCallExpression(node)) { this.processCallExpression(node) - } else if (arkts.isFunctionDeclaration(node)) { + } else if (ts.isFunctionDeclaration(node)) { this.processFunctionDeclaration(node) - } else if (arkts.isMethodDeclaration(node)) { + } else if (ts.isMethodDeclaration(node)) { this.processMethodDeclaration(node) - } else if (arkts.isArrowFunction(node)) { + } else if (ts.isArrowFunction(node)) { this.processArrowFunction(node) - } else if (arkts.isFunctionExpression(node)) { + } else if (ts.isFunctionExpression(node)) { this.processFunctionExpression(node) - } else if (arkts.isParameter(node)) { + } else if (ts.isParameter(node)) { this.processParameter(node) } // else if (arkts.isVariableDeclaration(node)) { // this.processVariableDeclaration(node) // } - else if (arkts.isVariableDeclarationList(node)) { + else if (ts.isVariableDeclarationList(node)) { this.processVariableDeclarationList(node) } - else if (arkts.isPropertyDeclaration(node)) { + else if (ts.isPropertyDeclaration(node)) { this.processPropertyDeclaration(node) - } else if (arkts.isPropertySignature(node)) { + } else if (ts.isPropertySignature(node)) { this.processPropertySignature(node) - } else if (arkts.isFunctionTypeNode(node)) { + } else if (ts.isFunctionTypeNode(node)) { this.processFunctionTypeNode(node) - } else if (arkts.isMethodSignature(node)) { + } else if (ts.isMethodSignature(node)) { this.processMethodSignature(node) - } else if (arkts.isGetAccessorDeclaration(node)) { + } else if (ts.isGetAccessorDeclaration(node)) { this.processGetAccessorDeclaration(node) - } else if (arkts.isSetAccessorDeclaration(node)) { + } else if (ts.isSetAccessorDeclaration(node)) { this.processSetAccessorDeclaration(node) } diff --git a/arkoala-arkts/libarkts/compatible/src/builder-lambda-transformer.ts b/arkoala-arkts/libarkts/compatible/src/builder-lambda-transformer.ts index 0a72cbefaa53e394db48ed77884a3f3e461ff769..9aaac429cc2723af842c432ba5b20ebb3e9e787c 100644 --- a/arkoala-arkts/libarkts/compatible/src/builder-lambda-transformer.ts +++ b/arkoala-arkts/libarkts/compatible/src/builder-lambda-transformer.ts @@ -12,13 +12,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* -import * as arkts from "../../src/arkts" + +import * as ts from "../../src/api" import { AbstractVisitor } from "./AbstractVisitor"; export class BuilderLambdaTransformer extends AbstractVisitor { constructor( - ctx?: arkts.TransformationContext + ctx?: ts.TransformationContext ) { super(ctx) } @@ -26,38 +26,38 @@ export class BuilderLambdaTransformer extends AbstractVisitor { private static readonly builderLambdaPrefix = "_BuilderLambdaCall_" private static readonly builderLambdaInstanceName = "instance" - private isBuilderLambdaCall(node: arkts.CallExpression): boolean { - if (!arkts.isIdentifier(node.expression)) { + private isBuilderLambdaCall(node: ts.CallExpression): boolean { + if (!ts.isIdentifier(node.expression)) { return false } return node.expression.text.startsWith(BuilderLambdaTransformer.builderLambdaPrefix) } - visitor(beforeChildren: arkts.Node): arkts.Node { - const node: arkts.Node = this.visitEachChild(beforeChildren) + visitor(beforeChildren: ts.Node): ts.Node { + const node: ts.Node = this.visitEachChild(beforeChildren) - if (!arkts.isCallExpression(node)) { + if (!ts.isCallExpression(node)) { return node } if (true - && arkts.isPropertyAccessExpression(node.parent) - && arkts.isIdentifier(node.parent.name) - && arkts.isCallExpression(node.parent.parent) + && ts.isPropertyAccessExpression(node.parent) + && ts.isIdentifier(node.parent.name) + && ts.isCallExpression(node.parent.parent) ) { return node } - let instanceCalls: arkts.CallExpression[] = [] - let leaf: arkts.CallExpression = node + let instanceCalls: ts.CallExpression[] = [] + let leaf: ts.CallExpression = node while (true - && arkts.isPropertyAccessExpression(leaf.expression) - && arkts.isIdentifier(leaf.expression.name) - && arkts.isCallExpression(leaf.expression.expression) + && ts.isPropertyAccessExpression(leaf.expression) + && ts.isIdentifier(leaf.expression.name) + && ts.isCallExpression(leaf.expression.expression) ) { instanceCalls.push( - arkts.factory.createCallExpression( + ts.factory.createCallExpression( leaf.expression.name, undefined, leaf.arguments @@ -66,7 +66,7 @@ export class BuilderLambdaTransformer extends AbstractVisitor { leaf = leaf.expression.expression } - if (!this.isBuilderLambdaCall(leaf) || !arkts.isIdentifier(leaf.expression)) { + if (!this.isBuilderLambdaCall(leaf) || !ts.isIdentifier(leaf.expression)) { return node } @@ -75,13 +75,13 @@ export class BuilderLambdaTransformer extends AbstractVisitor { // }, ...leaf.arguments); instanceCalls = instanceCalls.reverse() - let lambdaBody: arkts.Identifier | arkts.CallExpression = arkts.factory.createIdentifier(BuilderLambdaTransformer.builderLambdaInstanceName) - instanceCalls.forEach(function (call: arkts.CallExpression) { - if (!arkts.isIdentifier(call.expression)) { + let lambdaBody: ts.Identifier | ts.CallExpression = ts.factory.createIdentifier(BuilderLambdaTransformer.builderLambdaInstanceName) + instanceCalls.forEach(function (call: ts.CallExpression) { + if (!ts.isIdentifier(call.expression)) { throw new Error('unreachable (call expression should be identifier)') } - lambdaBody = arkts.factory.createCallExpression( - arkts.factory.createPropertyAccessExpression( + lambdaBody = ts.factory.createCallExpression( + ts.factory.createPropertyAccessExpression( lambdaBody, call.expression ), @@ -89,23 +89,23 @@ export class BuilderLambdaTransformer extends AbstractVisitor { call.arguments ) }) - const lambdaArg = arkts.factory.createArrowFunction( + const lambdaArg = ts.factory.createArrowFunction( undefined, undefined, [ - arkts.factory.createParameterDeclaration( + ts.factory.createParameterDeclaration( undefined, undefined, - arkts.factory.createIdentifier(BuilderLambdaTransformer.builderLambdaInstanceName), + ts.factory.createIdentifier(BuilderLambdaTransformer.builderLambdaInstanceName), undefined, - arkts.factory.createKeywordTypeNode(arkts.SyntaxKind.StringKeyword), + ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword), undefined ) ], undefined, undefined, - arkts.factory.createBlock([ - arkts.factory.createReturnStatement( + ts.factory.createBlock([ + ts.factory.createReturnStatement( lambdaBody ) ]) @@ -114,9 +114,9 @@ export class BuilderLambdaTransformer extends AbstractVisitor { let funcName = leaf.expression.text funcName = funcName.slice(BuilderLambdaTransformer.builderLambdaPrefix.length) - return arkts.factory.updateCallExpression( + return ts.factory.updateCallExpression( node, - arkts.factory.updateIdentifier(leaf.expression, funcName), + ts.factory.createIdentifier(funcName), undefined, [ lambdaArg, @@ -125,4 +125,3 @@ export class BuilderLambdaTransformer extends AbstractVisitor { ) } } -*/ \ No newline at end of file diff --git a/arkoala-arkts/libarkts/compatible/src/example-transformer.ts b/arkoala-arkts/libarkts/compatible/src/example-transformer.ts index d267f6e453fb8a1720252f9f10885e7e803f8a21..16984619c936df3c0081b3ad994372208bfb79d7 100644 --- a/arkoala-arkts/libarkts/compatible/src/example-transformer.ts +++ b/arkoala-arkts/libarkts/compatible/src/example-transformer.ts @@ -1,5 +1,4 @@ -/* -import * as arkts from "../../src/arkts" +import * as ts from "../../src/api" import { BuilderLambdaTransformer } from "./builder-lambda-transformer" export interface TransformerOptions { @@ -7,13 +6,12 @@ export interface TransformerOptions { } // TODO: remove question marks -export default function exampleTransformer(program?: arkts.Program, userPluginOptions?: TransformerOptions) { - return (ctx?: arkts.TransformationContext) => { - return (node: arkts.SourceFile) => { +export default function exampleTransformer(program?: ts.Program, userPluginOptions?: TransformerOptions) { + return (ctx?: ts.TransformationContext) => { + return (node: ts.SourceFile) => { const builderLambdaTransformer = (ctx !== undefined) ? new BuilderLambdaTransformer(ctx) : new BuilderLambdaTransformer() const transformed = builderLambdaTransformer.visitor(node) return transformed } } } -*/ \ No newline at end of file diff --git a/arkoala-arkts/libarkts/compatible/src/function-transformer.ts b/arkoala-arkts/libarkts/compatible/src/function-transformer.ts index 6e6d61eab1c0bb9d2c2ba9850a81a413d6c4f834..eaad17604694642b8ea2e3a4095e0fbb8d29959b 100644 --- a/arkoala-arkts/libarkts/compatible/src/function-transformer.ts +++ b/arkoala-arkts/libarkts/compatible/src/function-transformer.ts @@ -13,57 +13,58 @@ * limitations under the License. */ /* -import * as arkts from "../../src/arkts" +import global from "../../src/api/static/global" +import * as ts from "../../src/api" import { AbstractVisitor } from "./AbstractVisitor"; const ANNOTATION = "_REWRITE_" -function isAnnotatedCallExpression(node: arkts.Node): boolean { - return arkts.isCallExpression(node) && arkts.isIdentifier(node.expression) && node.expression.text.startsWith(ANNOTATION) +function isAnnotatedCallExpression(node: ts.Node): boolean { + return ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.expression.text.startsWith(ANNOTATION) } -function isAnnotatedMethodDeclaration(node: arkts.Node): boolean { - return arkts.isMethodDeclaration(node) && node.name.text.startsWith(ANNOTATION) +function isAnnotatedMethodDeclaration(node: ts.Node): boolean { + return ts.isMethodDeclaration(node) && node.name.text.startsWith(ANNOTATION) } -function transformCallExpression(node: arkts.CallExpression): arkts.Node { - const decl = arkts.getDecl(node.expression) +function transformCallExpression(node: ts.CallExpression): ts.Node { + const decl = ts.getDecl(node.expression) if (decl === undefined) { - arkts.throwError('memo function not found') + global.throwError('memo function not found') } - if (!arkts.isIdentifier(node.expression)) { - arkts.throwError('expression should be Identifier') + if (!ts.isIdentifier(node.expression)) { + global.throwError('expression should be Identifier') } - return arkts.factory.updateCallExpression( + return ts.factory.updateCallExpression( node, - arkts.factory.updateIdentifier( + ts.factory.updateIdentifier( node.expression, node.expression.text.slice(ANNOTATION.length) ), undefined, [ - arkts.factory.createStringLiteral("SAMPLE"), + ts.factory.createStringLiteral("SAMPLE"), ...node.arguments ] ) } -function transformMethodDeclaration(node: arkts.MethodDeclaration): arkts.Node { +function transformMethodDeclaration(node: ts.MethodDeclaration): ts.Node { const stringParam = - arkts.factory.createParameterDeclaration( + ts.factory.createParameterDeclaration( undefined, undefined, - arkts.factory.createIdentifier("x"), + ts.factory.createIdentifier("x"), undefined, - arkts.factory.createKeywordTypeNode(arkts.SyntaxKind.StringKeyword), + ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword), undefined ) - return arkts.factory.updateMethodDeclaration( + return ts.factory.updateMethodDeclaration( node, undefined, undefined, - arkts.factory.updateIdentifier( + ts.factory.updateIdentifier( node.name, node.name.text.slice(ANNOTATION.length) ), @@ -80,17 +81,17 @@ function transformMethodDeclaration(node: arkts.MethodDeclaration): arkts.Node { export class FunctionTransformer extends AbstractVisitor { constructor( - ctx?: arkts.TransformationContext + ctx?: ts.TransformationContext ) { super(ctx) } - visitor(beforeChildren: arkts.Node): arkts.Node { - const node: arkts.Node = this.visitEachChild(beforeChildren) + visitor(beforeChildren: ts.Node): ts.Node { + const node: ts.Node = this.visitEachChild(beforeChildren) - if (isAnnotatedMethodDeclaration(node) && arkts.isMethodDeclaration(node)) { + if (isAnnotatedMethodDeclaration(node) && ts.isMethodDeclaration(node)) { return transformMethodDeclaration(node) - } else if (isAnnotatedCallExpression(node) && arkts.isCallExpression(node) && arkts.isIdentifier(node.expression)) { + } else if (isAnnotatedCallExpression(node) && ts.isCallExpression(node) && ts.isIdentifier(node.expression)) { return transformCallExpression(node) } diff --git a/arkoala-arkts/libarkts/compatible/src/print-visitor.ts b/arkoala-arkts/libarkts/compatible/src/print-visitor.ts index 7865d20e7b1b340f55e5cb1e82c0d4b104173dcc..e28b65221f04eb489ba93caa5b1224bc14e1651e 100644 --- a/arkoala-arkts/libarkts/compatible/src/print-visitor.ts +++ b/arkoala-arkts/libarkts/compatible/src/print-visitor.ts @@ -1,10 +1,9 @@ -/* -import * as arkts from "../../src/arkts" +import * as ts from "../../src/api" import { AbstractVisitor } from "./AbstractVisitor"; export class PrintVisitor extends AbstractVisitor { constructor( - ctx?: arkts.TransformationContext + ctx?: ts.TransformationContext ) { super(ctx) } @@ -15,17 +14,16 @@ export class PrintVisitor extends AbstractVisitor { this.result += " ".repeat(4 * this.indentation) + s + '\n' } - visitor(beforeChildren: arkts.Node): arkts.Node { + visitor(beforeChildren: ts.Node): ts.Node { this.print(beforeChildren.constructor.name + " (mods: [" + beforeChildren.modifiers + "])") const node = this.visitEachChild(beforeChildren) return node } - astToString(node: arkts.Node): string { + astToString(node: ts.Node): string { this.result = "" this.visitor(node) return this.result.trim() } } -*/ \ No newline at end of file diff --git a/arkoala-arkts/libarkts/native/meson.build b/arkoala-arkts/libarkts/native/meson.build index 76a00282321059cbd03d4b40328a966c9ff12eb1..c5e194b8d9486399c41d35122969739bd4cc4be2 100644 --- a/arkoala-arkts/libarkts/native/meson.build +++ b/arkoala-arkts/libarkts/native/meson.build @@ -5,8 +5,8 @@ project('Es2panda interop', 'cpp', source_dir = meson.current_source_dir() interop_src = '../../../interop/src/cpp' -es2panda_header = '../node_modules/@koalaui/plugin-api-panda-sdk/@panda/sdk/ohos_arm64/include/tools/es2panda/public' -es2panda_gen = '../node_modules/@koalaui/plugin-api-panda-sdk/@panda/sdk/ohos_arm64/include/tools/es2panda/' +es2panda_header = '../node_modules/@panda/sdk/ohos_arm64/include/tools/es2panda/public' +es2panda_gen = '../node_modules/@panda/sdk/ohos_arm64/include/tools/es2panda' is_msvc = meson.get_compiler('cpp').get_id() == 'msvc' implementation = './src/es2panda_lib.cc' diff --git a/arkoala-arkts/libarkts/native/src/es2panda_lib.cc b/arkoala-arkts/libarkts/native/src/es2panda_lib.cc index 8da28e2b3ad7304342f30a806f459006536db6e2..dce6766c0ae1567e19d524d58e7fa89e4e6d681a 100644 --- a/arkoala-arkts/libarkts/native/src/es2panda_lib.cc +++ b/arkoala-arkts/libarkts/native/src/es2panda_lib.cc @@ -40,7 +40,7 @@ static es2panda_Impl *impl = nullptr; #define LIB_SUFFIX ".so" #endif -const char* libpath = "./node_modules/@koalaui/plugin-api-panda-sdk/@panda/sdk/" PLUGIN_DIR "/lib/" LIB_PREFIX "es2panda-public" LIB_SUFFIX; +const char* libpath = "./node_modules/@panda/sdk/" PLUGIN_DIR "/lib/" LIB_PREFIX "es2panda-public" LIB_SUFFIX; es2panda_Impl *GetImpl() { if (impl) { @@ -259,6 +259,25 @@ KNativePointer impl_CreateMethodDefinition( } KOALA_INTEROP_6(CreateMethodDefinition, KNativePointer, KNativePointer, KInt, KNativePointer, KNativePointer, KInt, KBoolean) +KNativePointer impl_CreateClassProperty( + KNativePointer contextPtr, + KNativePointer keyPtr, + KNativePointer valuePtr, + KNativePointer typeAnnotationPtr, + KInt modifiersT, + KBoolean isComputedT +) { + auto context = reinterpret_cast(contextPtr); + auto key = reinterpret_cast(keyPtr); + auto value = reinterpret_cast(valuePtr); + auto typeAnnotation = reinterpret_cast(typeAnnotationPtr); + auto modifiers = static_cast(modifiersT); + auto isComputed = static_cast(isComputedT); + + return GetImpl()->CreateClassProperty(context, key, value, typeAnnotation, modifiers, isComputed); +} +KOALA_INTEROP_6(CreateClassProperty, KNativePointer, KNativePointer, KNativePointer, KNativePointer, KNativePointer, KInt, KBoolean) + KNativePointer impl_AstNodeDumpJsonConst(KNativePointer contextPtr, KNativePointer nodePtr) { auto context = reinterpret_cast(contextPtr); auto node = reinterpret_cast(nodePtr); @@ -460,16 +479,6 @@ KNativePointer impl_VariableDeclarationDeclaratorsConst(KNativePointer contextPt } KOALA_INTEROP_2(VariableDeclarationDeclaratorsConst, KNativePointer, KNativePointer, KNativePointer) -KNativePointer impl_BlockStatementUpdateStatements(KNativePointer contextPtr, KNativePointer nodePtr, KNativePointerArray statementListPtr, KInt statementListLen, KNativePointer returnTypeLenPtr) { - auto context = reinterpret_cast(contextPtr); - auto node = reinterpret_cast(nodePtr); - auto statementList = reinterpret_cast(statementListPtr); - size_t n; - auto statements = GetImpl()->BlockStatementUpdateStatements(context, node, statementList, statementListLen, &n); - return new vector(statements, statements + n); -} -KOALA_INTEROP_5(BlockStatementUpdateStatements, KNativePointer, KNativePointer, KNativePointer, KNativePointerArray, KInt, KNativePointer) - KNativePointer impl_ClassDeclarationDefinition(KNativePointer contextPtr, KNativePointer nodePtr) { auto context = reinterpret_cast(contextPtr); auto node = reinterpret_cast(nodePtr); @@ -548,29 +557,39 @@ KBoolean impl_ScriptFunctionDeclareConst(KNativePointer contextPtr, KNativePoint } KOALA_INTEROP_2(ScriptFunctionDeclareConst, KBoolean, KNativePointer, KNativePointer) -KNativePointer impl_CreateFunctionDeclaration(KNativePointer contextPtr, KNativePointer funcPtr, KBoolean isAnonK) { +KNativePointer impl_CreateFunctionDeclaration( + KNativePointer contextPtr, + KNativePointer funcPtr, + KNativePointerArray annotationsPtr, + KInt annotationsLen, + KBoolean isAnonK +) { auto context = reinterpret_cast(contextPtr); auto func = reinterpret_cast(funcPtr); + auto annotations = reinterpret_cast(annotationsPtr); auto isAnon = static_cast(isAnonK); - return GetImpl()->CreateFunctionDeclaration(context, func, isAnon); + return GetImpl()->CreateFunctionDeclaration(context, func, annotations, annotationsLen, isAnon); } -KOALA_INTEROP_3(CreateFunctionDeclaration, KNativePointer, KNativePointer, KNativePointer, KBoolean) +KOALA_INTEROP_5(CreateFunctionDeclaration, KNativePointer, KNativePointer, KNativePointer, KNativePointerArray, KInt, KBoolean) KNativePointer impl_UpdateFunctionDeclaration( KNativePointer contextPtr, KNativePointer nodePtr, KNativePointer funcPtr, + KNativePointerArray annotationsPtr, + KInt annotationsLen, KBoolean isAnonK ) { auto context = reinterpret_cast(contextPtr); auto node = reinterpret_cast(nodePtr); auto func = reinterpret_cast(funcPtr); + auto annotations = reinterpret_cast(annotationsPtr); auto isAnon = static_cast(isAnonK); - return GetImpl()->UpdateFunctionDeclaration(context, node, func, isAnon); + return GetImpl()->UpdateFunctionDeclaration(context, node, func, annotations, annotationsLen, isAnon); } -KOALA_INTEROP_4(UpdateFunctionDeclaration, KNativePointer, KNativePointer, KNativePointer, KNativePointer, KBoolean) +KOALA_INTEROP_6(UpdateFunctionDeclaration, KNativePointer, KNativePointer, KNativePointer, KNativePointer, KNativePointerArray, KInt, KBoolean) // TODO: add param initializer KNativePointer impl_CreateETSParameterExpression(KNativePointer contextPtr, KNativePointer identifierPtr, KNativePointer initializerPtr) { @@ -925,6 +944,15 @@ KNativePointer impl_CreateBinaryExpression(KNativePointer contextPtr, KNativePoi } KOALA_INTEROP_4(CreateBinaryExpression, KNativePointer, KNativePointer, KNativePointer, KNativePointer, KInt) +KNativePointer impl_CreateAssignmentExpression(KNativePointer contextPtr, KNativePointer leftPtr, KNativePointer rightPtr, KInt assignmentOperator) { + auto context = reinterpret_cast(contextPtr); + auto left = reinterpret_cast(leftPtr); + auto right = reinterpret_cast(rightPtr); + + return GetImpl()->CreateAssignmentExpression(context, left, right, Es2pandaTokenType(assignmentOperator)); +} +KOALA_INTEROP_4(CreateAssignmentExpression, KNativePointer, KNativePointer, KNativePointer, KNativePointer, KInt) + KNativePointer impl_CreateFunctionSignature( KNativePointer contextPtr, KNativePointer typeParamsPtr, @@ -1245,20 +1273,6 @@ KNativePointer impl_ScriptFunctionSetSignature( } KOALA_INTEROP_3(ScriptFunctionSetSignature, KNativePointer, KNativePointer, KNativePointer, KNativePointer) -KNativePointer impl_ScriptFunctionSetIrSignature( - KNativePointer contextPtr, - KNativePointer astPtr, - KNativePointer signaturePtr -) { - auto context = reinterpret_cast(contextPtr); - auto ast = reinterpret_cast(astPtr); - auto signature = reinterpret_cast(signaturePtr); - - GetImpl()->ScriptFunctionSetIrSignature(context, ast, signature); - return ast; -} -KOALA_INTEROP_3(ScriptFunctionSetIrSignature, KNativePointer, KNativePointer, KNativePointer, KNativePointer) - KNativePointer impl_ScriptFunctionSetBody( KNativePointer contextPtr, KNativePointer astPtr, @@ -1389,25 +1403,6 @@ KBoolean impl_IdentifierIsPrivateIdentConst(KNativePointer contextPtr, KNativePo } KOALA_INTEROP_2(IdentifierIsPrivateIdentConst, KBoolean, KNativePointer, KNativePointer) -KNativePointer impl_IdentifierSetReference(KNativePointer contextPtr, KNativePointer nodePtr, KBoolean isReferenceK) { - auto context = reinterpret_cast(contextPtr); - auto node = reinterpret_cast(nodePtr); - auto isReference = static_cast(isReferenceK); - - GetImpl()->IdentifierSetReference(context, node, isReference); - return node; -} -KOALA_INTEROP_3(IdentifierSetReference, KNativePointer, KNativePointer, KNativePointer, KBoolean) - -KNativePointer impl_ScriptFunctionUpdateIrSignature(KNativePointer contextPtr, KNativePointer nodePtr, KNativePointer signaturePtr) { - auto context = reinterpret_cast(contextPtr); - auto node = reinterpret_cast(nodePtr); - auto signature = reinterpret_cast(signaturePtr); - - return GetImpl()->ScriptFunctionUpdateIrSignature(context, node, signature); -} -KOALA_INTEROP_3(ScriptFunctionUpdateIrSignature, KNativePointer, KNativePointer, KNativePointer, KNativePointer) - KNativePointer impl_CreateFunctionExpression(KNativePointer contextPtr, KNativePointer funcPtr) { auto context = reinterpret_cast(contextPtr); auto func = reinterpret_cast(funcPtr); @@ -1590,17 +1585,6 @@ KNativePointer impl_ClassDefinitionSetTypeParams( } KOALA_INTEROP_3(ClassDefinitionSetTypeParams, KNativePointer, KNativePointer, KNativePointer, KNativePointer) -KNativePointer impl_ClassDefinitionLanguageConst( - KNativePointer contextPtr, - KNativePointer nodePtr -) { - auto context = reinterpret_cast(contextPtr); - auto node = reinterpret_cast(nodePtr); - - return GetImpl()->ClassDefinitionLanguageConst(context, node); -} -KOALA_INTEROP_2(ClassDefinitionLanguageConst, KNativePointer, KNativePointer, KNativePointer) - KNativePointer impl_ClassDefinitionIdent( KNativePointer contextPtr, KNativePointer nodePtr @@ -1618,8 +1602,7 @@ KNativePointer impl_CreateClassDefinition1( KNativePointerArray bodyPtr, KInt bodyLenT, KInt modifiersT, - KInt flagsT, - KNativePointer langT + KInt flagsT ) { auto context = reinterpret_cast(contextPtr); auto ident = reinterpret_cast(identPtr); @@ -1627,11 +1610,10 @@ KNativePointer impl_CreateClassDefinition1( auto bodyLen = static_cast(bodyLenT); auto modifiers = Es2pandaClassDefinitionModifiers(modifiersT); auto flags = Es2pandaModifierFlags(flagsT); - auto lang = reinterpret_cast(langT); - return GetImpl()->CreateClassDefinition1(context, ident, body, bodyLen, modifiers, flags, lang); + return GetImpl()->CreateClassDefinition1(context, ident, body, bodyLen, modifiers, flags); } -KOALA_INTEROP_7(CreateClassDefinition1, KNativePointer, KNativePointer, KNativePointer, KNativePointerArray, KInt, KInt, KInt, KNativePointer) +KOALA_INTEROP_6(CreateClassDefinition1, KNativePointer, KNativePointer, KNativePointer, KNativePointerArray, KInt, KInt, KInt) KNativePointer impl_UpdateClassDefinition1( KNativePointer contextPtr, @@ -1640,8 +1622,7 @@ KNativePointer impl_UpdateClassDefinition1( KNativePointerArray bodyPtr, KInt bodyLenT, KInt modifiersT, - KInt flagsT, - KNativePointer langT + KInt flagsT ) { auto context = reinterpret_cast(contextPtr); auto original = reinterpret_cast(originalPtr); @@ -1650,8 +1631,7 @@ KNativePointer impl_UpdateClassDefinition1( auto bodyLen = static_cast(bodyLenT); auto modifiers = Es2pandaClassDefinitionModifiers(modifiersT); auto flags = Es2pandaModifierFlags(flagsT); - auto lang = reinterpret_cast(langT); - return GetImpl()->UpdateClassDefinition1(context, original, ident, body, bodyLen, modifiers, flags, lang); + return GetImpl()->UpdateClassDefinition1(context, original, ident, body, bodyLen, modifiers, flags); } -KOALA_INTEROP_8(UpdateClassDefinition1, KNativePointer, KNativePointer, KNativePointer, KNativePointer, KNativePointerArray, KInt, KInt, KInt, KNativePointer) +KOALA_INTEROP_7(UpdateClassDefinition1, KNativePointer, KNativePointer, KNativePointer, KNativePointer, KNativePointerArray, KInt, KInt, KInt) diff --git a/arkoala-arkts/libarkts/native/src/playground.cc b/arkoala-arkts/libarkts/native/src/playground.cc index 49e0785e382d7f1fa753a61c26cb6d6f8b8b3725..b4e971ad0e88346f33df57545e66216aeadbc2ff 100644 --- a/arkoala-arkts/libarkts/native/src/playground.cc +++ b/arkoala-arkts/libarkts/native/src/playground.cc @@ -3,7 +3,7 @@ #include "dynamic-loader.h" #include "es2panda_lib.h" -const char* libpath = "../node_modules/@koalaui/plugin-api-panda-sdk/@panda/sdk/linux_host_tools/lib/libes2panda-public.so"; +const char* libpath = "../node_modules/@panda/sdk/linux_host_tools/lib/libes2panda-public.so"; static es2panda_Impl *impl = nullptr; diff --git a/arkoala-arkts/libarkts/package.json b/arkoala-arkts/libarkts/package.json index c910549a16b624506d21d1aa543c9da74ca1fc4b..bd9192a4ea12e73e493db3b17843bee10f1d6690 100644 --- a/arkoala-arkts/libarkts/package.json +++ b/arkoala-arkts/libarkts/package.json @@ -6,7 +6,7 @@ "commander": "^11.1.0" }, "devDependencies": { - "@koalaui/plugin-api-panda-sdk": "1.0.5", + "@panda/sdk": "1.5.0-dev.9184", "@types/chai": "^4.3.1", "@types/mocha": "^9.1.0", "chai": "^4.3.6", diff --git a/arkoala-arkts/libarkts/src/NativeModule.ts b/arkoala-arkts/libarkts/src/NativeModule.ts index 5532f492dc42fa22ac2bbe84026abee142e89a86..d352a3488969d13211de2a20dcb76e4d0f63b1af 100644 --- a/arkoala-arkts/libarkts/src/NativeModule.ts +++ b/arkoala-arkts/libarkts/src/NativeModule.ts @@ -62,9 +62,7 @@ export interface NativeModule { _ScriptFunctionBody(context: KNativePointer, node: KNativePointer): KNativePointer _ScriptFunctionSetIdent(context: KNativePointer, ast: KNativePointer, id: KNativePointer): KNativePointer _ScriptFunctionIrSignature(context: KNativePointer, ast: KNativePointer): KNativePointer - _ScriptFunctionUpdateIrSignature(context: KNativePointer, ast: KNativePointer, signature: KNativePointer): KNativePointer _ScriptFunctionSetSignature(context: KNativePointer, ast: KNativePointer, signature: KNativePointer): KNativePointer - _ScriptFunctionSetIrSignature(context: KNativePointer, ast: KNativePointer, signature: KNativePointer): KNativePointer _ScriptFunctionSetBody(context: KNativePointer, ast: KNativePointer, body: KNativePointer): KNativePointer _ScriptFunctionSetScope(context: KNativePointer, ast: KNativePointer, scope: KNativePointer): KNativePointer _ScriptFunctionDeclareConst(context: KNativePointer, node: KNativePointer): KBoolean @@ -75,15 +73,16 @@ export interface NativeModule { _BlockStatementSetScope(context: KNativePointer, node: KNativePointer, scope: KNativePointerArray): void _CreateIdentifier1(context: KNativePointer, name: String): KNativePointer _CreateIdentifier2(context: KNativePointer, name: String, type_annotation: KNativePointer): KNativePointer - _IdentifierSetReference(context: KNativePointer, node: KNativePointer, isRef: KBoolean): void _IdentifierSetName(context: KNativePointer, node: KNativePointer, name: String): void - _CreateFunctionDeclaration(context: KNativePointer, func: KNativePointer, isAnon: KBoolean): KNativePointer - _UpdateFunctionDeclaration(context: KNativePointer, node: KNativePointer, func: KNativePointer, isAnon: KBoolean): KNativePointer + _CreateFunctionDeclaration(context: KNativePointer, func: KNativePointer, annotations: KNativePointerArray, annotationsLen: KInt, isAnon: KBoolean): KNativePointer + _UpdateFunctionDeclaration(context: KNativePointer, node: KNativePointer, annotations: KNativePointerArray, annotationsLen: KInt, func: KNativePointer, isAnon: KBoolean): KNativePointer _CreateReturnStatement1(context: KNativePointer, argument: KNativePointer): KNativePointer _ReturnStatementArgument(context: KNativePointer, node: KNativePointer): KNativePointer _CreateIfStatement(context: KNativePointer, test: KNativePointer, consequent: KNativePointer, alternate: KNativePointer): KNativePointer _CreateBinaryExpression(context: KNativePointer, left: KNativePointer, right: KNativePointer, operatorType: KInt): KNativePointer + _CreateAssignmentExpression(context: KNativePointer, left: KNativePointer, right: KNativePointer, assignmentOperator: KInt): KNativePointer _CreateMethodDefinition(context: KNativePointer, kind: KInt, key: KNativePointer, value: KNativePointer, modifiers: KInt, isComputed: KBoolean): KNativePointer + _CreateClassProperty(context: KNativePointer, key: KNativePointer, value: KNativePointer, typeAnnotation: KNativePointer, modifiers: KInt, isComputed: KBoolean): KNativePointer _CreateFunctionSignature(context: KNativePointer, typeParams: KNativePointer, params: KNativePointerArray, paramsLen: KInt, returnTypeAnnotation: KNativePointer): KNativePointer _CreateScriptFunction(context: KNativePointer, databody: KNativePointer, datasignature: KNativePointer, datafuncFlags: KInt, dataflags: KInt, datadeclare: KBoolean): KNativePointer @@ -155,17 +154,15 @@ export interface NativeModule { _BlockStatementStatements(context: KNativePointer, node: KNativePointer): KNativePointer _BlockStatementSetStatements(context: KNativePointer, node: KNativePointer, statements: KNativePointerArray, statementsLen: KInt): void - _BlockStatementUpdateStatements(context: KNativePointer, original: KNativePointer, statementList: KNativePointerArray, statementListLen: KInt, returnType: KNativePointer): KNativePointer _ClassDeclarationDefinition(context: KNativePointer, node: KNativePointer): KNativePointer _ClassDefinitionBody(context: KNativePointer, node: KNativePointer): KNativePointer - _ClassDefinitionLanguageConst(context: KNativePointer, node: KNativePointer): KNativePointer _ClassDefinitionIdent(context: KNativePointer, node: KNativePointer): KNativePointer _ClassDefinitionTypeParamsConst(context: KNativePointer, node: KNativePointer): KNativePointer _CreateClassDeclaration(context: KNativePointer, def: KNativePointer): KNativePointer _UpdateClassDeclaration(context: KNativePointer, original: KNativePointer, def: KNativePointer): KNativePointer - _CreateClassDefinition1(context: KNativePointer, ident: KNativePointer, body: KNativePointerArray, bodyLen: KInt, modifiers: KInt, flags: KInt, lang: KNativePointer): KNativePointer + _CreateClassDefinition1(context: KNativePointer, ident: KNativePointer, body: KNativePointerArray, bodyLen: KInt, modifiers: KInt, flags: KInt): KNativePointer _ClassDefinitionSetTypeParams(context: KNativePointer, ast: KNativePointer, typeParams: KNativePointer): void - _UpdateClassDefinition1(context: KNativePointer, original: KNativePointer, ident: KNativePointer, body: KNativePointerArray, bodyLen: KInt, modifiers: KInt, flags: KInt, lang: KNativePointer): KNativePointer + _UpdateClassDefinition1(context: KNativePointer, original: KNativePointer, ident: KNativePointer, body: KNativePointerArray, bodyLen: KInt, modifiers: KInt, flags: KInt): KNativePointer _CreateETSFunctionTypeIr(context: KNativePointer, signature: KNativePointer, funcFlags: KInt): KNativePointer _IsProgram(context: KNativePointer, node: KNativePointer): KBoolean diff --git a/arkoala-arkts/libarkts/src/api/factory/nodeFactory.ts b/arkoala-arkts/libarkts/src/api/factory/nodeFactory.ts index 0f79dab9675b25af8dc4609e3f4c394af5530afc..bbf4a4f1716890bdb02d0d0334646fd9a59e7547 100644 --- a/arkoala-arkts/libarkts/src/api/factory/nodeFactory.ts +++ b/arkoala-arkts/libarkts/src/api/factory/nodeFactory.ts @@ -1,6 +1,7 @@ import global from "../static/global" import * as ts from "@koalaui/ets-tsc" -import * as arkts from "../layers/arkts/types" +import * as arkts from "../layers/arkts" +import { nativeModule } from "src/NativeModule" import { passNode, @@ -16,6 +17,9 @@ import { // updateModifiers, // passTypeParams, } from "../layers/arkts/utilities" +import { + passTypeParams, +} from "./utilities" import { SyntaxKind, Es2pandaPrimitiveType, @@ -45,10 +49,9 @@ import { IfStatement, ExpressionStatement, CallExpression, - // ArrowFunction, + ArrowFunction, TypeReferenceNode, BinaryExpression, - // ClassDeclaration, FunctionTypeNode, TypeNode, Expression, @@ -59,6 +62,7 @@ import { ConstructorDeclaration, TypeParameterDeclaration, NumericLiteral, + ClassDeclaration, // VariableDeclaration, // VariableDeclarationList, // VariableStatement, @@ -71,7 +75,6 @@ export function createNodeFactory() { createSourceFile, updateSourceFile, createIdentifier, - // updateIdentifier, createStringLiteral, createNumericLiteral, createFunctionDeclaration, @@ -96,10 +99,8 @@ export function createNodeFactory() { createToken, createBinaryExpression, updateBinaryExpression, - // createArrowFunction, - // updateArrowFunction, - // createClassDeclaration, - // updateClassDeclaration, + createArrowFunction, + updateArrowFunction, createFunctionTypeNode, updateFunctionTypeNode, createMethodDeclaration, @@ -108,6 +109,8 @@ export function createNodeFactory() { updateConstructorDeclaration, createTypeParameterDeclaration, updateTypeParameterDeclaration, + createClassDeclaration, + updateClassDeclaration, // createVariableDeclarationList, // updateVariableDeclarationList, // createVariableStatement, @@ -152,17 +155,18 @@ export function createNodeFactory() { // createIdentifier( // text: string // ): Identifier; - function createIdentifier(text: string): Identifier { - const node = arkts.Identifier.create(text) - return new Identifier(node) + function createIdentifier( + text: string, + typeAnnotation?: TypeNode | undefined + ): Identifier { + return new Identifier( + arkts.Identifier.create( + text, + passNode(typeAnnotation) + ) + ) } - // // @api - // // NOT PROVIDED - // function updateIdentifier(node: Identifier, text: string, typeAnnotation?: TypeNode | undefined): Identifier { - // return new Identifier(text, typeAnnotation?.node) - // } - // @api // createStringLiteral( // text: string, @@ -319,11 +323,7 @@ export function createNodeFactory() { arkts.ScriptFunction.create( body?.node, arkts.FunctionSignature.create( - (typeParameters !== undefined) ? - arkts.TSTypeParameterDeclaration.create( - passNodeArray(typeParameters) - ) : - undefined, + passTypeParams(typeParameters), passNodeArray(parameters), type?.node ), @@ -809,7 +809,7 @@ export function createNodeFactory() { const updated = createBinaryExpression(left, operator, right) return new BinaryExpression(updateNode(updated.node, node.node)) } -/* + // @api // createArrowFunction( // modifiers: readonly Modifier[] | undefined, @@ -821,42 +821,28 @@ export function createNodeFactory() { // ): ArrowFunction; function createArrowFunction( modifiers: readonly Modifier[] | undefined, - typeParameters: undefined, + typeParameters: readonly TypeParameterDeclaration[] | undefined, parameters: readonly ParameterDeclaration[], - type: undefined, + type: TypeNode | undefined, equalsGreaterThanToken: undefined, body: Block ) { - const peer = withNodeArray(parameters, (_parameters: BigUint64Array) => { - const _context = global.context - const _body = body.peer - - const _signature = nativeModule._CreateFunctionSignature( - _context, - NULLPTR, - _parameters, - _parameters.length, - NULLPTR - ) - - const _scriptFunc = createScriptFunction( - _body, - _signature, - Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_ARROW, - Es2pandaModifierFlags.MODIFIER_FLAGS_NONE, - false, - undefined - ) - - const _arrowFunc = nativeModule._CreateArrowFunctionExpression( - _context, - _scriptFunc + return new ArrowFunction( + arkts.ArrowFunctionExpression.create( + arkts.ScriptFunction.create( + passNode(body), + arkts.FunctionSignature.create( + passTypeParams(typeParameters), + passNodeArray(parameters), + passNode(type) + ), + passModifiersToScriptFunction(modifiers) | Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_ARROW, + passModifiers(modifiers) | Es2pandaModifierFlags.MODIFIER_FLAGS_NONE, + false, + undefined + ) ) - updateChildren(_arrowFunc) - - return _arrowFunc - }) - return new ArrowFunction(updateChildren(updateModifiers(peer, modifiers))) + ) } // @api @@ -872,14 +858,14 @@ export function createNodeFactory() { function updateArrowFunction( node: ArrowFunction, modifiers: readonly Modifier[] | undefined, - typeParameters: undefined, + typeParameters: readonly TypeParameterDeclaration[] | undefined, parameters: readonly ParameterDeclaration[], type: undefined, equalsGreaterThanToken: undefined, body: Block, ): ArrowFunction { const updated = createArrowFunction(modifiers, typeParameters, parameters, type, equalsGreaterThanToken, body) - return new ArrowFunction(updateNode(updated.peer, node.peer)) + return new ArrowFunction(updateNode(updated.node, node.node)) } // @api @@ -897,30 +883,18 @@ export function createNodeFactory() { heritageClauses: undefined, members: readonly ClassElement[] ) { - const peer = withNodeArray(members, (_members: BigUint64Array) => { - const _context = global.context - - const _newClassDefinition = nativeModule._CreateClassDefinition1( - _context, - // TODO: maybe this is wrong - passIdentifier(name), - _members, - _members.length, - Es2pandaClassDefinitionModifiers.CLASS_DEFINITION_MODIFIERS_NONE, - Es2pandaModifierFlags.MODIFIER_FLAGS_NONE, - // TODO: fix lang - NULLPTR - ) - nativeModule._ClassDefinitionSetTypeParams(_context, _newClassDefinition, passTypeParams(typeParameters)) - updateModifiers(_newClassDefinition, modifiers) - updateChildren(_newClassDefinition) - - return nativeModule._CreateClassDeclaration( - _context, - _newClassDefinition + return new ClassDeclaration( + arkts.ClassDeclaration.create( + arkts.ClassDefinition.create( + passIdentifier(name), + passNodeArray(members), + passModifiers(modifiers) | Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC | Es2pandaModifierFlags.MODIFIER_FLAGS_STATIC, + // TODO: pass through modifiers + Es2pandaClassDefinitionModifiers.CLASS_DEFINITION_MODIFIERS_NONE, + undefined + ) ) - }) - return new ClassDeclaration(updateChildren(updateModifiers(peer, modifiers))) + ) } // @api @@ -941,46 +915,38 @@ export function createNodeFactory() { members: readonly ClassElement[] ) { // TODO: rewrite like this (impossible now because of Language param) - // const updated = createClassDeclaration(modifiers, name, typeParameters, heritageClauses, members) - // return new ClassDeclaration(updateNode(updated.peer, node.peer)) - - const peer = withNodeArray(members, (_members: BigUint64Array) => { - const _context = global.context - const _classDefinition = nativeModule._ClassDeclarationDefinition( - _context, - node.peer - ) - - const _lang = nativeModule._ClassDefinitionLanguageConst( - _context, - _classDefinition - ) - - const _newClassDefinition = nativeModule._UpdateClassDefinition1( - _context, - _classDefinition, - // TODO: maybe this is wrong - passIdentifier(name), - _members, - _members.length, - Es2pandaClassDefinitionModifiers.CLASS_DEFINITION_MODIFIERS_NONE, - Es2pandaModifierFlags.MODIFIER_FLAGS_NONE, - _lang - ) - nativeModule._ClassDefinitionSetTypeParams(_context, _newClassDefinition, passTypeParams(typeParameters)) - updateModifiers(_newClassDefinition, modifiers) - updateNode(_newClassDefinition, _classDefinition) - - return nativeModule._UpdateClassDeclaration( - _context, - node.peer, - _newClassDefinition + const _classDefinition = nativeModule._ClassDeclarationDefinition( + global.context, + node.node.peer + ) + const _members = arkts.passNodeArray(passNodeArray(members)) + const _newClassDefinition = + arkts.unpackNonNullableNode( + nativeModule._UpdateClassDefinition1( + global.context, + _classDefinition, + // TODO: maybe this is wrong + arkts.passNode(passIdentifier(name)), + _members, + _members.length, + Es2pandaClassDefinitionModifiers.CLASS_DEFINITION_MODIFIERS_NONE, + Es2pandaModifierFlags.MODIFIER_FLAGS_NONE + ) ) - }) - return new ClassDeclaration(updateNode(updateModifiers(peer, modifiers), node.peer)) + nativeModule._ClassDefinitionSetTypeParams(global.context, _newClassDefinition.peer, arkts.passNode(passTypeParams(typeParameters))) + _newClassDefinition.modifiers = passModifiers(modifiers) + return new ClassDeclaration( + arkts.unpackNonNullableNode( + nativeModule._UpdateClassDeclaration( + global.context, + node.node.peer, + _newClassDefinition.updateNode(arkts.unpackNonNullableNode(_classDefinition)).peer + ) + ).updateModifiers(Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC | Es2pandaModifierFlags.MODIFIER_FLAGS_STATIC) as arkts.ClassDeclaration + ) } -*/ + // @api // tsc: createFunctionTypeNode( // typeParameters: readonly TypeParameterDeclaration[] | undefined, @@ -988,14 +954,14 @@ export function createNodeFactory() { // type: TypeNode // ): FunctionTypeNode; function createFunctionTypeNode( - typeParameters: undefined, + typeParameters: readonly TypeParameterDeclaration[] | undefined, parameters: readonly ParameterDeclaration[], type: TypeNode ): FunctionTypeNode { return new FunctionTypeNode( arkts.FunctionTypeNode.create( arkts.FunctionSignature.create( - passNode(undefined), + passTypeParams(typeParameters), passNodeArray(parameters), passNode(type) ), @@ -1013,7 +979,7 @@ export function createNodeFactory() { // ): FunctionTypeNode function updateFunctionTypeNode( node: FunctionTypeNode, - typeParameters: undefined, + typeParameters: readonly TypeParameterDeclaration[] | undefined, parameters: readonly ParameterDeclaration[], type: TypeNode ): FunctionTypeNode { @@ -1043,7 +1009,7 @@ export function createNodeFactory() { asteriskToken: undefined, name: string | Identifier, questionToken: undefined, - typeParameters: undefined, + typeParameters: readonly TypeParameterDeclaration[] | undefined, parameters: readonly ParameterDeclaration[], type: TypeNode | undefined, body: Block | undefined @@ -1057,12 +1023,12 @@ export function createNodeFactory() { arkts.ScriptFunction.create( passNode(body), arkts.FunctionSignature.create( - undefined, + passTypeParams(typeParameters), passNodeArray(parameters), passNode(type) ), - passModifiersToScriptFunction(modifiers), - passModifiers(modifiers), + passModifiersToScriptFunction(modifiers) | Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_METHOD, + passModifiers(modifiers) | Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC || Es2pandaModifierFlags.MODIFIER_FLAGS_STATIC, false, _name ) @@ -1071,44 +1037,6 @@ export function createNodeFactory() { false ) ) - // const peer = withNodeArray(parameters, (_parameters: BigUint64Array) => { - // const _context = global.context - - // const _signature = nativeModule._CreateFunctionSignature( - // _context, - // NULLPTR, - // _parameters, - // _parameters.length, - // type?.peer ?? NULLPTR, - // ) - - // const _ident = passIdentifier(name) - - // const _scriptFunc = createScriptFunction( - // body?.peer ?? NULLPTR, - // _signature, - // passScriptFuncModifiers(modifiers), - // passModifiers(modifiers), - // false, - // _ident - // ) - - // const _funcExpr = nativeModule._CreateFunctionExpression( - // global.context, - // _scriptFunc - // ) - // updateChildren(_funcExpr) - - // return nativeModule._CreateMethodDefinition( - // _context, - // Es2pandaMethodDefinitionKind.METHOD_DEFINITION_KIND_METHOD, - // _ident, - // _funcExpr, - // passModifiers(modifiers), - // false - // ) - // }) - // return new MethodDeclaration(updateChildren(updateModifiers(peer, modifiers))) } // @api @@ -1125,11 +1053,11 @@ export function createNodeFactory() { // ): MethodDeclaration; function updateMethodDeclaration( node: MethodDeclaration, - modifiers: undefined, // TODO: support (now default public, static) + modifiers: readonly Modifier[] | undefined, asteriskToken: undefined, name: Identifier, questionToken: undefined, - typeParameters: undefined, + typeParameters: readonly TypeParameterDeclaration[] | undefined, parameters: readonly ParameterDeclaration[], type: undefined, body: Block | undefined @@ -1146,17 +1074,6 @@ export function createNodeFactory() { body ).node.updateNode(node.node) ) - // const updated = createMethodDeclaration( - // modifiers, - // asteriskToken, - // name, - // questionToken, - // typeParameters, - // parameters, - // type, - // body - // ) - // return new MethodDeclaration(updateNode(updated.peer, node.peer)) } // @api @@ -1173,7 +1090,7 @@ export function createNodeFactory() { const _name = arkts.Identifier.create("constructor") return new ConstructorDeclaration( arkts.MethodDefinition.create( - Es2pandaMethodDefinitionKind.METHOD_DEFINITION_KIND_METHOD, + Es2pandaMethodDefinitionKind.METHOD_DEFINITION_KIND_CONSTRUCTOR, _name, arkts.FunctionExpression.create( arkts.ScriptFunction.create( @@ -1185,7 +1102,7 @@ export function createNodeFactory() { undefined ), passModifiersToScriptFunction(modifiers) | Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_CONSTRUCTOR, - passModifiers(modifiers) | Es2pandaModifierFlags.MODIFIER_FLAGS_CONSTRUCTOR, + passModifiers(modifiers) | Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC | Es2pandaModifierFlags.MODIFIER_FLAGS_CONSTRUCTOR, false, _name ) @@ -1194,47 +1111,6 @@ export function createNodeFactory() { false ) ) - // const peer = withNodeArray(parameters, (_parameters: BigUint64Array) => { - // const _context = global.context - - // const _signature = nativeModule._CreateFunctionSignature( - // _context, - // NULLPTR, - // _parameters, - // _parameters.length, - // // TODO: change void maybe - // NULLPTR - // ) - - // const _ident = nativeModule._CreateIdentifier1(_context, "constructor") - // const _scriptFunc = createScriptFunction( - // body?.peer ?? NULLPTR, - // _signature, - // Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_CONSTRUCTOR | - // passScriptFuncModifiers(modifiers), - // Es2pandaModifierFlags.MODIFIER_FLAGS_CONSTRUCTOR | - // passModifiers(modifiers), - // false, - // _ident - // ) - - // const _funcExpr = nativeModule._CreateFunctionExpression( - // global.context, - // _scriptFunc - // ) - // updateChildren(_funcExpr) - - // return nativeModule._CreateMethodDefinition( - // _context, - // Es2pandaMethodDefinitionKind.METHOD_DEFINITION_KIND_CONSTRUCTOR, - // _ident, - // _funcExpr, - // Es2pandaModifierFlags.MODIFIER_FLAGS_CONSTRUCTOR | - // passModifiers(modifiers), - // false - // ) - // }) - // return new ConstructorDeclaration(updateChildren(updateModifiers(peer, [...modifiers ?? [], Es2pandaModifierFlags.MODIFIER_FLAGS_CONSTRUCTOR]))) } // @api diff --git a/arkoala-arkts/libarkts/src/api/factory/nodeTests.ts b/arkoala-arkts/libarkts/src/api/factory/nodeTests.ts index 517d2adc30689db85ed05508bea67fcc679fd17c..fb4274b56c90a1a025397cfeb6e78c1230bd1085 100644 --- a/arkoala-arkts/libarkts/src/api/factory/nodeTests.ts +++ b/arkoala-arkts/libarkts/src/api/factory/nodeTests.ts @@ -12,22 +12,22 @@ import { PropertyAccessExpression, ParameterDeclaration, ReturnStatement, - // IfStatement, + IfStatement, ExpressionStatement, CallExpression, - // ArrowFunction, + ArrowFunction, TypeReferenceNode, - // BinaryExpression, + BinaryExpression, BinaryOperatorToken, - // ClassDeclaration, + ClassDeclaration, FunctionTypeNode, TypeNode, Expression, Statement, SourceFile, - // ClassElement, - // MethodDeclaration, - // FunctionExpression, + ClassElement, + MethodDeclaration, + FunctionExpression, // VariableDeclaration, // VariableDeclarationList, // PropertyDeclaration, @@ -54,9 +54,9 @@ export function isFunctionDeclaration(node: Node): node is FunctionDeclaration { return node.kind === SyntaxKind.FunctionDeclaration } -// export function isMethodDeclaration(node: Node): node is MethodDeclaration { -// return node.kind === SyntaxKind.MethodDeclaration -// } +export function isMethodDeclaration(node: Node): node is MethodDeclaration { + return node.kind === SyntaxKind.MethodDeclaration +} export function isSourceFile(node: Node): node is SourceFile { return node.kind === SyntaxKind.SourceFile @@ -66,21 +66,21 @@ export function isExpressionStatement(node: Node): node is ExpressionStatement { return node.kind === SyntaxKind.ExpressionStatement } -// export function isArrowFunction(node: Node): node is ArrowFunction { -// return node.kind === SyntaxKind.ArrowFunction -// } +export function isArrowFunction(node: Node): node is ArrowFunction { + return node.kind === SyntaxKind.ArrowFunction +} -// export function isClassDeclaration(node: Node): node is ClassDeclaration { -// return node.kind === SyntaxKind.ClassDeclaration -// } +export function isClassDeclaration(node: Node): node is ClassDeclaration { + return node.kind === SyntaxKind.ClassDeclaration +} export function isBlock(node: Node): node is Block { return node.kind === SyntaxKind.Block } -// export function isFunctionExpression(node: Node): node is FunctionExpression { -// return node.kind === SyntaxKind.FunctionExpression -// } +export function isFunctionExpression(node: Node): node is FunctionExpression { + return node.kind === SyntaxKind.FunctionExpression +} export function isParameter(node: Node): node is ParameterDeclaration { return node.kind === SyntaxKind.Parameter diff --git a/arkoala-arkts/libarkts/src/api/factory/utilities.ts b/arkoala-arkts/libarkts/src/api/factory/utilities.ts index 9a073b276bae3b763acd25c2d876b7d967cb8964..8d62af9c0a1cf55429925436dd94ea39f12ea167 100644 --- a/arkoala-arkts/libarkts/src/api/factory/utilities.ts +++ b/arkoala-arkts/libarkts/src/api/factory/utilities.ts @@ -13,46 +13,34 @@ * limitations under the License. */ -// import global from "../static/global" -// import * as arkts from "../layers/ts/types" -// import * as ets from "../layers/arkts/types" - -// import { nativeModule } from "../../NativeModule" -// import { KNativePointer, KInt, KBoolean } from "@koalaui/interop" -// import { -// TokenSyntaxKind, -// Es2pandaModifierFlags, -// Es2pandaScriptFunctionFlags, -// } from "../static/enums" +import global from "../static/global" +import * as ts from "../layers/ts/types" +import * as arkts from "../layers/arkts/types" + +import { nativeModule } from "../../NativeModule" +import { KNativePointer, KInt, KBoolean } from "@koalaui/interop" +import { + TokenSyntaxKind, + Es2pandaModifierFlags, + Es2pandaScriptFunctionFlags, +} from "../static/enums" +import { + passNodeArray, +} from "../layers/ts/utilities" // export function updateAllChildren(peer: KNativePointer) { // nativeModule._AstNodeUpdateAll(global.context, peer) // } - - - - - - - - - - - -// // TODO: support unrequired params (with ?) -// export function passTypeParams(params: readonly arkts.TypeParameterDeclaration[] | undefined): KNativePointer { -// if (params === undefined) { -// return arkts.NULLPTR -// } - -// const peer = withNodeArray(params, (_params: BigUint64Array) => { -// return nativeModule._CreateTSTypeParameterDeclaration(global.context, _params, _params.length, _params.length) -// }) -// updateChildren(peer) - -// return peer -// } +// TODO: support unrequired params (with ?) +export function passTypeParams(params: readonly ts.TypeParameterDeclaration[] | undefined): arkts.TSTypeParameterDeclaration | undefined { + if (params === undefined) { + return undefined + } + return arkts.TSTypeParameterDeclaration.create( + passNodeArray(params) + ) +} // export function identifierUpdateTypeAnnotation(node: arkts.Identifier, type: arkts.TypeNode | undefined): arkts.Identifier { // const peer = withString(node.text, (_text: string) => { diff --git a/arkoala-arkts/libarkts/src/api/index.ts b/arkoala-arkts/libarkts/src/api/index.ts index d2d8662f36b475134fad3edd7521a6a2dc90f7be..4b90c66f0f5365b5a3251ae4bfaa62ee7eb31274 100644 --- a/arkoala-arkts/libarkts/src/api/index.ts +++ b/arkoala-arkts/libarkts/src/api/index.ts @@ -20,9 +20,9 @@ export * from "./utilities/utilities" export * from "./factory/nodeFactory" export * from "./factory/nodeTests" export * from "./static/global" -// export { -// visitEachChild, -// } from "./visitor" +export { + visitEachChild, +} from "./visitor/visitor" export { SyntaxKind, ContextState, diff --git a/arkoala-arkts/libarkts/src/api/layers/arkts/index.ts b/arkoala-arkts/libarkts/src/api/layers/arkts/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..f7237d2d9a286d0f8a621788d3b0bdc4245b249d --- /dev/null +++ b/arkoala-arkts/libarkts/src/api/layers/arkts/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2022-2023 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. + */ + +export * from "./types" +export * from "./utilities" diff --git a/arkoala-arkts/libarkts/src/api/layers/arkts/types.ts b/arkoala-arkts/libarkts/src/api/layers/arkts/types.ts index e14190bb107a60946eb110bf96c30122a94182a4..aaa04fbe8cfebcc51779ee8fb261a0717ffd47cb 100644 --- a/arkoala-arkts/libarkts/src/api/layers/arkts/types.ts +++ b/arkoala-arkts/libarkts/src/api/layers/arkts/types.ts @@ -16,7 +16,7 @@ import global from "../../static/global" import { nativeModule } from "../../../NativeModule" -import { KInt, KNativePointer as KPtr, withStringResult, withStringArray } from "@koalaui/interop" +import { KInt, KNativePointer as KPtr } from "@koalaui/interop" import { Es2pandaPrimitiveType, Es2pandaModifierFlags, @@ -27,8 +27,8 @@ import { Es2pandaMethodDefinitionKind, } from "../../static/enums" import { + unpackNonNullableNode, unpackNode, - unpackNullableNode, passNode, unpackNodeArray, @@ -37,6 +37,9 @@ import { unpackObject, passString, + unpackString, + + passStringArray, } from "./utilities" import { proceedToState, @@ -85,7 +88,7 @@ export abstract class Node extends ArktsObject { readonly kind: es2pandaKind // TODO: update scopes and other data - updateNode(original: T): T { + updateNode(original: this): this { if (typeof this !== typeof original) { throw new Error('updateNode called on different type') } @@ -103,7 +106,7 @@ export abstract class Node extends ArktsObject { // } // TODO: rewrite - return this as unknown as T + return this } updateChildren(): void { @@ -113,16 +116,22 @@ export abstract class Node extends ArktsObject { nativeModule._AstNodeUpdateChildren(global.context, this.peer) } + updateModifiers(modifierFlags: KInt | undefined): this { + nativeModule._AstNodeClearModifier(global.context, this.peer, ALL_FLAGS) + nativeModule._AstNodeAddModifier(global.context, this.peer, modifierFlags ?? Es2pandaModifierFlags.MODIFIER_FLAGS_NONE) + return this + } + dumpJson(): string { - return withStringResult(nativeModule._AstNodeDumpJsonConst(global.context, this.peer)) ?? global.throwError('failed to dump Json') + return unpackString(nativeModule._AstNodeDumpJsonConst(global.context, this.peer)) } dumpSrc(): string { - return withStringResult(nativeModule._AstNodeDumpEtsSrcConst(global.context, this.peer)) ?? global.throwError('failed to dump Src') + return unpackString(nativeModule._AstNodeDumpEtsSrcConst(global.context, this.peer)) } dumpModifiers(): string { - return withStringResult(nativeModule._AstNodeDumpModifiers(global.context, this.peer)) ?? global.throwError('failed to dump ModifierFlags') + return unpackString(nativeModule._AstNodeDumpModifiers(global.context, this.peer)) } get parent(): Node { @@ -130,7 +139,7 @@ export abstract class Node extends ArktsObject { if (_parent === NULLPTR) { global.throwError('BAD ACCESS: PARENT IS NULLPTR') } - return unpackNode(_parent) + return unpackNonNullableNode(_parent) } set parent(node: Node) { @@ -198,9 +207,7 @@ export class Program extends Node { source: string ): Program { if (!global.isInitializedConfig()) { - global.config = withStringArray(["", "--arktsconfig", "./arktsconfig.json", "./input/main.sts"], (stringArray: string[]) => { - return nativeModule._CreateConfig(4, stringArray) - }) + global.config = nativeModule._CreateConfig(4, passStringArray(["", "--arktsconfig", "./arktsconfig.json", "./input/main.sts"])) } global.context = nativeModule._CreateContextFromString(global.config, passString(source), passString("./input/main.sts")) @@ -223,7 +230,7 @@ export class ExpressionStatement extends Node { constructor(peer: KPtr) { assertValidPeer(peer, es2pandaKind.ExpressionStatement) super(peer) - this.expression = unpackNode(nativeModule._ExpressionStatementGetExpression(global.context, this.peer)) + this.expression = unpackNonNullableNode(nativeModule._ExpressionStatementGetExpression(global.context, this.peer)) } static create(expression: Node): ExpressionStatement { @@ -237,7 +244,7 @@ export class CallExpression extends Node { constructor(peer: KPtr) { assertValidPeer(peer, es2pandaKind.CallExpression) super(peer) - this.expression = unpackNode(nativeModule._CallExpressionCallee(global.context, this.peer)) + this.expression = unpackNonNullableNode(nativeModule._CallExpressionCallee(global.context, this.peer)) this.arguments = unpackNodeArray(nativeModule._CallExpressionArguments(global.context, this.peer, NULLPTR)) } @@ -263,12 +270,34 @@ export class CallExpression extends Node { readonly arguments: readonly Node[] } +export class AssignmentExpression extends Node { + constructor(peer: KPtr) { + assertValidPeer(peer, es2pandaKind.AssignmentExpression) + super(peer) + } + + static create( + left: Node, + assignmentOperator: Es2pandaTokenType, + right: Node + ): AssignmentExpression { + return new AssignmentExpression( + nativeModule._CreateAssignmentExpression( + global.context, + passNode(left), + passNode(right), + assignmentOperator + ) + ) + } +} + export class MemberExpression extends Node { constructor(peer: KPtr) { assertValidPeer(peer, es2pandaKind.MemberExpression) super(peer) - this.object = unpackNode(nativeModule._MemberExpressionObject(global.context, this.peer)) - this.property = unpackNode(nativeModule._MemberExpressionProperty(global.context, this.peer)) + this.object = unpackNonNullableNode(nativeModule._MemberExpressionObject(global.context, this.peer)) + this.property = unpackNonNullableNode(nativeModule._MemberExpressionProperty(global.context, this.peer)) } static create( @@ -362,7 +391,7 @@ export class ETSTypeReferencePart extends Node { // TODO: support type params and prev static create(typeName: Identifier) { - typeName.setReference(true) + // typeName.setReference(true) // TODO: support type params and prev return new ETSTypeReferencePart( nativeModule._CreateETSTypeReferencePart( @@ -458,7 +487,7 @@ export class Identifier extends Node { // TODO: return (string | undefined) maybe get name(): string { - return withStringResult(nativeModule._IdentifierName(global.context, this.peer)) ?? global.throwError('name is undefined') + return unpackString(nativeModule._IdentifierName(global.context, this.peer)) } set name(name: string) { @@ -467,9 +496,9 @@ export class Identifier extends Node { readonly isPrivate: boolean - setReference(isRef: boolean): void { - nativeModule._IdentifierSetReference(global.context, this.peer, isRef) - } + // setReference(isRef: boolean): void { + // nativeModule._IdentifierSetReference(global.context, this.peer, isRef) + // } // TODO: // typeAnnotation() @@ -482,7 +511,7 @@ export class StringLiteral extends Node { constructor(peer: KPtr) { assertValidPeer(peer, es2pandaKind.StringLiteral) super(peer) - this.str = withStringResult(nativeModule._StringLiteralStrConst(global.context, this.peer)) ?? global.throwError('str is undefined') + this.str = unpackString(nativeModule._StringLiteralStrConst(global.context, this.peer)) } static create(str: string): StringLiteral { @@ -496,7 +525,7 @@ export class NumberLiteral extends Node { constructor(peer: KPtr) { assertValidPeer(peer, es2pandaKind.NumberLiteral) super(peer) - this.str = withStringResult(nativeModule._NumberLiteralStrConst(global.context, this.peer)) ?? global.throwError('str is undefined') + this.str = unpackString(nativeModule._NumberLiteralStrConst(global.context, this.peer)) } static create(str: string): NumberLiteral { @@ -509,9 +538,9 @@ export class NumberLiteral extends Node { export class FunctionSignature extends ArktsObject { constructor(peer: KPtr) { super(peer) - this.typeParamsDecl = unpackNullableNode(nativeModule._FunctionSignatureTypeParams(global.context, this.peer)) + this.typeParamsDecl = unpackNode(nativeModule._FunctionSignatureTypeParams(global.context, this.peer)) this.parameters = unpackNodeArray(nativeModule._FunctionSignatureParamsConst(global.context, this.peer, NULLPTR)) - this.returnTypeAnnotation = unpackNullableNode(nativeModule._FunctionSignatureReturnType(global.context, this.peer)) + this.returnTypeAnnotation = unpackNode(nativeModule._FunctionSignatureReturnType(global.context, this.peer)) } static create( @@ -539,6 +568,7 @@ export class ScriptFunction extends Node { constructor(peer: KPtr) { assertValidPeer(peer, es2pandaKind.ScriptFunction) super(peer) + this.parameters = unpackNodeArray(nativeModule._ScriptFunctionParams(global.context, this.peer)) this.body = unpackNode(nativeModule._ScriptFunctionBody(global.context, this.peer)) this.signature = unpackObject(FunctionSignature, nativeModule._ScriptFunctionSignature(global.context, this.peer)) this.declare = nativeModule._ScriptFunctionDeclareConst(global.context, this.peer) @@ -567,22 +597,44 @@ export class ScriptFunction extends Node { return new ScriptFunction(peer) } + readonly parameters: readonly ETSParameterExpression[] readonly body?: Node readonly signature?: FunctionSignature readonly declare: boolean readonly ident?: Identifier } +export class ArrowFunctionExpression extends Node { + constructor(peer: KPtr) { + assertValidPeer(peer, es2pandaKind.ArrowFunctionExpression) + super(peer) + this.scriptFunction = unpackNonNullableNode(nativeModule._ArrowFunctionExpressionFunction(global.context, this.peer)) + } + + static create( + func: ScriptFunction + ): ArrowFunctionExpression { + return new ArrowFunctionExpression( + nativeModule._CreateArrowFunctionExpression( + global.context, + passNode(func) + ) + ) + } + + readonly scriptFunction: ScriptFunction +} + export class FunctionDeclaration extends Node { constructor(peer: KPtr) { assertValidPeer(peer, es2pandaKind.FunctionDeclaration) super(peer) - this.scriptFunction = unpackNode(nativeModule._FunctionDeclarationFunction(global.context, this.peer)) + this.scriptFunction = unpackNonNullableNode(nativeModule._FunctionDeclarationFunction(global.context, this.peer)) this.parameters = unpackNodeArray(nativeModule._ScriptFunctionParams(global.context, this.scriptFunction.peer)) - this.name = unpackNullableNode(nativeModule._ScriptFunctionId(global.context, this.scriptFunction.peer)) - this.body = unpackNullableNode(nativeModule._ScriptFunctionBody(global.context, this.scriptFunction.peer)) - this.typeParamsDecl = unpackNullableNode(nativeModule._ScriptFunctionTypeParams(global.context, this.scriptFunction.peer)) - this.type = unpackNullableNode(nativeModule._ScriptFunctionReturnTypeAnnotation(global.context, this.scriptFunction.peer)) + this.name = unpackNode(nativeModule._ScriptFunctionId(global.context, this.scriptFunction.peer)) + this.body = unpackNode(nativeModule._ScriptFunctionBody(global.context, this.scriptFunction.peer)) + this.typeParamsDecl = unpackNode(nativeModule._ScriptFunctionTypeParams(global.context, this.scriptFunction.peer)) + this.type = unpackNode(nativeModule._ScriptFunctionReturnTypeAnnotation(global.context, this.scriptFunction.peer)) this.isAnon = nativeModule._FunctionDeclarationIsAnonymousConst(global.context, this.peer) } @@ -594,6 +646,9 @@ export class FunctionDeclaration extends Node { nativeModule._CreateFunctionDeclaration( global.context, scriptFunction.peer, + // TODO: support annotations + NULLPTR_ARRAY, + 0, isAnon ) ) @@ -615,7 +670,7 @@ export class FunctionExpression extends Node { constructor(peer: KPtr) { assertValidPeer(peer, es2pandaKind.FunctionExpression) super(peer) - this.scriptFunction = unpackNode(nativeModule._FunctionExpressionFunction(global.context, this.peer)) + this.scriptFunction = unpackNonNullableNode(nativeModule._FunctionExpressionFunction(global.context, this.peer)) } static create( @@ -703,7 +758,7 @@ export class TSTypeParameter extends Node { constructor(peer: KPtr) { assertValidPeer(peer, es2pandaKind.TSTypeParameter) super(peer) - this.name = unpackNode(nativeModule._TSTypeParameterName(global.context, this.peer)) + this.name = unpackNonNullableNode(nativeModule._TSTypeParameterName(global.context, this.peer)) } static create( @@ -730,7 +785,7 @@ export class ReturnStatement extends Node { constructor(peer: KPtr) { assertValidPeer(peer, es2pandaKind.ReturnStatement) super(peer) - this.argument = unpackNullableNode( + this.argument = unpackNode( nativeModule._ReturnStatementArgument(global.context, this.peer) ) } @@ -771,12 +826,66 @@ export class IfStatement extends Node { } } +export class ClassDeclaration extends Node { + constructor(peer: KPtr) { + assertValidPeer(peer, es2pandaKind.ClassDeclaration) + super(peer) + this.definition = unpackNonNullableNode(nativeModule._ClassDeclarationDefinition(global.context, this.peer)) + } + + static create( + definition: ClassDefinition + ): ClassDeclaration { + return new ClassDeclaration( + nativeModule._CreateClassDeclaration( + global.context, + passNode(definition) + ) + ) + } + + readonly definition: ClassDefinition +} + +export class ClassDefinition extends Node { + constructor(peer: KPtr) { + assertValidPeer(peer, es2pandaKind.ClassDefinition) + super(peer) + this.name = unpackNonNullableNode(nativeModule._ClassDefinitionIdent(global.context, this.peer)) + this.members = unpackNodeArray(nativeModule._ClassDefinitionBody(global.context, this.peer)) + this.typeParamsDecl = unpackNode(nativeModule._ClassDefinitionTypeParamsConst(global.context, this.peer)) + } + + static create( + name: Identifier | undefined, + members: Node[], + modifiers: KInt, + classFlags: KInt, + lang: undefined + ): ClassDefinition { + return new ClassDefinition( + nativeModule._CreateClassDefinition1( + global.context, + passNode(name), + passNodeArray(members), + members.length, + classFlags, + modifiers + ) + ) + } + + readonly name: Identifier + readonly members: readonly Node[] + readonly typeParamsDecl?: TSTypeParameterDeclaration +} + export class MethodDefinition extends Node { constructor(peer: KPtr) { assertValidPeer(peer, es2pandaKind.MethodDefinition) super(peer) - this.scriptFunction = unpackNode(nativeModule._MethodDefinitionFunction(global.context, this.peer)) - this.name = unpackNode(nativeModule._ScriptFunctionId(global.context, this.scriptFunction.peer)) + this.scriptFunction = unpackNonNullableNode(nativeModule._MethodDefinitionFunction(global.context, this.peer)) + this.name = unpackNonNullableNode(nativeModule._ScriptFunctionId(global.context, this.scriptFunction.peer)) } static create( @@ -801,3 +910,29 @@ export class MethodDefinition extends Node { readonly scriptFunction: ScriptFunction readonly name: Identifier } + +export class ClassProperty extends Node { + constructor(peer: KPtr) { + assertValidPeer(peer, es2pandaKind.ClassProperty) + super(peer) + } + + static create( + key: Node, + value: Node, + typeAnnotation: Node, + modifiers: KInt, + isComputed: boolean + ): ClassProperty { + return new ClassProperty( + nativeModule._CreateClassProperty( + global.context, + passNode(key), + passNode(value), + passNode(typeAnnotation), + modifiers, + isComputed + ) + ) + } +} diff --git a/arkoala-arkts/libarkts/src/api/layers/arkts/utilities.ts b/arkoala-arkts/libarkts/src/api/layers/arkts/utilities.ts index 8884cca1b3b08a286baba926237bb1e68fe8a9d2..52ab99579f9e7b3f74bf96e80b147b9b3cbba577 100644 --- a/arkoala-arkts/libarkts/src/api/layers/arkts/utilities.ts +++ b/arkoala-arkts/libarkts/src/api/layers/arkts/utilities.ts @@ -16,7 +16,7 @@ import * as arkts from "./types" import global from "../../static/global" -import { KNativePointer, withString, fromPtrArray } from "@koalaui/interop" +import { KNativePointer, withString, fromPtrArray, withStringResult, withStringArray } from "@koalaui/interop" import { nativeModule } from "../../../NativeModule" import { NativePtrDecoder } from "../../../node/Platform" import { @@ -48,20 +48,26 @@ export function classByPeer(peer: KNativePointer): { new (peer: KNativePointer): [es2pandaKind.IfStatement, arkts.IfStatement], [es2pandaKind.BinaryExpression, arkts.BinaryExpression], [es2pandaKind.ETSUnionType, arkts.ETSUnionType], + [es2pandaKind.ArrowFunctionExpression, arkts.ArrowFunctionExpression], + [es2pandaKind.ClassDeclaration, arkts.ClassDeclaration], + [es2pandaKind.ClassDefinition, arkts.ClassDefinition], + [es2pandaKind.MethodDefinition, arkts.MethodDefinition], + [es2pandaKind.ClassProperty, arkts.ClassProperty], + [es2pandaKind.AssignmentExpression, arkts.AssignmentExpression], ]) const kind = nativeModule._GetKind(global.context, peer) return kinds.get(kind) ?? global.throwError(`UNSUPPORTED NODE (arkts): ${es2pandaKind[kind]}`) } -export function unpackNode(peer: KNativePointer): T { +export function unpackNonNullableNode(peer: KNativePointer): T { if (peer === arkts.NULLPTR) { - global.throwError('peer is NULLPTR') + global.throwError('peer is NULLPTR (maybe you should use unpackNode)') } return (new (classByPeer(peer))(peer)) as T } -export function unpackNullableNode(peer: KNativePointer): T | undefined { +export function unpackNode(peer: KNativePointer): T | undefined { if (peer === arkts.NULLPTR) { return undefined } @@ -72,14 +78,15 @@ export function passNode(node: arkts.Node | undefined): KNativePointer { return node?.peer ?? arkts.NULLPTR } +// meaning unpackNonNullableNodeArray export function unpackNodeArray(nodesPtr: KNativePointer): readonly T[] { if (nodesPtr === arkts.NULLPTR) { - return [] + global.throwError('nodesPtr is NULLPTR (maybe you should use unpackNodeArray)') } const array = (new NativePtrDecoder).decode(nodesPtr) return fromPtrArray(array, (peer: KNativePointer) => { - return unpackNode(peer) + return unpackNonNullableNode(peer) }) } @@ -94,6 +101,10 @@ export function unpackObject(type: { new (peer: KNa return new type(peer) } +export function unpackString(peer: KNativePointer): string { + return withStringResult(peer) ?? global.throwError(`failed to unpack (peer shouldn't be NULLPTR)`) +} + export function passString(str: string | undefined): string { if (str === undefined) { return "" @@ -101,6 +112,10 @@ export function passString(str: string | undefined): string { return withString(str, (_str: string) => _str) } +export function passStringArray(strs: string[]): string[] { + return withStringArray(strs, (_strs: string[]) => _strs) +} + export function scriptFunctionHasBody(peer: KNativePointer): boolean { const flags = nativeModule._ScriptFunctionFlagsConst(global.context, peer) return true diff --git a/arkoala-arkts/libarkts/src/api/layers/ts/index.ts b/arkoala-arkts/libarkts/src/api/layers/ts/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..f7237d2d9a286d0f8a621788d3b0bdc4245b249d --- /dev/null +++ b/arkoala-arkts/libarkts/src/api/layers/ts/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2022-2023 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. + */ + +export * from "./types" +export * from "./utilities" diff --git a/arkoala-arkts/libarkts/src/api/layers/ts/types.ts b/arkoala-arkts/libarkts/src/api/layers/ts/types.ts index 1c9213577bfe19325445ed5f808c331ed0ba2b4f..ded166dfed5030d7ac4752feac883bb135cda115 100644 --- a/arkoala-arkts/libarkts/src/api/layers/ts/types.ts +++ b/arkoala-arkts/libarkts/src/api/layers/ts/types.ts @@ -395,35 +395,25 @@ export class StringLiteral extends Node implements ts.Strin _expressionBrand: any _declarationBrand: any } -/* -export class ClassDeclaration extends NodeBase implements ts.ClassDeclaration { - constructor(peer: KNativePointer) { - super(peer) - this._definition = nativeModule._ClassDeclarationDefinition(global.context, peer) - this.members = util.unpack(nativeModule._ClassDefinitionBody(global.context, this._definition)) as NodeArray - this.name = util.nodeByPeer(nativeModule._ClassDefinitionIdent(global.context, this._definition)) as Identifier - - const _typeParamDecl = nativeModule._ClassDefinitionTypeParamsConst(global.context, this._definition) - this._typeParameters = (_typeParamDecl !== NULLPTR) ? nativeModule._TSTypeParameterDeclarationParamsConst(global.context, _typeParamDecl, NULLPTR) : NULLPTR - this.typeParameters = (this._typeParameters !== NULLPTR) ? (util.unpack(this._typeParameters) as NodeArray) : undefined +export class ClassDeclaration extends Node implements ts.ClassDeclaration { + constructor(node: arkts.ClassDeclaration) { + super(node) + this.name = unpackNode(node.definition.name) + this.members = unpackNodeArray(node.definition.members) + this.typeParameters = unpackNodeArray(node.definition.typeParamsDecl?.parameters) } - private _definition - - readonly members: NodeArray - readonly name: Identifier - - private readonly _typeParameters: KNativePointer - readonly typeParameters?: NodeArray | undefined; + readonly members: NodeArray + readonly typeParameters?: NodeArray + readonly kind: ts.SyntaxKind.ClassDeclaration = ts.SyntaxKind.ClassDeclaration // TODO: support minimal interface _declarationBrand: any _statementBrand: any - kind: ts.SyntaxKind.ClassDeclaration = ts.SyntaxKind.ClassDeclaration } -*/ + export abstract class ClassElement extends Node implements ts.ClassElement { readonly name?: PropertyName @@ -520,10 +510,7 @@ export class MethodDeclaration extends Node implements t constructor(node: arkts.MethodDefinition) { super(node) this.name = unpackNode(node.name) - if (node.scriptFunction.signature === undefined) { - global.throwError('node.scriptFunction.signature not expected to be undefined') - } - this.parameters = unpackNodeArray(node.scriptFunction.signature.parameters) + this.parameters = unpackNodeArray(node.scriptFunction.parameters) this.body = unpackNode(node.scriptFunction.body) } @@ -547,10 +534,7 @@ export class ConstructorDeclaration extends Node impleme constructor(node: arkts.MethodDefinition) { super(node) this.name = unpackNode(node.name) - if (node.scriptFunction.signature === undefined) { - global.throwError('node.scriptFunction.signature not expected to be undefined') - } - this.parameters = unpackNodeArray(node.scriptFunction.signature.parameters) + this.parameters = unpackNodeArray(node.scriptFunction.parameters) this.body = unpackNode(node.scriptFunction.body) } @@ -848,33 +832,31 @@ export class AssignmentExpression extends NodeBase implements ts.AssignmentExpre readonly operatorToken: any kind: ts.SyntaxKind.BinaryExpression = ts.SyntaxKind.BinaryExpression } - -export class ArrowFunction extends NodeBase implements ts.ArrowFunction { - constructor(peer: KNativePointer) { - super(peer) - - this._scriptFunction = nativeModule._ArrowFunctionExpressionFunction(global.context, this.peer) - this.parameters = util.unpack(nativeModule._ScriptFunctionParams(global.context, this._scriptFunction)) as NodeArray - this.body = util.nodeByPeer(nativeModule._ScriptFunctionBody(global.context, this._scriptFunction)) as Block - } - - get name(): never { - return this.name +*/ +export class ArrowFunction extends Node implements ts.ArrowFunction { + constructor(node: arkts.ArrowFunctionExpression) { + super(node) + if (node.scriptFunction.body === undefined) { + global.throwError('node.scriptFunction.body not expected to be undefined') + } + this.body = unpackNode(node.scriptFunction.body) + this.parameters = unpackNodeArray(node.scriptFunction.parameters) } + readonly body: Block readonly parameters: NodeArray - body: Block - - private _scriptFunction: KNativePointer + readonly kind: ts.SyntaxKind.ArrowFunction = ts.SyntaxKind.ArrowFunction // TODO: support minimal interface equalsGreaterThanToken: any _expressionBrand: any _functionLikeDeclarationBrand: any _declarationBrand: any - readonly kind: ts.SyntaxKind.ArrowFunction = ts.SyntaxKind.ArrowFunction + get name(): never { + return global.throwError(`name doesn't exist for ArrowFunction`) + } } -*/ + export class NumericLiteral extends Node implements ts.NumericLiteral, Declaration { constructor(node: arkts.NumberLiteral) { super(node) diff --git a/arkoala-arkts/libarkts/src/api/layers/ts/utilities.ts b/arkoala-arkts/libarkts/src/api/layers/ts/utilities.ts index a9149a832957ad8144027d687ce32d0853f6be90..4a7fea601b74d71ceb82f65c55689fd780431aa0 100644 --- a/arkoala-arkts/libarkts/src/api/layers/ts/utilities.ts +++ b/arkoala-arkts/libarkts/src/api/layers/ts/utilities.ts @@ -48,6 +48,9 @@ type kindTypes = | { new (node: arkts.ETSPrimitiveType | arkts.ETSTypeReference): ts.KeywordTypeNode } | { new (node: arkts.BinaryExpression): ts.BinaryExpression } | { new (node: arkts.ETSUnionType): ts.UnionTypeNode } + | { new (node: arkts.ArrowFunctionExpression): ts.ArrowFunction } + | { new (node: arkts.ClassDeclaration): ts.ClassDeclaration } + | { new (node: arkts.MethodDefinition): ts.MethodDeclaration } export function classByEtsNode(node: arkts.Node) { const kinds = @@ -71,6 +74,9 @@ export function classByEtsNode(node: arkts.Node) { [es2pandaKind.ETSPrimitiveType, ts.KeywordTypeNode], [es2pandaKind.BinaryExpression, ts.BinaryExpression], [es2pandaKind.ETSUnionType, ts.UnionTypeNode], + [es2pandaKind.ArrowFunctionExpression, ts.ArrowFunction], + [es2pandaKind.ClassDeclaration, ts.ClassDeclaration], + [es2pandaKind.MethodDefinition, ts.MethodDeclaration], ]) return kinds.get(node.kind) ?? global.throwError(`UNSUPPORTED NODE (ts): ${es2pandaKind[node.kind]}`) diff --git a/arkoala-arkts/libarkts/src/api/static/enums.ts b/arkoala-arkts/libarkts/src/api/static/enums.ts index 1011a1698dc797ec62c9626c949676b2661227f0..a43d1f3477ca3b0550c6544e7379f07afbc4abef 100644 --- a/arkoala-arkts/libarkts/src/api/static/enums.ts +++ b/arkoala-arkts/libarkts/src/api/static/enums.ts @@ -38,7 +38,7 @@ export enum es2pandaKind { MethodDefinition = 4, ClassDeclaration = 5, FunctionDeclaration = 6, - PropertyDeclaration = 7, + ClassProperty = 7, TSTypeParameterDeclaration = 8, ETSFunctionType = 9, CallExpression = 10, diff --git a/arkoala-arkts/libarkts/src/api/visitor/visitor.ts b/arkoala-arkts/libarkts/src/api/visitor/visitor.ts index 72492c4fac6ff07f2f216b50db9d51b20568e738..9940cbd74f1c1a63d2f8c69af01c4783c2ab9f12 100644 --- a/arkoala-arkts/libarkts/src/api/visitor/visitor.ts +++ b/arkoala-arkts/libarkts/src/api/visitor/visitor.ts @@ -1,13 +1,13 @@ -/* -import * as arkts from "./types" -import { factory } from "./factory/nodeFactory" -import * as util from "./utilities" -import { SyntaxKind } from "./enums" +import global from "../static/global" -type Visitor = (node: arkts.Node) => arkts.Node +import * as ts from "../layers/ts" +import { factory } from "../factory/nodeFactory" +import { SyntaxKind } from "../static/enums" + +type Visitor = (node: ts.Node) => ts.Node // TODO: rethink (remove as) -function nodeVisitor(node: T, visitor: Visitor): T { +function nodeVisitor(node: T, visitor: Visitor): T { if (node === undefined) { return node } @@ -15,39 +15,39 @@ function nodeVisitor(node: T, visitor: Visitor } // TODO: rethink (remove as) -function nodesVisitor | undefined>(nodes: TIn, visitor: Visitor): T[] | TIn { +function nodesVisitor | undefined>(nodes: TIn, visitor: Visitor): T[] | TIn { if (nodes === undefined) { return nodes } return nodes.map(node => visitor(node) as T) } -type VisitEachChildFunction = (node: T, visitor: Visitor) => T +type VisitEachChildFunction = (node: T, visitor: Visitor) => T // TODO: add more nodes type HasChildren = - | arkts.SourceFile - | arkts.FunctionDeclaration - | arkts.ExpressionStatement - | arkts.CallExpression - | arkts.PropertyAccessExpression - | arkts.ClassDeclaration - | arkts.MethodDeclaration - | arkts.Block - | arkts.VariableStatement - | arkts.VariableDeclarationList + | ts.SourceFile + | ts.FunctionDeclaration + | ts.ExpressionStatement + | ts.CallExpression + | ts.PropertyAccessExpression + | ts.ClassDeclaration + | ts.MethodDeclaration + | ts.Block + // | ts.VariableStatement + // | ts.VariableDeclarationList type VisitEachChildTable = { [TNode in HasChildren as TNode["kind"]]: VisitEachChildFunction } // TODO: add more nodes const visitEachChildTable: VisitEachChildTable = { - [SyntaxKind.SourceFile]: function (node: arkts.SourceFile, visitor: Visitor) { + [SyntaxKind.SourceFile]: function (node: ts.SourceFile, visitor: Visitor) { return factory.updateSourceFile( node, nodesVisitor(node.statements, visitor) ) }, - [SyntaxKind.FunctionDeclaration]: function (node: arkts.FunctionDeclaration, visitor: Visitor) { + [SyntaxKind.FunctionDeclaration]: function (node: ts.FunctionDeclaration, visitor: Visitor) { return factory.updateFunctionDeclaration( node, node.modifiers, @@ -59,13 +59,13 @@ const visitEachChildTable: VisitEachChildTable = { nodeVisitor(node.body, visitor), ) }, - [SyntaxKind.ExpressionStatement]: function (node: arkts.ExpressionStatement, visitor: Visitor) { + [SyntaxKind.ExpressionStatement]: function (node: ts.ExpressionStatement, visitor: Visitor) { return factory.updateExpressionStatement( node, nodeVisitor(node.expression, visitor) ) }, - [SyntaxKind.CallExpression]: function (node: arkts.CallExpression, visitor: Visitor) { + [SyntaxKind.CallExpression]: function (node: ts.CallExpression, visitor: Visitor) { return factory.updateCallExpression( node, nodeVisitor(node.expression, visitor), @@ -73,14 +73,14 @@ const visitEachChildTable: VisitEachChildTable = { nodesVisitor(node.arguments, visitor) ) }, - [SyntaxKind.PropertyAccessExpression]: function (node: arkts.PropertyAccessExpression, visitor: Visitor) { + [SyntaxKind.PropertyAccessExpression]: function (node: ts.PropertyAccessExpression, visitor: Visitor) { return factory.updatePropertyAccessExpression( node, nodeVisitor(node.expression, visitor), nodeVisitor(node.name, visitor) ) }, - [SyntaxKind.ClassDeclaration]: function (node: arkts.ClassDeclaration, visitor: Visitor) { + [SyntaxKind.ClassDeclaration]: function (node: ts.ClassDeclaration, visitor: Visitor) { return factory.updateClassDeclaration( node, undefined, @@ -90,7 +90,7 @@ const visitEachChildTable: VisitEachChildTable = { nodesVisitor(node.members, visitor) ) }, - [SyntaxKind.MethodDeclaration]: function (node: arkts.MethodDeclaration, visitor: Visitor) { + [SyntaxKind.MethodDeclaration]: function (node: ts.MethodDeclaration, visitor: Visitor) { return factory.updateMethodDeclaration( node, undefined, @@ -103,39 +103,38 @@ const visitEachChildTable: VisitEachChildTable = { nodeVisitor(node.body, visitor), ) }, - [SyntaxKind.Block]: function (node: arkts.Block, visitor: Visitor) { + [SyntaxKind.Block]: function (node: ts.Block, visitor: Visitor) { return factory.updateBlock( node, nodesVisitor(node.statements, visitor), ) }, - [SyntaxKind.VariableStatement]: function (node: arkts.VariableStatement, visitor: Visitor) { - return factory.updateVariableStatement( - node, - undefined, - nodeVisitor(node.declarationList, visitor), - ) - }, - [SyntaxKind.VariableDeclarationList]: function (node: arkts.VariableDeclarationList, visitor: Visitor) { - return factory.updateVariableDeclarationList( - node, - nodesVisitor(node.declarations, visitor), - ) - }, + // [SyntaxKind.VariableStatement]: function (node: ts.VariableStatement, visitor: Visitor) { + // return factory.updateVariableStatement( + // node, + // undefined, + // nodeVisitor(node.declarationList, visitor), + // ) + // }, + // [SyntaxKind.VariableDeclarationList]: function (node: ts.VariableDeclarationList, visitor: Visitor) { + // return factory.updateVariableDeclarationList( + // node, + // nodesVisitor(node.declarations, visitor), + // ) + // }, } -function nodeHasChildren(node: arkts.Node): node is HasChildren { +function nodeHasChildren(node: ts.Node): node is HasChildren { return node.kind in visitEachChildTable } -export function visitEachChild( +export function visitEachChild( node: T, visitor: Visitor ): T { const visitFunc = (visitEachChildTable as Record | undefined>)[node.kind]; if (nodeHasChildren(node) && visitFunc === undefined) { - util.throwError('Unsupported node kind: ' + node.kind) + global.throwError('Unsupported node kind: ' + node.kind) } return (visitFunc === undefined) ? node : visitFunc(node, visitor); } -*/ \ No newline at end of file diff --git a/arkoala-arkts/libarkts/test/es2panda/abc-gen.test.ts b/arkoala-arkts/libarkts/test/es2panda/abc-gen.test.ts index 91662b7bc95ff90070a7620cdbc7c406dee3a59b..b4b78d0846406b447953884fdc3f7a3d8e6cf9f0 100644 --- a/arkoala-arkts/libarkts/test/es2panda/abc-gen.test.ts +++ b/arkoala-arkts/libarkts/test/es2panda/abc-gen.test.ts @@ -1,10 +1,9 @@ -/* import * as util from "../test-util" -import * as arkts from "../../src/arkts" +import * as ts from "../../src/api" // tests for abc generation suite(util.getSuiteTitle(__filename), () => { - test.only("updating-expression-statement", function() { + test("updating-expression-statement", function() { const sample_in = ` function foo(lambda: (instance: string) => string): void { @@ -14,27 +13,23 @@ suite(util.getSuiteTitle(__filename), () => { foo((instance: string) => { return instance }) ` - util.getDefaultSetup(sample_in) + let sourceFile = ts.factory.createSourceFile(sample_in) + util.assert(ts.isSourceFile(sourceFile)) - const sourceFile = arkts.nodeByPeer(util.AstProvider.provideAst()) - util.assert(arkts.isSourceFile(sourceFile)) + const expressionStatement = sourceFile.statements[1] + util.assert(ts.isExpressionStatement(expressionStatement)) - console.log(arkts.dumpJsonNode(sourceFile)) - - // const expressionStatement = sourceFile.statements[1] - // util.assert(arkts.isExpressionStatement(expressionStatement)) - - // const newStatements = [ - // sourceFile.statements[0], - // arkts.factory.updateExpressionStatement( - // expressionStatement, - // expressionStatement.expression - // ) - // ] + const newStatements = [ + sourceFile.statements[0], + ts.factory.updateExpressionStatement( + expressionStatement, + expressionStatement.expression + ) + ] - // arkts.factory.updateSourceFile(sourceFile, newStatements) + ts.factory.updateSourceFile(sourceFile, newStatements) - // util.assertEqualsBinaryOuptut('ABC', this) + util.assertEqualsBinaryOuptut('ABC', this) }) test("updating-function-declaration", function() { @@ -48,16 +43,14 @@ suite(util.getSuiteTitle(__filename), () => { foo() ` - util.getDefaultSetup(sample_in) - - const sourceFile = arkts.nodeByPeer(util.AstProvider.provideAst()) - util.assert(arkts.isSourceFile(sourceFile)) + let sourceFile = ts.factory.createSourceFile(sample_in) + util.assert(ts.isSourceFile(sourceFile)) const funcDecl = sourceFile.statements[0] - util.assert(arkts.isFunctionDeclaration(funcDecl)) + util.assert(ts.isFunctionDeclaration(funcDecl)) const newStatements = [ - arkts.factory.updateFunctionDeclaration( + ts.factory.updateFunctionDeclaration( funcDecl, undefined, undefined, @@ -70,7 +63,7 @@ suite(util.getSuiteTitle(__filename), () => { sourceFile.statements[1], ] - arkts.factory.updateSourceFile(sourceFile, newStatements) + ts.factory.updateSourceFile(sourceFile, newStatements) util.assertEqualsBinaryOuptut('A', this) }) @@ -82,18 +75,16 @@ suite(util.getSuiteTitle(__filename), () => { foo(() => {}) ` - util.getDefaultSetup(sample_in) - - const sourceFile = arkts.nodeByPeer(util.AstProvider.provideAst()) - util.assert(arkts.isSourceFile(sourceFile)) + let sourceFile = ts.factory.createSourceFile(sample_in) + util.assert(ts.isSourceFile(sourceFile)) const exprStatement = sourceFile.statements[1] - util.assert(arkts.isExpressionStatement(exprStatement)) + util.assert(ts.isExpressionStatement(exprStatement)) const callExpr = exprStatement.expression - util.assert(arkts.isCallExpression(callExpr)) + util.assert(ts.isCallExpression(callExpr)) - util.assert(arkts.isArrowFunction(callExpr.arguments[0])) + util.assert(ts.isArrowFunction(callExpr.arguments[0])) const lambdaArg = - arkts.factory.createArrowFunction( + ts.factory.createArrowFunction( undefined, undefined, [], @@ -102,14 +93,14 @@ suite(util.getSuiteTitle(__filename), () => { callExpr.arguments[0].body ) - util.assert(arkts.isIdentifier(callExpr.expression)) + util.assert(ts.isIdentifier(callExpr.expression)) const newStatements = [ sourceFile.statements[0], - arkts.factory.updateExpressionStatement( + ts.factory.updateExpressionStatement( exprStatement, - arkts.factory.updateCallExpression( + ts.factory.updateCallExpression( callExpr, - arkts.factory.updateIdentifier(callExpr.expression, 'foo'), + ts.factory.createIdentifier('foo'), undefined, [ lambdaArg, @@ -117,9 +108,8 @@ suite(util.getSuiteTitle(__filename), () => { ) ) ] - arkts.factory.updateSourceFile(sourceFile, newStatements) + ts.factory.updateSourceFile(sourceFile, newStatements) util.assertEqualsBinaryOuptut('', this) }) }) -*/ \ No newline at end of file diff --git a/arkoala-arkts/libarkts/test/es2panda/basic.test.ts b/arkoala-arkts/libarkts/test/es2panda/basic.test.ts index bd6a231bf8d72e51c37a4ff34a5fd08e6ecab06e..7fbfae8af30f4be00fedd98e3a7956e79bac90c0 100644 --- a/arkoala-arkts/libarkts/test/es2panda/basic.test.ts +++ b/arkoala-arkts/libarkts/test/es2panda/basic.test.ts @@ -9,6 +9,7 @@ suite(util.getSuiteTitle(__filename), () => { ` let sourceFile = factory.createSourceFile(sample_in) + util.assert(ts.isSourceFile(sourceFile)) sourceFile = factory.updateSourceFile( sourceFile, @@ -23,7 +24,8 @@ suite(util.getSuiteTitle(__filename), () => { sourceFile, ` abc - ` + `, + ts.ContextState.ES2PANDA_STATE_PARSED ) }) @@ -33,6 +35,7 @@ suite(util.getSuiteTitle(__filename), () => { ` let sourceFile = factory.createSourceFile(sample_in) + util.assert(ts.isSourceFile(sourceFile)) sourceFile = factory.updateSourceFile( sourceFile, @@ -56,7 +59,8 @@ suite(util.getSuiteTitle(__filename), () => { sourceFile, ` function test() {} - ` + `, + ts.ContextState.ES2PANDA_STATE_PARSED ) }) @@ -66,6 +70,7 @@ suite(util.getSuiteTitle(__filename), () => { ` let sourceFile = factory.createSourceFile(sample_in) + util.assert(ts.isSourceFile(sourceFile)) sourceFile = factory.updateSourceFile( sourceFile, @@ -104,7 +109,8 @@ suite(util.getSuiteTitle(__filename), () => { function test(x: string): number { return 0; } - ` + `, + ts.ContextState.ES2PANDA_STATE_PARSED ) }) @@ -114,6 +120,7 @@ suite(util.getSuiteTitle(__filename), () => { ` let sourceFile = factory.createSourceFile(sample_in) + util.assert(ts.isSourceFile(sourceFile)) sourceFile = factory.updateSourceFile( sourceFile, @@ -162,7 +169,8 @@ suite(util.getSuiteTitle(__filename), () => { function test(x: T): string { return "aaa" } - ` + `, + ts.ContextState.ES2PANDA_STATE_PARSED ) }) }) diff --git a/arkoala-arkts/libarkts/test/es2panda/builder-lambda-rewrite.test.ts b/arkoala-arkts/libarkts/test/es2panda/builder-lambda-rewrite.test.ts index 4fa6c351a95096e6186f3e25d2da57533aa3dc8d..f6eb521c5517a4c685789cad7b6d80d735edd03c 100644 --- a/arkoala-arkts/libarkts/test/es2panda/builder-lambda-rewrite.test.ts +++ b/arkoala-arkts/libarkts/test/es2panda/builder-lambda-rewrite.test.ts @@ -1,6 +1,6 @@ -/* import * as util from "../test-util" -import * as arkts from "../../src/arkts" +import * as ts from "../../src/api" +import { factory } from "../../src/api" import { BuilderLambdaTransformer } from "../../compatible/src/builder-lambda-transformer" suite(util.getSuiteTitle(__filename), () => { @@ -18,32 +18,30 @@ suite(util.getSuiteTitle(__filename), () => { } ` - util.getDefaultSetup(sample_in) - - let sourceFile = arkts.nodeByPeer(util.AstProvider.provideAst()) - util.assert(arkts.isSourceFile(sourceFile)) + let sourceFile = factory.createSourceFile(sample_in) + util.assert(ts.isSourceFile(sourceFile)) const newName = "_Foo" const paramName = "instance" - const firstStatement = arkts.nodeByPeer(util.getStatement(0)) - util.assert(arkts.isExpressionStatement(firstStatement)) + const firstStatement = sourceFile.statements[0] + util.assert(ts.isExpressionStatement(firstStatement)) const node = firstStatement.expression - util.assert(arkts.isCallExpression(node)) + util.assert(ts.isCallExpression(node)) - const instanceLambdaBody = arkts.factory.createBlock([]) + const instanceLambdaBody = ts.factory.createBlock([]) const lambdaParams = [ - arkts.factory.createParameterDeclaration( + ts.factory.createParameterDeclaration( undefined, undefined, - arkts.factory.createIdentifier(paramName), + ts.factory.createIdentifier(paramName), undefined, - arkts.factory.createKeywordTypeNode(arkts.SyntaxKind.StringKeyword), + ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword), undefined ) ] - const lambda = arkts.factory.createArrowFunction( + const lambda = ts.factory.createArrowFunction( undefined, undefined, lambdaParams, @@ -52,9 +50,9 @@ suite(util.getSuiteTitle(__filename), () => { instanceLambdaBody ) - const result = arkts.factory.updateCallExpression( + const result = ts.factory.updateCallExpression( node, - arkts.factory.createIdentifier(newName), + ts.factory.createIdentifier(newName), undefined, [ lambda, @@ -62,10 +60,10 @@ suite(util.getSuiteTitle(__filename), () => { ] ) - sourceFile = arkts.factory.updateSourceFile( + sourceFile = ts.factory.updateSourceFile( sourceFile, [ - arkts.factory.createExpressionStatement(result) + ts.factory.createExpressionStatement(result) ] ) @@ -74,7 +72,7 @@ suite(util.getSuiteTitle(__filename), () => { ` _Foo(((instance: string) => {}), "label") `, - arkts.ContextState.ES2PANDA_STATE_PARSED, + ts.ContextState.ES2PANDA_STATE_PARSED, ) }) @@ -88,37 +86,35 @@ suite(util.getSuiteTitle(__filename), () => { Foo(instance.bar().qux(), "label1", "label2") ` - util.getDefaultSetup(sample_in) - - let sourceFile = arkts.nodeByPeer(util.AstProvider.provideAst()) - util.assert(arkts.isSourceFile(sourceFile)) + let sourceFile = factory.createSourceFile(sample_in) + util.assert(ts.isSourceFile(sourceFile)) const newName = "_Foo" const paramName = "instance" - const firstStatement = arkts.nodeByPeer(util.getStatement(0)) - util.assert(arkts.isExpressionStatement(firstStatement)) + const firstStatement = sourceFile.statements[0] + util.assert(ts.isExpressionStatement(firstStatement)) const node = firstStatement.expression - util.assert(arkts.isCallExpression(node)) + util.assert(ts.isCallExpression(node)) - const instanceLambdaBody = arkts.factory.createBlock([ - arkts.factory.createExpressionStatement( + const instanceLambdaBody = ts.factory.createBlock([ + ts.factory.createExpressionStatement( node.arguments[0] ) ]) const lambdaParams = [ - arkts.factory.createParameterDeclaration( + ts.factory.createParameterDeclaration( undefined, undefined, - arkts.factory.createIdentifier(paramName), + ts.factory.createIdentifier(paramName), undefined, - arkts.factory.createKeywordTypeNode(arkts.SyntaxKind.StringKeyword), + ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword), undefined ) ] - const lambda = arkts.factory.createArrowFunction( + const lambda = ts.factory.createArrowFunction( undefined, undefined, lambdaParams, @@ -127,9 +123,9 @@ suite(util.getSuiteTitle(__filename), () => { instanceLambdaBody ) - const result = arkts.factory.updateCallExpression( + const result = ts.factory.updateCallExpression( node, - arkts.factory.createIdentifier(newName), + ts.factory.createIdentifier(newName), undefined, [ lambda, @@ -137,10 +133,10 @@ suite(util.getSuiteTitle(__filename), () => { ] ) - sourceFile = arkts.factory.updateSourceFile( + sourceFile = ts.factory.updateSourceFile( sourceFile, [ - arkts.factory.createExpressionStatement(result) + ts.factory.createExpressionStatement(result) ] ) @@ -151,7 +147,7 @@ suite(util.getSuiteTitle(__filename), () => { instance.bar().qux(); }), "label1", "label2") `, - arkts.ContextState.ES2PANDA_STATE_PARSED, + ts.ContextState.ES2PANDA_STATE_PARSED, ) }) @@ -165,23 +161,22 @@ suite(util.getSuiteTitle(__filename), () => { _BuilderLambdaCall_foo("label") ` - util.getDefaultSetup(sample_in) - - let sourceFile = arkts.nodeByPeer(util.AstProvider.provideAst()) - util.assert(arkts.isSourceFile(sourceFile)) + let sourceFile = factory.createSourceFile(sample_in) + util.assert(ts.isSourceFile(sourceFile)) const builderLambdaTransformer = new BuilderLambdaTransformer() - sourceFile = builderLambdaTransformer.visitor(sourceFile) + const result = builderLambdaTransformer.visitor(sourceFile) + util.assert(ts.isSourceFile(result)) util.assertEqualsAfter( - sourceFile, + result, ` foo(((instance: string) => { return instance; }), "label") `, - arkts.ContextState.ES2PANDA_STATE_PARSED, + ts.ContextState.ES2PANDA_STATE_PARSED, ) }) @@ -195,23 +190,22 @@ suite(util.getSuiteTitle(__filename), () => { _BuilderLambdaCall_foo("label1", "label2").bar().qux() ` - util.getDefaultSetup(sample_in) - - let sourceFile = arkts.nodeByPeer(util.AstProvider.provideAst()) - util.assert(arkts.isSourceFile(sourceFile)) + let sourceFile = factory.createSourceFile(sample_in) + util.assert(ts.isSourceFile(sourceFile)) const builderLambdaTransformer = new BuilderLambdaTransformer() - sourceFile = builderLambdaTransformer.visitor(sourceFile) + const result = builderLambdaTransformer.visitor(sourceFile) + util.assert(ts.isSourceFile(result)) util.assertEqualsAfter( - sourceFile, + result, ` foo(((instance: string) => { return instance.bar().qux(); }), "label1", "label2") `, - arkts.ContextState.ES2PANDA_STATE_PARSED, + ts.ContextState.ES2PANDA_STATE_PARSED, ) }) @@ -233,17 +227,16 @@ suite(util.getSuiteTitle(__filename), () => { _BuilderLambdaCall_Foo("> second_char_of_ABC: ").charAt(1) ` - util.getDefaultSetup(sample_in) - - let sourceFile = arkts.nodeByPeer(util.AstProvider.provideAst()) - util.assert(arkts.isSourceFile(sourceFile)) + let sourceFile = factory.createSourceFile(sample_in) + util.assert(ts.isSourceFile(sourceFile)) const builderLambdaTransformer = new BuilderLambdaTransformer() - sourceFile = builderLambdaTransformer.visitor(sourceFile) + const result = builderLambdaTransformer.visitor(sourceFile) + util.assert(ts.isSourceFile(result)) util.assertEqualsAfter( - sourceFile, + result, ` function Foo(builder: ((instance: string)=> string), arg1: string): void { console.log(((arg1) + (builder("ABC")))); @@ -253,8 +246,7 @@ suite(util.getSuiteTitle(__filename), () => { return instance.charAt(1); }), "> second_char_of_ABC: ") `, - arkts.ContextState.ES2PANDA_STATE_PARSED, + ts.ContextState.ES2PANDA_STATE_PARSED, ) }) }) -*/ \ No newline at end of file diff --git a/arkoala-arkts/libarkts/test/es2panda/create-function-declaration.test.ts b/arkoala-arkts/libarkts/test/es2panda/create-function-declaration.test.ts index 57b1d5ba4c9625b25da0025d933fbbaaa24f959c..79d3e2ba5a37908435e0f9493403df0bfe794a39 100644 --- a/arkoala-arkts/libarkts/test/es2panda/create-function-declaration.test.ts +++ b/arkoala-arkts/libarkts/test/es2panda/create-function-declaration.test.ts @@ -175,80 +175,80 @@ suite(util.getSuiteTitle(__filename), () => { ) }) - // test("empty-method-with-public-static-modifiers", function() { - // // class A { - // // static test_func() { - // // } - // // } - - // const sample_in = - // ` - // class A { - // } - // ` - - // let sourceFile = factory.createSourceFile(sample_in) - // util.assert(ts.isSourceFile(sourceFile)) - - // let classDecl = sourceFile.statements[0] - // util.assert(ts.isClassDeclaration(classDecl)) - - // classDecl = factory.updateClassDeclaration( - // classDecl, - // undefined, - // factory.createIdentifier("A"), - // undefined, - // undefined, - // [ - // factory.createMethodDeclaration( - // [ - // factory.createToken(ts.SyntaxKind.PublicKeyword), - // factory.createToken(ts.SyntaxKind.StaticKeyword) - // ], - // undefined, - // factory.createIdentifier("test_func"), - // undefined, - // undefined, - // [], - // undefined, - // factory.createBlock( - // [], - // false - // ) - // ), - // factory.createConstructorDeclaration( - // [ - // factory.createToken(ts.SyntaxKind.PublicKeyword) - // ], - // [], - // factory.createBlock( - // [], - // false - // ) - // ) - // ] - // ) - - // sourceFile = factory.updateSourceFile( - // sourceFile, - // [ - // classDecl - // ] - // ) - - // util.assertEqualsAfter( - // sourceFile, - // ` - // class A { - // public static test_func() {} - - // public constructor() {} - - // } - // `, - // ts.ContextState.ES2PANDA_STATE_PARSED, - // ) - // }) + test("empty-method-with-public-static-modifiers", function() { + // class A { + // static test_func() { + // } + // } + + const sample_in = + ` + class A { + } + ` + + let sourceFile = factory.createSourceFile(sample_in) + util.assert(ts.isSourceFile(sourceFile)) + + let classDecl = sourceFile.statements[0] + util.assert(ts.isClassDeclaration(classDecl)) + + classDecl = factory.updateClassDeclaration( + classDecl, + undefined, + factory.createIdentifier("A"), + undefined, + undefined, + [ + factory.createMethodDeclaration( + [ + factory.createToken(ts.SyntaxKind.PublicKeyword), + factory.createToken(ts.SyntaxKind.StaticKeyword) + ], + undefined, + factory.createIdentifier("test_func"), + undefined, + undefined, + [], + undefined, + factory.createBlock( + [], + false + ) + ), + factory.createConstructorDeclaration( + [ + factory.createToken(ts.SyntaxKind.PublicKeyword) + ], + [], + factory.createBlock( + [], + false + ) + ) + ] + ) + + sourceFile = factory.updateSourceFile( + sourceFile, + [ + classDecl + ] + ) + + util.assertEqualsAfter( + sourceFile, + ` + class A { + public static test_func() {} + + public constructor() {} + + } + `, + ts.ContextState.ES2PANDA_STATE_PARSED, + ) + }) test("function-with-type-parameters", function() { // function test_func(): void {} diff --git a/arkoala-arkts/libarkts/test/es2panda/function-rewrite.test.ts b/arkoala-arkts/libarkts/test/es2panda/function-rewrite.test.ts index bc7d406c2e810553470c35f1d825ae18c1484cd0..a69aa515ef16dab8f3f9b2de06498fc6edc21113 100644 --- a/arkoala-arkts/libarkts/test/es2panda/function-rewrite.test.ts +++ b/arkoala-arkts/libarkts/test/es2panda/function-rewrite.test.ts @@ -1,10 +1,9 @@ -/* import * as util from "../test-util" -import * as arkts from "../../src/arkts" -import { FunctionTransformer } from "../../compatible/src/function-transformer" +import * as ts from "../../src/api" +// import { FunctionTransformer } from "../../compatible/src/function-transformer" // TODO: update scopes (transforming after check) -suite(util.getSuiteTitle(__filename), () => { +suite.skip(util.getSuiteTitle(__filename), () => { test("function-transformer-sample-1", function() { const sample_in = ` @@ -44,4 +43,3 @@ suite(util.getSuiteTitle(__filename), () => { // arkts.proceedToState(arkts.ContextState.ES2PANDA_STATE_BIN_GENERATED) }) }) -*/ \ No newline at end of file diff --git a/arkoala-arkts/libarkts/test/es2panda/import.test.ts b/arkoala-arkts/libarkts/test/es2panda/import.test.ts index f08f11cd1decd8760a2a649c939e2550f225c6b7..42979123bfe478c3eda323e6ad2f79b477bfc005 100644 --- a/arkoala-arkts/libarkts/test/es2panda/import.test.ts +++ b/arkoala-arkts/libarkts/test/es2panda/import.test.ts @@ -1,8 +1,7 @@ -/* import * as util from "../test-util" -import * as arkts from "../../src/arkts" +import * as ts from "../../src/api" -suite(util.getSuiteTitle(__filename), () => { +suite.skip(util.getSuiteTitle(__filename), () => { // TODO: doesn't running now, but compiles (config gets only one file) test("sample-1", function() { const sample_in = @@ -60,4 +59,3 @@ suite(util.getSuiteTitle(__filename), () => { // util.generateBinAndRun() }) }) -*/ \ No newline at end of file diff --git a/arkoala-arkts/libarkts/test/es2panda/print-visitor.test.ts b/arkoala-arkts/libarkts/test/es2panda/print-visitor.test.ts index 151267112e9bc0e26879eccf802c8d6419907a39..d817e6aebb3abe83087e1c2436c6d562ae3c9641 100644 --- a/arkoala-arkts/libarkts/test/es2panda/print-visitor.test.ts +++ b/arkoala-arkts/libarkts/test/es2panda/print-visitor.test.ts @@ -1,9 +1,8 @@ -/* -import * as arkts from "../../src/arkts" +import * as ts from "../../src/api" import * as util from "../test-util" import { PrintVisitor } from "../../compatible/src/print-visitor" -suite(util.getSuiteTitle(__filename), () => { +suite.skip(util.getSuiteTitle(__filename), () => { test("sample-1", function() { const source = ` @@ -61,13 +60,9 @@ suite(util.getSuiteTitle(__filename), () => { Identifier (mods: []) Identifier (mods: []) ` - util.getDefaultSetup(source) - - const peer = util.AstProvider.provideAst() - const ast = arkts.nodeByPeer(peer) - const output = (new PrintVisitor()).astToString(ast) + let sourceFile = ts.factory.createSourceFile(source) + const output = (new PrintVisitor()).astToString(sourceFile) util.assert.equal(util.alignText(output), util.alignText(expected)) }) }) -*/ \ No newline at end of file diff --git a/arkoala-arkts/libarkts/test/es2panda/simple-transformations.test.ts b/arkoala-arkts/libarkts/test/es2panda/simple-transformations.test.ts index c85a28a2b597f56dcf5c4c118ac8cddae7b09669..3f0e79fba6a869a72497e73d8b58609e0873b0a0 100644 --- a/arkoala-arkts/libarkts/test/es2panda/simple-transformations.test.ts +++ b/arkoala-arkts/libarkts/test/es2panda/simple-transformations.test.ts @@ -1,6 +1,6 @@ /* import * as util from "../test-util" -import * as arkts from "../../src/arkts" +import * as ts from "../../src/api" suite(util.getSuiteTitle(__filename), () => { test("sample-1", function() { @@ -13,34 +13,32 @@ suite(util.getSuiteTitle(__filename), () => { console.log("ok") ` - util.getDefaultSetup(sample_in) - - let sourceFile = arkts.nodeByPeer(util.AstProvider.provideAst()) - util.assert(arkts.isSourceFile(sourceFile)) + let sourceFile = ts.factory.createSourceFile(sample_in) + util.assert(ts.isSourceFile(sourceFile)) const varDecl = sourceFile.statements[1] - util.assert(arkts.isVariableStatement(varDecl)) + util.assert(ts.isVariableStatement(varDecl)) const declList = varDecl.declarationList - util.assert(arkts.isVariableDeclarationList(declList)) + util.assert(ts.isVariableDeclarationList(declList)) const x = declList.declarations[0] - util.assert(arkts.isVariableDeclaration(x)) + util.assert(ts.isVariableDeclaration(x)) - sourceFile = arkts.factory.updateSourceFile( + sourceFile = ts.factory.updateSourceFile( sourceFile, [ sourceFile.statements[0], - arkts.factory.updateVariableStatement( + ts.factory.updateVariableStatement( varDecl, undefined, // declList - arkts.factory.createVariableDeclarationList( - [arkts.factory.createVariableDeclaration( - arkts.factory.createIdentifier("x"), + ts.factory.createVariableDeclarationList( + [ts.factory.createVariableDeclaration( + ts.factory.createIdentifier("x"), undefined, - arkts.factory.createTypeReferenceNode( - arkts.factory.createIdentifier("A") + ts.factory.createTypeReferenceNode( + ts.factory.createIdentifier("A") ), undefined )]