1 /** 2 * Copyright (c) 2024-2025 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/base/classProperty.h" 19 #include <gtest/gtest.h> 20 #include "checker/ETSchecker.h" 21 22 using ark::es2panda::compiler::ast_verifier::CheckConstProperties; 23 24 namespace { 25 TEST_F(ASTVerifierTest,CheckConstProperties)26TEST_F(ASTVerifierTest, CheckConstProperties) 27 { 28 ark::es2panda::util::DiagnosticEngine de {}; 29 ark::es2panda::checker::ETSChecker checker {de}; 30 31 char const *text = R"( 32 class Test 33 { 34 readonly value = "readonly"; 35 } 36 37 function main(): void { 38 const test = new Test(); 39 console.log(test.value); 40 } 41 )"; 42 43 auto addConst = [](ark::es2panda::ir::AstNode *node) { 44 if (node->IsClassProperty()) { 45 auto property = node->AsClassProperty(); 46 if (property->IsReadonly()) { 47 property->AddModifier(ark::es2panda::ir::ModifierFlags::CONST); 48 } 49 } 50 }; 51 52 CONTEXT(ES2PANDA_STATE_LOWERED, text) 53 { 54 GetAst()->IterateRecursively(addConst); 55 56 EXPECT_TRUE(Verify<CheckConstProperties>(ExpectVerifierMessage {"Class property cannot be const"})); 57 } 58 } 59 60 } // namespace