1 /**
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ast_verifier_test.h"
17 #include "ir/astNode.h"
18 #include "ir/expressions/callExpression.h"
19 #include "checker/ETSchecker.h"
20 #include "checker/types/signature.h"
21
22 #include <gtest/gtest.h>
23
24 using ark::es2panda::checker::SignatureFlags;
25 using ark::es2panda::compiler::ast_verifier::ASTVerifier;
26 using ark::es2panda::compiler::ast_verifier::InvariantNameSet;
27 using ark::es2panda::ir::AstNode;
28 using ark::es2panda::ir::Identifier;
29 using ark::es2panda::ir::MemberExpression;
30 using ark::es2panda::ir::MemberExpressionKind;
31 using ark::es2panda::ir::SuperExpression;
32
33 namespace {
34
TEST_F(ASTVerifierTest,LabelsHaveReferences)35 TEST_F(ASTVerifierTest, LabelsHaveReferences)
36 {
37 ark::es2panda::checker::ETSChecker checker;
38 ASTVerifier verifier {Allocator()};
39
40 char const *text = R"(
41 abstract class A {
42 abstract foo (): void
43 bar(): void {}
44 }
45
46 class B extends A {
47 foo () {
48 super.bar()
49 }
50 }
51 )";
52
53 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
54 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
55 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
56
57 auto *ast = reinterpret_cast<AstNode *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
58
59 // Setup call to abstract method via super
60 ast->IterateRecursively([&checker, this](ark::es2panda::ir::AstNode *child) {
61 if (child->IsCallExpression()) {
62 auto *const call = child->AsCallExpression();
63 auto *super = checker.AllocNode<SuperExpression>();
64 auto *id = checker.AllocNode<Identifier>("foo", Allocator());
65
66 auto *callee =
67 checker.AllocNode<MemberExpression>(super, id, MemberExpressionKind::PROPERTY_ACCESS, false, false);
68 call->SetCallee(callee);
69
70 // For testing just copy signature from original callee and add abstract flag
71 auto *const signature = call->Signature();
72 signature->AddSignatureFlag(SignatureFlags::ABSTRACT);
73 call->SetSignature(signature);
74 }
75 });
76
77 InvariantNameSet checks;
78 checks.insert("CheckAbstractMethodForAll");
79 const auto &messages = verifier.Verify(ast, checks);
80
81 // Expecting warning
82 ASSERT_EQ(messages.size(), 1);
83 ASSERT_EQ(messages[0].Cause(), "CALL TO ABSTRACT METHOD VIA SUPER");
84
85 impl_->DestroyContext(ctx);
86 }
87
88 } // namespace
89