• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "variableNameIdentifierNameSame.h"
17 #include "ir/expressions/identifier.h"
18 #include "checker/types/ets/etsEnumType.h"
19 #include "ir/expressions/memberExpression.h"
20 #include "ir/ets/etsImportDeclaration.h"
21 
22 namespace ark::es2panda::compiler::ast_verifier {
23 
operator ()(CheckContext & ctx,const ir::AstNode * ast)24 [[nodiscard]] CheckResult VariableNameIdentifierNameSame::operator()(CheckContext &ctx, const ir::AstNode *ast)
25 {
26     if (!ast->IsIdentifier()) {
27         return {CheckDecision::CORRECT, CheckAction::CONTINUE};
28     }
29     const auto *id = ast->AsIdentifier();
30     const auto variable = ast->AsIdentifier()->Variable();
31     if (variable == nullptr || variable->Declaration() == nullptr || variable->Declaration()->Node() == nullptr) {
32         return {CheckDecision::CORRECT, CheckAction::CONTINUE};
33     }
34     const auto variableNode = variable->Declaration()->Node();
35     // NOTE(psaykerone): skip because, this exceptions need to be fixed in checker and lowering
36     if (variableNode->IsExported() || variableNode->IsExportedType() || variableNode->IsDefaultExported() ||
37         id->Name().Utf8().find("field") == 0 || variable->Name().Utf8().find("field") == 0) {
38         return {CheckDecision::CORRECT, CheckAction::CONTINUE};
39     }
40     if (id->Name() == variable->Name()) {
41         return {CheckDecision::CORRECT, CheckAction::CONTINUE};
42     }
43 
44     // For dynamic imports imported identifier name does not match variable name
45     // Example:
46     // import { A as AA } from "dynamic_js_import_tests"
47     // Variable name will be AA
48     // But imported identifier name is A
49     auto parent = ast->Parent();
50     while (parent != nullptr) {
51         if (parent->IsETSImportDeclaration() && parent->AsETSImportDeclaration()->IsPureDynamic()) {
52             return {CheckDecision::CORRECT, CheckAction::CONTINUE};
53         }
54 
55         parent = parent->Parent();
56     }
57 
58     parent = id->Parent();
59     if (parent->IsMemberExpression() && parent->AsMemberExpression()->Object()->TsType()->IsETSStringEnumType()) {
60         const bool isValueOfMethod = (id->Name() == checker::ETSEnumType::VALUE_OF_METHOD_NAME);
61         const bool isToStringMethod = (variable->Name() == checker::ETSEnumType::TO_STRING_METHOD_NAME);
62         if (isValueOfMethod && isToStringMethod) {
63             // For string enums valueOf method calls toString method
64             return {CheckDecision::CORRECT, CheckAction::CONTINUE};
65         }
66     }
67 
68     ctx.AddCheckMessage("IDENTIFIER_NAME_DIFFERENCE", *id, id->Start());
69     return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
70 }
71 
72 }  // namespace ark::es2panda::compiler::ast_verifier
73