idlize
70 строк · 2.6 Кб
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
16import * as ts from 'typescript'
17import { DeclarationTable } from "./DeclarationTable";
18import { MaterializedClass } from "./Materialized";
19import { PeerClass } from "./PeerClass";
20import { PeerFile } from "./PeerFile";
21import { ComponentDeclaration } from './PeerGeneratorVisitor';
22import { BuilderClass } from './BuilderClass';
23
24export type PeerLibraryOutput = {
25outputC: string[]
26}
27
28export class PeerLibrary {
29public readonly files: PeerFile[] = []
30public readonly builderClasses: Map<string, BuilderClass> = new Map()
31public readonly materializedClasses: Map<string, MaterializedClass> = new Map()
32
33constructor(
34public declarationTable: DeclarationTable,
35public componentsToGenerate: Set<string>,
36) {}
37
38readonly customComponentMethods: string[] = []
39// todo really dirty - we use it until we can generate interfaces
40// replacing import type nodes
41readonly importTypesStubToSource: Map<string, string> = new Map()
42readonly componentsDeclarations: ComponentDeclaration[] = []
43readonly conflictedDeclarations: Set<ts.Declaration> = new Set()
44
45findPeerByComponentName(componentName: string): PeerClass | undefined {
46for (const file of this.files)
47for (const peer of file.peers.values())
48if (peer.componentName == componentName)
49return peer
50return undefined
51}
52
53findFileByOriginalFilename(filename: string): PeerFile | undefined {
54return this.files.find(it => it.originalFilename === filename)
55}
56
57findComponentByDeclaration(node: ts.Declaration): ComponentDeclaration | undefined {
58return this.componentsDeclarations.find(it => {
59return it.interfaceDeclaration === node || it.attributesDeclarations === node
60})
61}
62
63isComponentDeclaration(node: ts.Declaration): boolean {
64return this.findComponentByDeclaration(node) !== undefined
65}
66
67shouldGenerateComponent(name: string): boolean {
68return !this.componentsToGenerate.size || this.componentsToGenerate.has(name)
69}
70}