diff --git a/koala-wrapper/src/arkts-api/factory/nodeFactory.ts b/koala-wrapper/src/arkts-api/factory/nodeFactory.ts index c780fdbf9e9b760ce14b9536ad5b386725d3772d..8450b4bc12f50f5a44daffbada701a64a52d4a03 100644 --- a/koala-wrapper/src/arkts-api/factory/nodeFactory.ts +++ b/koala-wrapper/src/arkts-api/factory/nodeFactory.ts @@ -81,6 +81,8 @@ import { ForUpdateStatement, ForInStatement, ForOfStatement, + SwitchStatement, + SwitchCaseStatement, } from '../../generated'; import { Es2pandaModifierFlags } from '../../generated/Es2pandaEnums'; import { classPropertySetOptional, hasModifierFlag } from '../utilities/public'; @@ -142,6 +144,8 @@ import { updateTSClassImplements } from '../node-utilities/TSClassImplements'; import { updateForUpdateStatement } from '../node-utilities/ForUpdateStatement'; import { updateForInStatement } from '../node-utilities/ForInStatement'; import { updateForOfStatement } from '../node-utilities/ForOfStatement'; +import { updateSwitchStatement } from '../node-utilities/SwitchStatement'; +import { updateSwitchCaseStatement } from '../node-utilities/SwitchCaseStatement'; export const factory = { get createIdentifier(): (...args: Parameters) => Identifier { @@ -601,6 +605,22 @@ export const factory = { get updateForOfStatement(): (...args: Parameters) => ForOfStatement { return updateForOfStatement; }, + get createSwitchStatement(): ( + ...args: Parameters + ) => SwitchStatement { + return SwitchStatement.createSwitchStatement; + }, + get updateSwitchStatement(): (...args: Parameters) => SwitchStatement { + return updateSwitchStatement; + }, + get createSwitchCaseStatement(): ( + ...args: Parameters + ) => SwitchCaseStatement { + return SwitchCaseStatement.createSwitchCaseStatement; + }, + get updateSwitchCaseStatement(): (...args: Parameters) => SwitchCaseStatement { + return updateSwitchCaseStatement; + }, /** @deprecated */ createTypeParameter1_(name: Identifier, constraint?: TypeNode, defaultType?: TypeNode) { return TSTypeParameter.createTSTypeParameter(Identifier.create1Identifier(name.name), constraint, defaultType); diff --git a/koala-wrapper/src/arkts-api/node-utilities/SwitchCaseStatement.ts b/koala-wrapper/src/arkts-api/node-utilities/SwitchCaseStatement.ts new file mode 100644 index 0000000000000000000000000000000000000000..4e3e528ef59f0824c4794dd26d1953a69d148eb8 --- /dev/null +++ b/koala-wrapper/src/arkts-api/node-utilities/SwitchCaseStatement.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Expression, Statement, SwitchCaseStatement } from '../../generated'; +import { isSameNativeObject } from '../peers/ArktsObject'; +import { attachModifiers, updateThenAttach } from '../utilities/private'; + +export function updateSwitchCaseStatement( + original: SwitchCaseStatement, + test: Expression | undefined, + consequent: readonly Statement[] +): SwitchCaseStatement { + if (isSameNativeObject(test, original.test) && isSameNativeObject(consequent, original.consequent)) { + return original; + } + + const update = updateThenAttach(SwitchCaseStatement.updateSwitchCaseStatement, attachModifiers); + return update(original, test, consequent); +} diff --git a/koala-wrapper/src/arkts-api/node-utilities/SwitchStatement.ts b/koala-wrapper/src/arkts-api/node-utilities/SwitchStatement.ts new file mode 100644 index 0000000000000000000000000000000000000000..cf601ee8a50322f616f78d99072fa2a76ef9ffe7 --- /dev/null +++ b/koala-wrapper/src/arkts-api/node-utilities/SwitchStatement.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Expression, SwitchCaseStatement, SwitchStatement } from '../../generated'; +import { isSameNativeObject } from '../peers/ArktsObject'; +import { attachModifiers, updateThenAttach } from '../utilities/private'; + +export function updateSwitchStatement( + original: SwitchStatement, + discriminant: Expression | undefined, + cases: readonly SwitchCaseStatement[] +): SwitchStatement { + if (isSameNativeObject(discriminant, original.discriminant) && isSameNativeObject(cases, original.cases)) { + return original; + } + + const update = updateThenAttach(SwitchStatement.updateSwitchStatement, attachModifiers); + return update(original, discriminant, cases); +} diff --git a/koala-wrapper/src/arkts-api/visitor.ts b/koala-wrapper/src/arkts-api/visitor.ts index 3a76bf8404a5ecb303cac0db7b933874c9f8d9c0..a08392e69ede0c9a545e530a3ff83d7e5134050c 100644 --- a/koala-wrapper/src/arkts-api/visitor.ts +++ b/koala-wrapper/src/arkts-api/visitor.ts @@ -54,6 +54,8 @@ import { isTSTypeAliasDeclaration, isETSParameterExpression, isETSFunctionType, + isSwitchStatement, + isSwitchCaseStatement, } from '../generated'; import { isEtsScript, @@ -106,6 +108,7 @@ export function visitEachChild(node: AstNode, visitor: Visitor): AstNode { script = visitDefinitionBody(script, visitor); script = visitStatement(script, visitor); script = visitForLoopStatement(script, visitor); + script = visitSwitchCaseStatement(script, visitor); script = visitOuterExpression(script, visitor); script = visitInnerExpression(script, visitor); script = visitTrivialExpression(script, visitor); @@ -397,6 +400,23 @@ function visitForLoopStatement(node: AstNode, visitor: Visitor): AstNode { return node; } +function visitSwitchCaseStatement(node: AstNode, visitor: Visitor): AstNode { + if (updated) { + return node; + } + if (isSwitchStatement(node)) { + return factory.updateSwitchStatement( + node, + nodeVisitor(node.discriminant, visitor), + nodesVisitor(node.cases, visitor) + ); + } + if (isSwitchCaseStatement(node)) { + return factory.updateSwitchCaseStatement(node, node.test, nodesVisitor(node.consequent, visitor)); + } + return node; +} + function visitETSModule(node: AstNode, visitor: Visitor): AstNode { if (updated) { return node;