13af6ab5fSopenharmony_ci/* 23af6ab5fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ciimport { describe, it } from 'mocha'; 173af6ab5fSopenharmony_ciimport { expect } from 'chai'; 183af6ab5fSopenharmony_ciimport { NodeUtils, collectReservedNameForObf } from '../../../src/utils/NodeUtils'; 193af6ab5fSopenharmony_ciimport * as ts from 'typescript' 203af6ab5fSopenharmony_ciimport sinon from 'sinon'; 213af6ab5fSopenharmony_ciimport { MergedConfig } from '../../../src/initialization/ConfigResolver'; 223af6ab5fSopenharmony_ciimport { UnobfuscationCollections } from '../../../src/utils/CommonCollections'; 233af6ab5fSopenharmony_ci 243af6ab5fSopenharmony_citype Mutable<T extends object> = { -readonly [K in keyof T]: T[K] } 253af6ab5fSopenharmony_ci 263af6ab5fSopenharmony_cidescribe('test for NodeUtils', function () { 273af6ab5fSopenharmony_ci describe('test for isPropertyDeclarationNode', function () { 283af6ab5fSopenharmony_ci it('should return false if node has no parent', function () { 293af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 303af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyDeclarationNode(node)).to.be.false; 313af6ab5fSopenharmony_ci }) 323af6ab5fSopenharmony_ci it('should return ture when node.parent is PropertyAssignment', function () { 333af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 343af6ab5fSopenharmony_ci const parent = ts.factory.createPropertyAssignment(node, ts.factory.createNumericLiteral('1')); 353af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 363af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyDeclarationNode(node)).to.be.true; 373af6ab5fSopenharmony_ci }) 383af6ab5fSopenharmony_ci it('should return ture when node.parent is ComputedPropertyName', function () { 393af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 403af6ab5fSopenharmony_ci const parent = ts.factory.createComputedPropertyName(node); 413af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 423af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyDeclarationNode(node)).to.be.true; 433af6ab5fSopenharmony_ci }) 443af6ab5fSopenharmony_ci it('should return ture when node.parent is BindingElement', function () { 453af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 463af6ab5fSopenharmony_ci const parent = ts.factory.createBindingElement(undefined, node, 'bindingElement'); 473af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 483af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyDeclarationNode(node)).to.be.true; 493af6ab5fSopenharmony_ci }) 503af6ab5fSopenharmony_ci it('should return ture when node.parent is PropertySignature', function () { 513af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 523af6ab5fSopenharmony_ci const parent = ts.factory.createPropertySignature(undefined, node, undefined, ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)); 533af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 543af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyDeclarationNode(node)).to.be.true; 553af6ab5fSopenharmony_ci }) 563af6ab5fSopenharmony_ci it('should return ture when node.parent is MethodSignature', function () { 573af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 583af6ab5fSopenharmony_ci const parent = ts.factory.createMethodSignature(undefined, node, undefined, undefined, [], undefined); 593af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 603af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyDeclarationNode(node)).to.be.true; 613af6ab5fSopenharmony_ci }) 623af6ab5fSopenharmony_ci it('should return ture when node.parent is EnumMember', function () { 633af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 643af6ab5fSopenharmony_ci const parent = ts.factory.createEnumMember(node); 653af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 663af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyDeclarationNode(node)).to.be.true; 673af6ab5fSopenharmony_ci }) 683af6ab5fSopenharmony_ci it('should return ture when node.parent is PropertyDeclaration', function () { 693af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 703af6ab5fSopenharmony_ci const parent = ts.factory.createPropertyDeclaration(undefined, undefined, node, undefined, undefined, undefined); 713af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 723af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyDeclarationNode(node)).to.be.true; 733af6ab5fSopenharmony_ci }) 743af6ab5fSopenharmony_ci it('should return ture when node.parent is MethodDeclaration', function () { 753af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 763af6ab5fSopenharmony_ci const parent = ts.factory.createMethodDeclaration(undefined, undefined, undefined, node, undefined, undefined, [], undefined, undefined); 773af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 783af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyDeclarationNode(node)).to.be.true; 793af6ab5fSopenharmony_ci }) 803af6ab5fSopenharmony_ci it('should return ture when node.parent is SetAccessorDeclaration', function () { 813af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 823af6ab5fSopenharmony_ci const parent = ts.factory.createSetAccessorDeclaration(undefined, undefined, node, [], undefined); 833af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 843af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyDeclarationNode(node)).to.be.true; 853af6ab5fSopenharmony_ci }) 863af6ab5fSopenharmony_ci it('should return ture when node.parent is GetAccessorDeclaration', function () { 873af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 883af6ab5fSopenharmony_ci const parent = ts.factory.createGetAccessorDeclaration(undefined, undefined, node, [], undefined, undefined); 893af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 903af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyDeclarationNode(node)).to.be.true; 913af6ab5fSopenharmony_ci }) 923af6ab5fSopenharmony_ci }) 933af6ab5fSopenharmony_ci describe('test for isPropertyOrElementAccessNode', function () { 943af6ab5fSopenharmony_ci let isPropertyAccessNodeStub; 953af6ab5fSopenharmony_ci let isElementAccessNodeStub; 963af6ab5fSopenharmony_ci 973af6ab5fSopenharmony_ci beforeEach(function () { 983af6ab5fSopenharmony_ci isPropertyAccessNodeStub = sinon.stub(NodeUtils, 'isPropertyAccessNode').returns(false); 993af6ab5fSopenharmony_ci isElementAccessNodeStub = sinon.stub(NodeUtils, 'isElementAccessNode').returns(false); 1003af6ab5fSopenharmony_ci }); 1013af6ab5fSopenharmony_ci 1023af6ab5fSopenharmony_ci afterEach(function () { 1033af6ab5fSopenharmony_ci isPropertyAccessNodeStub.restore(); 1043af6ab5fSopenharmony_ci isElementAccessNodeStub.restore(); 1053af6ab5fSopenharmony_ci }); 1063af6ab5fSopenharmony_ci 1073af6ab5fSopenharmony_ci it('should return true when node is a PropertyAccessNode', function () { 1083af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 1093af6ab5fSopenharmony_ci isPropertyAccessNodeStub.returns(true); 1103af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyOrElementAccessNode(node)).to.be.true; 1113af6ab5fSopenharmony_ci }) 1123af6ab5fSopenharmony_ci it('should return true when node is a isElementAccessNode', function () { 1133af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 1143af6ab5fSopenharmony_ci isElementAccessNodeStub.returns(true); 1153af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyOrElementAccessNode(node)).to.be.true; 1163af6ab5fSopenharmony_ci }) 1173af6ab5fSopenharmony_ci it('should return false when both isPropertyAccessNode and isElementAccessNode return false', function () { 1183af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 1193af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyOrElementAccessNode(node)).to.be.false; 1203af6ab5fSopenharmony_ci }) 1213af6ab5fSopenharmony_ci }) 1223af6ab5fSopenharmony_ci describe('test for isPropertyAccessNode', function () { 1233af6ab5fSopenharmony_ci let isInClassDeclarationStub; 1243af6ab5fSopenharmony_ci let isInExpressionStub; 1253af6ab5fSopenharmony_ci 1263af6ab5fSopenharmony_ci beforeEach(function () { 1273af6ab5fSopenharmony_ci isInClassDeclarationStub = sinon.stub(NodeUtils, 'isInClassDeclaration').returns(false); 1283af6ab5fSopenharmony_ci isInExpressionStub = sinon.stub(NodeUtils, 'isInExpression').returns(false); 1293af6ab5fSopenharmony_ci }); 1303af6ab5fSopenharmony_ci 1313af6ab5fSopenharmony_ci afterEach(function () { 1323af6ab5fSopenharmony_ci isInClassDeclarationStub.restore(); 1333af6ab5fSopenharmony_ci isInExpressionStub.restore(); 1343af6ab5fSopenharmony_ci }); 1353af6ab5fSopenharmony_ci 1363af6ab5fSopenharmony_ci it('should return false if node has no parent', function () { 1373af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 1383af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyAccessNode(node)).to.be.false; 1393af6ab5fSopenharmony_ci }) 1403af6ab5fSopenharmony_ci it('should return true if isPropertyAccessExpression and parent.name equals to node', function () { 1413af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 1423af6ab5fSopenharmony_ci const parent = ts.factory.createPropertyAccessExpression(node, node); 1433af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 1443af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyAccessNode(node)).to.be.true; 1453af6ab5fSopenharmony_ci }) 1463af6ab5fSopenharmony_ci it('should return isInExpression(parent) if isPrivateIdentifier and isInClassDeclaration', function () { 1473af6ab5fSopenharmony_ci const node = ts.factory.createPrivateIdentifier("#name"); 1483af6ab5fSopenharmony_ci const parent = ts.factory.createIdentifier('parent'); 1493af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 1503af6ab5fSopenharmony_ci isInClassDeclarationStub.returns(true); 1513af6ab5fSopenharmony_ci isInExpressionStub.returns(true) 1523af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyAccessNode(node)).to.be.true; 1533af6ab5fSopenharmony_ci isInExpressionStub.returns(false) 1543af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyAccessNode(node)).to.be.false; 1553af6ab5fSopenharmony_ci }) 1563af6ab5fSopenharmony_ci it('should return true if isQualifiedName and parent.right equals to node', function () { 1573af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 1583af6ab5fSopenharmony_ci const parent = ts.factory.createQualifiedName(node, node); 1593af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 1603af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyAccessNode(node)).to.be.true; 1613af6ab5fSopenharmony_ci }) 1623af6ab5fSopenharmony_ci }) 1633af6ab5fSopenharmony_ci describe('test for isInClassDeclaration', function () { 1643af6ab5fSopenharmony_ci it('should return flase when node is undefined', function () { 1653af6ab5fSopenharmony_ci expect(NodeUtils.isInClassDeclarationForTest(undefined)).to.be.false; 1663af6ab5fSopenharmony_ci }) 1673af6ab5fSopenharmony_ci it('should return true when node is ClassDeclaration', function () { 1683af6ab5fSopenharmony_ci const node = ts.factory.createClassDeclaration(undefined, undefined, undefined, undefined, undefined, []); 1693af6ab5fSopenharmony_ci expect(NodeUtils.isInClassDeclarationForTest(node)).to.be.true; 1703af6ab5fSopenharmony_ci }) 1713af6ab5fSopenharmony_ci it('should return true when node is ClassExpression', function () { 1723af6ab5fSopenharmony_ci const node = ts.factory.createClassExpression(undefined, undefined, undefined, undefined, undefined, []); 1733af6ab5fSopenharmony_ci expect(NodeUtils.isInClassDeclarationForTest(node)).to.be.true; 1743af6ab5fSopenharmony_ci }) 1753af6ab5fSopenharmony_ci it('should return true when node.parent is isInClassDeclaration', function () { 1763af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 1773af6ab5fSopenharmony_ci const parent = ts.factory.createClassExpression(undefined, undefined, undefined, undefined, undefined, []); 1783af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 1793af6ab5fSopenharmony_ci expect(NodeUtils.isInClassDeclarationForTest(node)).to.be.true; 1803af6ab5fSopenharmony_ci }) 1813af6ab5fSopenharmony_ci }) 1823af6ab5fSopenharmony_ci describe('test for isInClassDeclaration', function () { 1833af6ab5fSopenharmony_ci it('should return flase when node is undefined', function () { 1843af6ab5fSopenharmony_ci expect(NodeUtils.isInClassDeclarationForTest(undefined)).to.be.false; 1853af6ab5fSopenharmony_ci }) 1863af6ab5fSopenharmony_ci it('should return true when node is ClassDeclaration', function () { 1873af6ab5fSopenharmony_ci const node = ts.factory.createClassDeclaration(undefined, undefined, undefined, undefined, undefined, []); 1883af6ab5fSopenharmony_ci expect(NodeUtils.isInClassDeclarationForTest(node)).to.be.true; 1893af6ab5fSopenharmony_ci }) 1903af6ab5fSopenharmony_ci it('should return true when node is ClassExpression', function () { 1913af6ab5fSopenharmony_ci const node = ts.factory.createClassExpression(undefined, undefined, undefined, undefined, undefined, []); 1923af6ab5fSopenharmony_ci expect(NodeUtils.isInClassDeclarationForTest(node)).to.be.true; 1933af6ab5fSopenharmony_ci }) 1943af6ab5fSopenharmony_ci it('should return true when node.parent is isInClassDeclaration', function () { 1953af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 1963af6ab5fSopenharmony_ci const parent = ts.factory.createClassExpression(undefined, undefined, undefined, undefined, undefined, []); 1973af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 1983af6ab5fSopenharmony_ci expect(NodeUtils.isInClassDeclarationForTest(node)).to.be.true; 1993af6ab5fSopenharmony_ci }) 2003af6ab5fSopenharmony_ci }) 2013af6ab5fSopenharmony_ci describe('test for isInExpression', function () { 2023af6ab5fSopenharmony_ci it('should return flase when node is undefined', function () { 2033af6ab5fSopenharmony_ci expect(NodeUtils.isInExpressionForTest(undefined)).to.be.false; 2043af6ab5fSopenharmony_ci }) 2053af6ab5fSopenharmony_ci it('should return isInOperator(node) when node is not undefined', function () { 2063af6ab5fSopenharmony_ci let isInOperatorStub = sinon.stub(NodeUtils, 'isInOperator').returns(false); 2073af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 2083af6ab5fSopenharmony_ci expect(NodeUtils.isInExpressionForTest(node)).to.be.false; 2093af6ab5fSopenharmony_ci isInOperatorStub.returns(true); 2103af6ab5fSopenharmony_ci expect(NodeUtils.isInExpressionForTest(node)).to.be.true; 2113af6ab5fSopenharmony_ci isInOperatorStub.restore(); 2123af6ab5fSopenharmony_ci }) 2133af6ab5fSopenharmony_ci }) 2143af6ab5fSopenharmony_ci describe('test for isInOperator', function () { 2153af6ab5fSopenharmony_ci it('should return true when node is binary expression and operator is InKeyword', function () { 2163af6ab5fSopenharmony_ci const name = ts.factory.createIdentifier('name'); 2173af6ab5fSopenharmony_ci const node = ts.factory.createBinaryExpression(name, ts.SyntaxKind.InKeyword, name); 2183af6ab5fSopenharmony_ci expect(NodeUtils.isInOperatorForTest(node)).to.be.true; 2193af6ab5fSopenharmony_ci }) 2203af6ab5fSopenharmony_ci it('should return false when node is not binary expression', function () { 2213af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 2223af6ab5fSopenharmony_ci expect(NodeUtils.isInOperatorForTest(node)).to.be.false; 2233af6ab5fSopenharmony_ci }) 2243af6ab5fSopenharmony_ci it('should return false when operator is not Inkeyword', function () { 2253af6ab5fSopenharmony_ci const name = ts.factory.createIdentifier('name'); 2263af6ab5fSopenharmony_ci const node = ts.factory.createBinaryExpression(name, ts.SyntaxKind.PlusEqualsToken, name); 2273af6ab5fSopenharmony_ci expect(NodeUtils.isInOperatorForTest(node)).to.be.false; 2283af6ab5fSopenharmony_ci }) 2293af6ab5fSopenharmony_ci }) 2303af6ab5fSopenharmony_ci 2313af6ab5fSopenharmony_ci describe('test for isElementAccessNode', function () { 2323af6ab5fSopenharmony_ci it('should return false if node has no parent', function () { 2333af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 2343af6ab5fSopenharmony_ci expect(NodeUtils.isElementAccessNode(node)).to.be.false; 2353af6ab5fSopenharmony_ci }) 2363af6ab5fSopenharmony_ci it('should return true if isElementAccessExpression and parent argumentExpression equals to node', function () { 2373af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 2383af6ab5fSopenharmony_ci const parent = ts.factory.createElementAccessExpression(node, node); 2393af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 2403af6ab5fSopenharmony_ci }) 2413af6ab5fSopenharmony_ci }) 2423af6ab5fSopenharmony_ci 2433af6ab5fSopenharmony_ci describe('test for isClassPropertyInConstructorParams', function () { 2443af6ab5fSopenharmony_ci it('should return false if node is not an Identifier', function () { 2453af6ab5fSopenharmony_ci const node = ts.factory.createRegularExpressionLiteral('name'); 2463af6ab5fSopenharmony_ci expect(NodeUtils.isClassPropertyInConstructorParams(node)).to.be.false; 2473af6ab5fSopenharmony_ci }) 2483af6ab5fSopenharmony_ci it('should return false when node has no parent', function () { 2493af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 2503af6ab5fSopenharmony_ci expect(NodeUtils.isClassPropertyInConstructorParams(node)).to.be.false; 2513af6ab5fSopenharmony_ci }) 2523af6ab5fSopenharmony_ci it('should return false when node parent is not a parameter', function () { 2533af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 2543af6ab5fSopenharmony_ci const parent = ts.factory.createElementAccessExpression(node, node); 2553af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 2563af6ab5fSopenharmony_ci expect(NodeUtils.isClassPropertyInConstructorParams(node)).to.be.false; 2573af6ab5fSopenharmony_ci }) 2583af6ab5fSopenharmony_ci it('should return false when modifiers is undefined', function () { 2593af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 2603af6ab5fSopenharmony_ci const parent = ts.factory.createParameterDeclaration([], undefined, node, undefined, undefined, undefined); 2613af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 2623af6ab5fSopenharmony_ci expect(NodeUtils.isClassPropertyInConstructorParams(node)).to.be.false; 2633af6ab5fSopenharmony_ci }) 2643af6ab5fSopenharmony_ci it('should return false when modifiers length is 0 or modifier is ParameterPropertyModifier', function () { 2653af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 2663af6ab5fSopenharmony_ci const parent = ts.factory.createParameterDeclaration([ts.factory.createModifier(ts.SyntaxKind.AbstractKeyword)], undefined, node, undefined, undefined, undefined); 2673af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 2683af6ab5fSopenharmony_ci expect(NodeUtils.isClassPropertyInConstructorParams(node)).to.be.false; 2693af6ab5fSopenharmony_ci }) 2703af6ab5fSopenharmony_ci it('should return true when node parent parent is ConstructorDeclaration', function () { 2713af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 2723af6ab5fSopenharmony_ci const parent = ts.factory.createParameterDeclaration([ts.factory.createModifier(ts.SyntaxKind.PublicKeyword)], undefined, node, undefined, undefined, undefined); 2733af6ab5fSopenharmony_ci const parentParent = ts.factory.createConstructorDeclaration(undefined, [ts.factory.createModifier(ts.SyntaxKind.AbstractKeyword)], [], undefined); 2743af6ab5fSopenharmony_ci (parent as Mutable<ts.Node>).parent = parentParent; 2753af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 2763af6ab5fSopenharmony_ci expect(NodeUtils.isClassPropertyInConstructorParams(node)).to.be.true; 2773af6ab5fSopenharmony_ci }) 2783af6ab5fSopenharmony_ci }) 2793af6ab5fSopenharmony_ci 2803af6ab5fSopenharmony_ci describe('test for isClassPropertyInConstructorBody', function () { 2813af6ab5fSopenharmony_ci it('should return false if node is not an Identifier', function () { 2823af6ab5fSopenharmony_ci const node = ts.factory.createRegularExpressionLiteral('name'); 2833af6ab5fSopenharmony_ci expect(NodeUtils.isClassPropertyInConstructorBody(node, new Set)).to.be.false; 2843af6ab5fSopenharmony_ci }) 2853af6ab5fSopenharmony_ci it('shound return true when node parent is ConstructorDeclaration and constructorParams has id', function () { 2863af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 2873af6ab5fSopenharmony_ci const set: Set<string> = new Set(); 2883af6ab5fSopenharmony_ci set.add('name'); 2893af6ab5fSopenharmony_ci const parent = ts.factory.createConstructorDeclaration(undefined, [ts.factory.createModifier(ts.SyntaxKind.AbstractKeyword)], [], undefined); 2903af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 2913af6ab5fSopenharmony_ci expect(NodeUtils.isClassPropertyInConstructorBody(node, set)).to.be.true; 2923af6ab5fSopenharmony_ci }) 2933af6ab5fSopenharmony_ci it('should return false when curNode is not a ConstructorDeclaration or id does not exist in constructorParams', function () { 2943af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 2953af6ab5fSopenharmony_ci const set: Set<string> = new Set(); 2963af6ab5fSopenharmony_ci set.add('test'); 2973af6ab5fSopenharmony_ci const parent = ts.factory.createElementAccessExpression(node, node); 2983af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 2993af6ab5fSopenharmony_ci expect(NodeUtils.isClassPropertyInConstructorBody(node, set)).to.be.false; 3003af6ab5fSopenharmony_ci }) 3013af6ab5fSopenharmony_ci }) 3023af6ab5fSopenharmony_ci 3033af6ab5fSopenharmony_ci describe('test for isPropertyNode', function () { 3043af6ab5fSopenharmony_ci it('should return true when node is PropertyOrElementAccessNode', function () { 3053af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 3063af6ab5fSopenharmony_ci const parent = ts.factory.createElementAccessExpression(node, node); 3073af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 3083af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyNode(node)).to.be.true; 3093af6ab5fSopenharmony_ci }) 3103af6ab5fSopenharmony_ci it('should return true when node is a PropertyDeclarationNode', function () { 3113af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 3123af6ab5fSopenharmony_ci const parent = ts.factory.createPropertyAssignment(node, node); 3133af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 3143af6ab5fSopenharmony_ci expect(NodeUtils.isPropertyNode(node)).to.be.true; 3153af6ab5fSopenharmony_ci }) 3163af6ab5fSopenharmony_ci }) 3173af6ab5fSopenharmony_ci 3183af6ab5fSopenharmony_ci describe('test for isObjectBindingPatternAssignment', function () { 3193af6ab5fSopenharmony_ci it('should return false when node is not VariableDeclaration', function () { 3203af6ab5fSopenharmony_ci const node = ts.factory.createObjectBindingPattern([]); 3213af6ab5fSopenharmony_ci const parent = ts.factory.createParameterDeclaration(undefined, undefined, undefined, node, undefined, undefined, undefined); 3223af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 3233af6ab5fSopenharmony_ci expect(NodeUtils.isObjectBindingPatternAssignment(node)).to.be.false; 3243af6ab5fSopenharmony_ci }) 3253af6ab5fSopenharmony_ci it('should return true when node parent initializer is CallExpression', function () { 3263af6ab5fSopenharmony_ci const node = ts.factory.createObjectBindingPattern([]); 3273af6ab5fSopenharmony_ci const parent = ts.factory.createVariableDeclaration(node, undefined, undefined, undefined); 3283af6ab5fSopenharmony_ci const initializer = ts.factory.createCallExpression(ts.factory.createIdentifier('name'), undefined, undefined); 3293af6ab5fSopenharmony_ci (parent as Mutable<ts.VariableDeclaration>).initializer = initializer; 3303af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 3313af6ab5fSopenharmony_ci expect(NodeUtils.isObjectBindingPatternAssignment(node)).to.be.true; 3323af6ab5fSopenharmony_ci }) 3333af6ab5fSopenharmony_ci }) 3343af6ab5fSopenharmony_ci 3353af6ab5fSopenharmony_ci describe('test for isDeclarationFile', function () { 3363af6ab5fSopenharmony_ci it('should return false when sourceFile is not a declarationFile', function () { 3373af6ab5fSopenharmony_ci const endOfFileToken = ts.factory.createToken(ts.SyntaxKind.EndOfFileToken); 3383af6ab5fSopenharmony_ci const sourceFile = ts.factory.createSourceFile([], endOfFileToken, ts.NodeFlags.AwaitContext); 3393af6ab5fSopenharmony_ci expect(NodeUtils.isDeclarationFile(sourceFile)).to.be.false; 3403af6ab5fSopenharmony_ci }) 3413af6ab5fSopenharmony_ci }) 3423af6ab5fSopenharmony_ci 3433af6ab5fSopenharmony_ci describe('test for getSourceFileOfNode', function () { 3443af6ab5fSopenharmony_ci it('should return node when node kind is SyntaxKind.SourceFile', function () { 3453af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 3463af6ab5fSopenharmony_ci const kind = ts.SyntaxKind.SourceFile; 3473af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).kind = kind; 3483af6ab5fSopenharmony_ci expect(NodeUtils.getSourceFileOfNode(node)).to.equal(node); 3493af6ab5fSopenharmony_ci }) 3503af6ab5fSopenharmony_ci it('should return node parent when node kind is not SyntaxKind.SourceFile', function () { 3513af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 3523af6ab5fSopenharmony_ci const kind = ts.SyntaxKind.SymbolKeyword; 3533af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).kind = kind; 3543af6ab5fSopenharmony_ci const endOfFileToken = ts.factory.createToken(ts.SyntaxKind.EndOfFileToken); 3553af6ab5fSopenharmony_ci const parent = ts.factory.createSourceFile([], endOfFileToken, ts.NodeFlags.AwaitContext); 3563af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).parent = parent; 3573af6ab5fSopenharmony_ci expect(NodeUtils.getSourceFileOfNode(node)).to.equal(node.parent); 3583af6ab5fSopenharmony_ci }) 3593af6ab5fSopenharmony_ci }) 3603af6ab5fSopenharmony_ci 3613af6ab5fSopenharmony_ci describe('test for isDETSFile', function () { 3623af6ab5fSopenharmony_ci it('should return true when node fileName end with .d.ets', function () { 3633af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 3643af6ab5fSopenharmony_ci const kind = ts.SyntaxKind.SourceFile; 3653af6ab5fSopenharmony_ci (node as Mutable<ts.Node>).kind = kind; 3663af6ab5fSopenharmony_ci const sourceFile = NodeUtils.getSourceFileOfNode(node); 3673af6ab5fSopenharmony_ci sourceFile.fileName = 'a.d.ets'; 3683af6ab5fSopenharmony_ci expect(NodeUtils.isDETSFile(node)).to.be.true; 3693af6ab5fSopenharmony_ci }) 3703af6ab5fSopenharmony_ci }) 3713af6ab5fSopenharmony_ci 3723af6ab5fSopenharmony_ci describe('test for isNewTargetNode', function () { 3733af6ab5fSopenharmony_ci it('should return true when node parent is MetaProperty and node parent keywordToken is yntaxKind.NewKeyword and node escapedText is target', function () { 3743af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('target'); 3753af6ab5fSopenharmony_ci const parent = ts.factory.createMetaProperty(ts.SyntaxKind.NewKeyword, node); 3763af6ab5fSopenharmony_ci (node as Mutable<ts.Identifier>).parent = parent; 3773af6ab5fSopenharmony_ci expect(NodeUtils.isNewTargetNode(node)).to.be.true; 3783af6ab5fSopenharmony_ci }) 3793af6ab5fSopenharmony_ci it('should return false when node is not a new target node', function () { 3803af6ab5fSopenharmony_ci const node = ts.factory.createIdentifier('name'); 3813af6ab5fSopenharmony_ci const parent = ts.factory.createMetaProperty(ts.SyntaxKind.ImportKeyword, node); 3823af6ab5fSopenharmony_ci (node as Mutable<ts.Identifier>).parent = parent; 3833af6ab5fSopenharmony_ci expect(NodeUtils.isNewTargetNode(node)).to.be.false; 3843af6ab5fSopenharmony_ci }) 3853af6ab5fSopenharmony_ci }) 3863af6ab5fSopenharmony_ci 3873af6ab5fSopenharmony_ci describe('test for collectReservedNameForObf', function () { 3883af6ab5fSopenharmony_ci process.env.compileTool = 'rollup'; 3893af6ab5fSopenharmony_ci const ENUM_TEST1: string = 3903af6ab5fSopenharmony_ci 'enum ANIMAL {\n' + 3913af6ab5fSopenharmony_ci ' CAT,\n' + 3923af6ab5fSopenharmony_ci ' DOG = CAT + 1,\n' + 3933af6ab5fSopenharmony_ci ' GOOSE = DOG + 1,\n' + 3943af6ab5fSopenharmony_ci ' DUCK = GOOSE + 1,\n' + 3953af6ab5fSopenharmony_ci '}'; 3963af6ab5fSopenharmony_ci it('test1 collectReservedNameForObf-enum: -enable-property-obfuscation, shouldETSOrTSFileTransformToJS = false', function () { 3973af6ab5fSopenharmony_ci const arkConfig = new MergedConfig(); 3983af6ab5fSopenharmony_ci arkConfig.options.enablePropertyObfuscation = true; 3993af6ab5fSopenharmony_ci const result: ts.TranspileOutput = ts.transpileModule(ENUM_TEST1, { 4003af6ab5fSopenharmony_ci compilerOptions: { 4013af6ab5fSopenharmony_ci "target": ts.ScriptTarget.ES2021 4023af6ab5fSopenharmony_ci }, 4033af6ab5fSopenharmony_ci fileName: "enum.ts", 4043af6ab5fSopenharmony_ci transformers: { before: [collectReservedNameForObf(arkConfig, false)] } 4053af6ab5fSopenharmony_ci }); 4063af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedEnum.has('CAT')).to.be.true; 4073af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedEnum.has('DOG')).to.be.true; 4083af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedEnum.has('GOOSE')).to.be.true; 4093af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedEnum.has('DUCK')).to.be.false; 4103af6ab5fSopenharmony_ci UnobfuscationCollections.clear(); 4113af6ab5fSopenharmony_ci }) 4123af6ab5fSopenharmony_ci 4133af6ab5fSopenharmony_ci const ENUM_TEST2: string = 4143af6ab5fSopenharmony_ci 'let test = 1;\n' + 4153af6ab5fSopenharmony_ci 'enum ANIMAL {\n' + 4163af6ab5fSopenharmony_ci ' CAT,\n' + 4173af6ab5fSopenharmony_ci ' DOG = (CAT + 1) + test,\n' + 4183af6ab5fSopenharmony_ci '}'; 4193af6ab5fSopenharmony_ci it('test2 collectReservedNameForObf-enum: -enable-property-obfuscation, shouldETSOrTSFileTransformToJS = false', function () { 4203af6ab5fSopenharmony_ci const arkConfig = new MergedConfig(); 4213af6ab5fSopenharmony_ci arkConfig.options.enablePropertyObfuscation = true; 4223af6ab5fSopenharmony_ci const result: ts.TranspileOutput = ts.transpileModule(ENUM_TEST2, { 4233af6ab5fSopenharmony_ci compilerOptions: { 4243af6ab5fSopenharmony_ci "target": ts.ScriptTarget.ES2021 4253af6ab5fSopenharmony_ci }, 4263af6ab5fSopenharmony_ci fileName: "enum.ts", 4273af6ab5fSopenharmony_ci transformers: { before: [collectReservedNameForObf(arkConfig, false)] } 4283af6ab5fSopenharmony_ci }); 4293af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedEnum.has('CAT')).to.be.true; 4303af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedEnum.has('test')).to.be.true; 4313af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedEnum.has('DOG')).to.be.false; 4323af6ab5fSopenharmony_ci UnobfuscationCollections.clear(); 4333af6ab5fSopenharmony_ci }) 4343af6ab5fSopenharmony_ci 4353af6ab5fSopenharmony_ci const ENUM_TEST3: string = 4363af6ab5fSopenharmony_ci 'class TEST{\n' + 4373af6ab5fSopenharmony_ci ' prop1 = 1\n' + 4383af6ab5fSopenharmony_ci '}\n' + 4393af6ab5fSopenharmony_ci 'let myclass = new TEST();\n' + 4403af6ab5fSopenharmony_ci 'enum TEST1{\n' + 4413af6ab5fSopenharmony_ci ' AAA,\n' + 4423af6ab5fSopenharmony_ci ' BBB = AAA + myclass.prop1\n' + 4433af6ab5fSopenharmony_ci '}'; 4443af6ab5fSopenharmony_ci it('test3 collectReservedNameForObf-enum: -enable-property-obfuscation, shouldETSOrTSFileTransformToJS = false', function () { 4453af6ab5fSopenharmony_ci const arkConfig = new MergedConfig(); 4463af6ab5fSopenharmony_ci arkConfig.options.enablePropertyObfuscation = true; 4473af6ab5fSopenharmony_ci const result: ts.TranspileOutput = ts.transpileModule(ENUM_TEST3, { 4483af6ab5fSopenharmony_ci compilerOptions: { 4493af6ab5fSopenharmony_ci "target": ts.ScriptTarget.ES2021 4503af6ab5fSopenharmony_ci }, 4513af6ab5fSopenharmony_ci fileName: "enum.ts", 4523af6ab5fSopenharmony_ci transformers: { before: [collectReservedNameForObf(arkConfig, false)] } 4533af6ab5fSopenharmony_ci }); 4543af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedEnum.has('AAA')).to.be.true; 4553af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedEnum.has('myclass')).to.be.true; 4563af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedEnum.has('BBB')).to.be.false; 4573af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedEnum.has('prop1')).to.be.false; 4583af6ab5fSopenharmony_ci UnobfuscationCollections.clear(); 4593af6ab5fSopenharmony_ci }) 4603af6ab5fSopenharmony_ci 4613af6ab5fSopenharmony_ci it('test4 collectReservedNameForObf-enum: -enable-property-obfuscation, shouldETSOrTSFileTransformToJS = true', function () { 4623af6ab5fSopenharmony_ci const arkConfig = new MergedConfig(); 4633af6ab5fSopenharmony_ci arkConfig.options.enablePropertyObfuscation = true; 4643af6ab5fSopenharmony_ci const result: ts.TranspileOutput = ts.transpileModule(ENUM_TEST3, { 4653af6ab5fSopenharmony_ci compilerOptions: { 4663af6ab5fSopenharmony_ci "target": ts.ScriptTarget.ES2021 4673af6ab5fSopenharmony_ci }, 4683af6ab5fSopenharmony_ci fileName: "enum.js", 4693af6ab5fSopenharmony_ci transformers: { before: [collectReservedNameForObf(arkConfig, true)] } 4703af6ab5fSopenharmony_ci }); 4713af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedEnum.size === 0).to.be.true; 4723af6ab5fSopenharmony_ci UnobfuscationCollections.clear(); 4733af6ab5fSopenharmony_ci }) 4743af6ab5fSopenharmony_ci 4753af6ab5fSopenharmony_ci const STRUCT_TEST1: string = 4763af6ab5fSopenharmony_ci 'class ViewPU {}\n' + 4773af6ab5fSopenharmony_ci 'export class Retransmission1 extends ViewPU {\n' + 4783af6ab5fSopenharmony_ci ' constructor() {\n' + 4793af6ab5fSopenharmony_ci ' super();\n' + 4803af6ab5fSopenharmony_ci ' }\n' + 4813af6ab5fSopenharmony_ci ' scroller1: string;\n' + 4823af6ab5fSopenharmony_ci ' controller1: number;\n' + 4833af6ab5fSopenharmony_ci ' callMethod1(): void {};\n' + 4843af6ab5fSopenharmony_ci '}\n' + 4853af6ab5fSopenharmony_ci 'class Retransmission2 extends ViewPU {\n' + 4863af6ab5fSopenharmony_ci ' constructor() {\n' + 4873af6ab5fSopenharmony_ci ' super();\n' + 4883af6ab5fSopenharmony_ci ' }\n' + 4893af6ab5fSopenharmony_ci ' scroller2: string;\n' + 4903af6ab5fSopenharmony_ci ' controller2: number;\n' + 4913af6ab5fSopenharmony_ci ' callMethod2(): void {};\n' + 4923af6ab5fSopenharmony_ci '}'; 4933af6ab5fSopenharmony_ci it('test5 collectReservedNameForObf-struct: -enable-property-obfuscation, shouldETSOrTSFileTransformToJS = false', function () { 4943af6ab5fSopenharmony_ci const arkConfig = new MergedConfig(); 4953af6ab5fSopenharmony_ci arkConfig.options.enablePropertyObfuscation = true; 4963af6ab5fSopenharmony_ci const result: ts.TranspileOutput = ts.transpileModule(STRUCT_TEST1, { 4973af6ab5fSopenharmony_ci compilerOptions: { 4983af6ab5fSopenharmony_ci "target": ts.ScriptTarget.ES2021 4993af6ab5fSopenharmony_ci }, 5003af6ab5fSopenharmony_ci fileName: "enum.ts", 5013af6ab5fSopenharmony_ci transformers: { before: [collectReservedNameForObf(arkConfig, false)] } 5023af6ab5fSopenharmony_ci }); 5033af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedStruct.has('scroller1')).to.be.true; 5043af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedStruct.has('controller1')).to.be.true; 5053af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedStruct.has('callMethod1')).to.be.true; 5063af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedStruct.has('scroller2')).to.be.true; 5073af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedStruct.has('controller2')).to.be.true; 5083af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedStruct.has('callMethod2')).to.be.true; 5093af6ab5fSopenharmony_ci UnobfuscationCollections.clear(); 5103af6ab5fSopenharmony_ci }) 5113af6ab5fSopenharmony_ci 5123af6ab5fSopenharmony_ci it('test6 collectReservedNameForObf-struct: -disable-obfuscation', function () { 5133af6ab5fSopenharmony_ci const arkConfig = new MergedConfig(); 5143af6ab5fSopenharmony_ci arkConfig.options.disableObfuscation = true; 5153af6ab5fSopenharmony_ci arkConfig.options.enablePropertyObfuscation = true; 5163af6ab5fSopenharmony_ci const result: ts.TranspileOutput = ts.transpileModule(STRUCT_TEST1, { 5173af6ab5fSopenharmony_ci compilerOptions: { 5183af6ab5fSopenharmony_ci "target": ts.ScriptTarget.ES2021 5193af6ab5fSopenharmony_ci }, 5203af6ab5fSopenharmony_ci fileName: "enum.ts", 5213af6ab5fSopenharmony_ci transformers: { before: [collectReservedNameForObf(arkConfig, false)] } 5223af6ab5fSopenharmony_ci }); 5233af6ab5fSopenharmony_ci expect(UnobfuscationCollections.reservedStruct.size === 0).to.be.true; 5243af6ab5fSopenharmony_ci UnobfuscationCollections.clear(); 5253af6ab5fSopenharmony_ci }) 5263af6ab5fSopenharmony_ci }) 5273af6ab5fSopenharmony_ci})