1/* 2 * Copyright (c) 2022-2023 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 16import * as ts from 'typescript'; 17 18export function scopeContainsThis(tsNode: ts.Node): boolean { 19 let found = false; 20 function visitNode(tsNode: ts.Node) { 21 // Stop visiting child nodes if finished searching. 22 if (found) { 23 return; 24 } 25 if (tsNode.kind === ts.SyntaxKind.ThisKeyword) { 26 found = true; 27 return; 28 } 29 // Visit children nodes. Skip any local declaration that defines 30 // its own scope as it needs to be checked separately. 31 if ( 32 !ts.isClassDeclaration(tsNode) && 33 !ts.isClassExpression(tsNode) && 34 !ts.isModuleDeclaration(tsNode) && 35 !ts.isFunctionDeclaration(tsNode) && 36 !ts.isFunctionExpression(tsNode) 37 ) 38 tsNode.forEachChild(visitNode); 39 } 40 visitNode(tsNode); 41 return found; 42} 43