From 48fc9c416b89d5bd41ebad328f03fc3dea6ed065 Mon Sep 17 00:00:00 2001 From: dengxinyu Date: Thu, 11 Jan 2024 12:50:17 +0000 Subject: [PATCH] Description: BugFix for target keyword and testcases Signed-off-by: dengxinyu Change-Id: I98c59a8d22a980ac68fb0f2277ee962097510e06 --- .../rename/RenameIdentifierTransformer.ts | 6 ++++ arkguard/src/utils/NodeUtils.ts | 10 ++++++- .../grammar/target/newTargetConstructor_1.js | 30 +++++++++++++++++++ .../newTargetConstructor_1_expected.txt | 26 ++++++++++++++++ .../grammar/target/newTargetConstructor_2.js | 29 ++++++++++++++++++ .../newTargetConstructor_2_expected.txt | 26 ++++++++++++++++ .../grammar/target/newTargetFunction_1.js | 30 +++++++++++++++++++ .../target/newTargetFunction_1_expected.txt | 27 +++++++++++++++++ .../grammar/target/newTargetFunction_2.js | 30 +++++++++++++++++++ .../target/newTargetFunction_2_expected.txt | 27 +++++++++++++++++ 10 files changed, 240 insertions(+), 1 deletion(-) create mode 100644 arkguard/test/grammar/target/newTargetConstructor_1.js create mode 100644 arkguard/test/grammar/target/newTargetConstructor_1_expected.txt create mode 100644 arkguard/test/grammar/target/newTargetConstructor_2.js create mode 100644 arkguard/test/grammar/target/newTargetConstructor_2_expected.txt create mode 100644 arkguard/test/grammar/target/newTargetFunction_1.js create mode 100644 arkguard/test/grammar/target/newTargetFunction_1_expected.txt create mode 100644 arkguard/test/grammar/target/newTargetFunction_2.js create mode 100644 arkguard/test/grammar/target/newTargetFunction_2_expected.txt diff --git a/arkguard/src/transformers/rename/RenameIdentifierTransformer.ts b/arkguard/src/transformers/rename/RenameIdentifierTransformer.ts index 420211daba..b8475375c1 100644 --- a/arkguard/src/transformers/rename/RenameIdentifierTransformer.ts +++ b/arkguard/src/transformers/rename/RenameIdentifierTransformer.ts @@ -14,6 +14,7 @@ */ import { + SyntaxKind, factory, forEachChild, isBreakOrContinueStatement, @@ -22,6 +23,7 @@ import { isIdentifier, isImportSpecifier, isLabeledStatement, + isMetaProperty, isSourceFile, isStructDeclaration, setParentRecursive, @@ -433,6 +435,10 @@ namespace secharmony { return node; } + if (NodeUtils.isNewTargetNode(node)) { + return node; + } + let sym: Symbol | undefined = checker.getSymbolAtLocation(shadowNode); let mangledPropertyNameOfNoSymbolImportExport = ''; if ((!sym || sym.name === 'default')) { diff --git a/arkguard/src/utils/NodeUtils.ts b/arkguard/src/utils/NodeUtils.ts index d3911af8c5..0bf806841c 100644 --- a/arkguard/src/utils/NodeUtils.ts +++ b/arkguard/src/utils/NodeUtils.ts @@ -13,7 +13,7 @@ * limitations under the License. */ -import type {ClassElement, Expression, Node, ObjectBindingPattern, SourceFile, StructDeclaration} from 'typescript'; +import type {ClassElement, Expression, Identifier, Node, ObjectBindingPattern, SourceFile, StructDeclaration} from 'typescript'; import { SyntaxKind, factory, @@ -29,6 +29,7 @@ import { isEnumMember, isGetAccessor, isIdentifier, + isMetaProperty, isMethodDeclaration, isMethodSignature, isParameter, @@ -211,4 +212,11 @@ export class NodeUtils { public static isInETSFile(node: Node | undefined): boolean { return !!node && NodeUtils.getSourceFileOfNode(node).fileName.endsWith('.ets'); } + + public static isNewTargetNode(node: Identifier): boolean { + if (isMetaProperty(node.parent) && node.parent.keywordToken === SyntaxKind.NewKeyword && node.escapedText === 'target') { + return true; + } + return false; + } } diff --git a/arkguard/test/grammar/target/newTargetConstructor_1.js b/arkguard/test/grammar/target/newTargetConstructor_1.js new file mode 100644 index 0000000000..e77c08826e --- /dev/null +++ b/arkguard/test/grammar/target/newTargetConstructor_1.js @@ -0,0 +1,30 @@ +/* + * 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. + */ + +class foo { + constructor() { + console.log(new.target.name); + } +} + +class Foo extends foo { + constructor() { + super(); + } +} + +var f = new foo(); +var F = new Foo(); + diff --git a/arkguard/test/grammar/target/newTargetConstructor_1_expected.txt b/arkguard/test/grammar/target/newTargetConstructor_1_expected.txt new file mode 100644 index 0000000000..fe62aaff88 --- /dev/null +++ b/arkguard/test/grammar/target/newTargetConstructor_1_expected.txt @@ -0,0 +1,26 @@ +/* + * 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. + */ +class g { + constructor() { + console.log(new.target.name); + } +} +class h extends g { + constructor() { + super(); + } +} +var f = new g(); +var i = new h(); diff --git a/arkguard/test/grammar/target/newTargetConstructor_2.js b/arkguard/test/grammar/target/newTargetConstructor_2.js new file mode 100644 index 0000000000..3a4f97d27c --- /dev/null +++ b/arkguard/test/grammar/target/newTargetConstructor_2.js @@ -0,0 +1,29 @@ +/* + * 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. + */ + +class Human { + constructor() { + console.log(new.target); + } +} + +class Woman extends Human { + constructor() { + super(); + } +} + +new Human(); +new Woman(); \ No newline at end of file diff --git a/arkguard/test/grammar/target/newTargetConstructor_2_expected.txt b/arkguard/test/grammar/target/newTargetConstructor_2_expected.txt new file mode 100644 index 0000000000..61fb97dd55 --- /dev/null +++ b/arkguard/test/grammar/target/newTargetConstructor_2_expected.txt @@ -0,0 +1,26 @@ +/* + * 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. + */ +class j { + constructor() { + console.log(new.target); + } +} +class k extends j { + constructor() { + super(); + } +} +new j(); +new k(); diff --git a/arkguard/test/grammar/target/newTargetFunction_1.js b/arkguard/test/grammar/target/newTargetFunction_1.js new file mode 100644 index 0000000000..3054b5d7fa --- /dev/null +++ b/arkguard/test/grammar/target/newTargetFunction_1.js @@ -0,0 +1,30 @@ +/* + * 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. + */ + +function P() { + this.name = 'John'; + console.log(new.target); +} + +function F() { + if(!new.target){ + console.log("Not new"); + } +} + +P(); +new P(); +F(); +new F(); \ No newline at end of file diff --git a/arkguard/test/grammar/target/newTargetFunction_1_expected.txt b/arkguard/test/grammar/target/newTargetFunction_1_expected.txt new file mode 100644 index 0000000000..2de673e865 --- /dev/null +++ b/arkguard/test/grammar/target/newTargetFunction_1_expected.txt @@ -0,0 +1,27 @@ +/* + * 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. + */ +function l() { + this.name = 'John'; + console.log(new.target); +} +function i() { + if (!new.target) { + console.log("Not new"); + } +} +l(); +new l(); +i(); +new i(); diff --git a/arkguard/test/grammar/target/newTargetFunction_2.js b/arkguard/test/grammar/target/newTargetFunction_2.js new file mode 100644 index 0000000000..4110c368bd --- /dev/null +++ b/arkguard/test/grammar/target/newTargetFunction_2.js @@ -0,0 +1,30 @@ +/* + * 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. + */ + +export var kmp = function(_) { + class Exception { + } + + class KMossException extends Exception { + constructor(message, cause) { + super(); + return new.target.new_kntr_k$(message, cause); + } + + static new_kntr_k$(message, cause) { + return new KMossException(); + } + } +} \ No newline at end of file diff --git a/arkguard/test/grammar/target/newTargetFunction_2_expected.txt b/arkguard/test/grammar/target/newTargetFunction_2_expected.txt new file mode 100644 index 0000000000..7282b2289b --- /dev/null +++ b/arkguard/test/grammar/target/newTargetFunction_2_expected.txt @@ -0,0 +1,27 @@ +/* + * 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. + */ +export var kmp = function (n) { + class o { + } + class p extends o { + constructor(s, t) { + super(); + return new.target.m(s, t); + } + static m(q, r) { + return new p(); + } + } +}; -- Gitee