idlize
45 строк · 1.5 Кб
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 'ohos-typescript'
17import { AbstractVisitor } from './AbstractVisitor'
18
19export class CallExpressionCollector extends AbstractVisitor {
20private readonly map = new Map<string, boolean>()
21
22constructor(sourceFile: ts.SourceFile, ctx: ts.TransformationContext, ...names: string[]) {
23super(sourceFile, ctx)
24for (const name of names) {
25this.map.set(name, false)
26}
27}
28
29isVisited(name: string): boolean {
30return this.map.get(name) === true
31}
32
33visitor(beforeChildren: ts.Node): ts.Node {
34const node = this.visitEachChild(beforeChildren)
35if (ts.isCallExpression(node)) {
36const expression = node.expression
37if (ts.isIdentifier(expression)) {
38const name = ts.idText(expression)
39const exist = this.map.get(name)
40if (!exist) this.map.set(name, true)
41}
42}
43return node
44}
45}
46