• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 "program.h"
17 
18 #include "varbinder/varbinder.h"
19 #include "varbinder/ETSBinder.h"
20 #include "ir/astDump.h"
21 #include "ir/base/classDefinition.h"
22 #include "ir/statements/blockStatement.h"
23 
24 namespace ark::es2panda::parser {
25 
Dump() const26 std::string Program::Dump() const
27 {
28     ir::AstDumper dumper {ast_, SourceCode()};
29     return dumper.Str();
30 }
31 
DumpSilent() const32 void Program::DumpSilent() const
33 {
34     [[maybe_unused]] ir::AstDumper dumper {ast_, SourceCode()};
35     ASSERT(!dumper.Str().empty());
36 }
37 
GlobalClassScope()38 varbinder::ClassScope *Program::GlobalClassScope()
39 {
40     return globalClass_->Scope()->AsClassScope();
41 }
42 
GlobalClassScope() const43 const varbinder::ClassScope *Program::GlobalClassScope() const
44 {
45     return globalClass_->Scope()->AsClassScope();
46 }
47 
GlobalScope()48 varbinder::GlobalScope *Program::GlobalScope()
49 {
50     ASSERT(ast_->Scope()->IsGlobalScope() || ast_->Scope()->IsModuleScope());
51     return static_cast<varbinder::GlobalScope *>(ast_->Scope());
52 }
53 
GlobalScope() const54 const varbinder::GlobalScope *Program::GlobalScope() const
55 {
56     ASSERT(ast_->Scope()->IsGlobalScope() || ast_->Scope()->IsModuleScope());
57     return static_cast<const varbinder::GlobalScope *>(ast_->Scope());
58 }
59 
SetDeclarationModuleInfo()60 void Program::SetDeclarationModuleInfo()
61 {
62     bool onlyDeclarations = true;
63     for (auto stmt : ast_->Statements()) {
64         if (stmt->IsDeclare() || stmt->IsTSTypeAliasDeclaration()) {
65             continue;
66         }
67         onlyDeclarations = false;
68         break;
69     }
70     moduleInfo_.isDeclModule = onlyDeclarations;
71 }
72 
AddNodeToETSNolintCollection(const ir::AstNode * node,const std::set<ETSWarnings> & warningsCollection)73 void Program::AddNodeToETSNolintCollection(const ir::AstNode *node, const std::set<ETSWarnings> &warningsCollection)
74 {
75     ArenaSet<ETSWarnings> tmp(allocator_->Adapter());
76     tmp.insert(warningsCollection.begin(), warningsCollection.end());
77     etsnolintCollection_.insert({node, tmp});
78 }
79 
NodeContainsETSNolint(const ir::AstNode * node,ETSWarnings warning)80 bool Program::NodeContainsETSNolint(const ir::AstNode *node, ETSWarnings warning)
81 {
82     auto nodeEtsnolints = etsnolintCollection_.find(node);
83     if (nodeEtsnolints == etsnolintCollection_.end()) {
84         return false;
85     }
86 
87     return nodeEtsnolints->second.find(warning) != nodeEtsnolints->second.end();
88 }
89 
90 }  // namespace ark::es2panda::parser
91