From 539e70fd07402c432993808dc4a55bdcde6ee023 Mon Sep 17 00:00:00 2001 From: huangyu Date: Mon, 27 Feb 2023 15:24:25 +0800 Subject: [PATCH] fixed a86e2d7 from https://gitee.com/huangyu76/arkcompiler_ets_frontend/pulls/888 Ignore generated MergeDeclarationMarker node when preserveConstEnums enabled Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I6I8J7 Test: ts2panda/tests/preserveConstEnums.test.ts Change-Id: Icfa2f2e1747916ded9b7e4246dd949e8b6894c14 Signed-off-by: huangyu --- ts2panda/src/compiler.ts | 1 + ts2panda/tests/preserveConstEnums.test.ts | 130 ++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 ts2panda/tests/preserveConstEnums.test.ts diff --git a/ts2panda/src/compiler.ts b/ts2panda/src/compiler.ts index 7cbbb4eca5..4818a0fd77 100644 --- a/ts2panda/src/compiler.ts +++ b/ts2panda/src/compiler.ts @@ -457,6 +457,7 @@ export class Compiler { case ts.SyntaxKind.EndOfDeclarationMarker: case ts.SyntaxKind.ModuleDeclaration: case ts.SyntaxKind.TypeAliasDeclaration: + case ts.SyntaxKind.MergeDeclarationMarker: break; default: throw new Error("Statement " + this.getNodeName(stmt) + " is unimplemented"); diff --git a/ts2panda/tests/preserveConstEnums.test.ts b/ts2panda/tests/preserveConstEnums.test.ts new file mode 100644 index 0000000000..30100b4d99 --- /dev/null +++ b/ts2panda/tests/preserveConstEnums.test.ts @@ -0,0 +1,130 @@ +/* + * Copyright (c) 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. + */ + + +import { + expect +} from 'chai'; +import 'mocha'; +import { creatAstFromSnippet } from "./utils/asthelper"; +import { checkInstructions, SnippetCompiler } from "./utils/base"; +import { PandaGen } from '../src/pandagen'; + +import { + Callarg1, + Createemptyobject, + Defineclasswithbuffer, + Definefunc, + Imm, + IRNode, + Isfalse, + Jeqz, + Jmp, + Label, + Lda, + Ldglobalvar, + Mov, + Returnundefined, + Sta, + Stglobalvar, + Sttoglobalrecord, + ThrowUndefinedifholewithname, + Tryldglobalbyname, + VReg, +} from "../src/irnodes"; + +describe("preserveConstEnumsTest", function () { + it("preserveConstEnumsInSingleFileTest", function() { + let snippetCompiler = new SnippetCompiler(); + snippetCompiler.compileAfter( + ` + const enum LanguageType { + JS, + TS, + ETS, + } + `, 'test.ts'); + IRNode.pg = new PandaGen("", creatAstFromSnippet(""), 0, undefined); + let insns = snippetCompiler.getGlobalInsns(); + let expected = [ + new Lda(new VReg()), + new Stglobalvar(new Imm(0), 'LanguageType'), + new Definefunc(new Imm(0), 'UnitTest.#1#', new Imm(1)), + new Sta(new VReg()), + new Ldglobalvar(new Imm(0), 'LanguageType'), + new Sta(new VReg()), + new Isfalse(), + new Jeqz(new Label()), + new Createemptyobject(), + new Sta(new VReg()), + new Stglobalvar(new Imm(0), 'LanguageType'), + new Jmp(new Label()), + new Label(), + new Lda(new VReg()), + new Label(), + new Sta(new VReg()), + new Lda(new VReg()), + new Callarg1(new Imm(0), new VReg()), + new Returnundefined() + ]; + expect(checkInstructions(insns, expected)).to.be.true; + }); + it("preserveConstEnumsInModuleTest", function () { + let snippetCompiler = new SnippetCompiler(); + snippetCompiler.compileAfter( + ` + class foo {}; + module foo { + const enum LanguageType { + JS, + TS, + ETS, + } + }; + `, 'test.ts'); + IRNode.pg = new PandaGen("", creatAstFromSnippet(""), 0, undefined); + let insns = snippetCompiler.getGlobalInsns(); + let expected = [ + new Mov(new VReg(), new VReg()), + new Defineclasswithbuffer(new Imm(0), 'UnitTest.#1#foo', 'test_1', new Imm(0), new VReg()), + new Sta(new VReg()), + new Lda(new VReg()), + new Sttoglobalrecord(new Imm(0), 'foo'), + new Definefunc(new Imm(0), 'UnitTest.#3#', new Imm(1)), + new Sta(new VReg()), + new Tryldglobalbyname(new Imm(0), 'foo'), + new Sta(new VReg()), + new Isfalse(), + new Jeqz(new Label()), + new Createemptyobject(), + new Sta(new VReg()), + new Sta(new VReg()), + new Lda(new VReg()), + new ThrowUndefinedifholewithname("foo"), + new Lda(new VReg()), + new Sta(new VReg()), + new Jmp(new Label()), + new Label(), + new Lda(new VReg()), + new Label(), + new Sta(new VReg()), + new Lda(new VReg()), + new Callarg1(new Imm(0), new VReg()), + new Returnundefined() + ]; + expect(checkInstructions(insns, expected)).to.be.true; + }); +}); + -- Gitee