idlize
49 строк · 1.9 Кб
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'
18import { getLineNumber, isCallDecorator } from './ApiUtils'
19import { NameTable, asString, isBuilderLambda, IssueTable, BuilderLambdaDecorator, filterDecorators } from './utils'
20
21
22export class NameCollector extends AbstractVisitor {
23private printer = ts.createPrinter({ removeComments: false })
24constructor(
25sourceFile: ts.SourceFile,
26ctx: ts.TransformationContext,
27public nameTable: NameTable,
28private issueTable: IssueTable
29) {
30super(sourceFile, ctx)
31}
32
33visitor(beforeChildren: ts.Node): ts.Node {
34const node = this.visitEachChild(beforeChildren)
35if (ts.isStructDeclaration(node)) {
36if (node.name) {
37this.nameTable.structs.push(ts.idText(node.name))
38}
39return node
40}
41if (isBuilderLambda(node)) {
42const decorators = filterDecorators(node)!
43const index = decorators.findIndex(decorator => isCallDecorator(decorator, BuilderLambdaDecorator))
44// Record builder lambda position to
45this.issueTable.builderLambda.push(getLineNumber(this.sourceFile, decorators[index].end))
46}
47return node
48}
49}
50