idlize
69 строк · 2.4 Кб
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 { getOrPut } from "../util"
18import { PeerClass } from "./PeerClass"
19import { DeclarationTable } from "./DeclarationTable"
20import { ImportFeature } from './ImportsCollector'
21
22export class EnumEntity {
23constructor(
24public readonly name: string,
25public readonly comment: string,
26public readonly members: EnumMember[] = [],
27) {}
28pushMember(name: string, comment: string, initializerText: string | undefined) {
29this.members.push(new EnumMember(name, comment, initializerText))
30}
31}
32
33class EnumMember {
34constructor(
35public readonly name: string,
36public readonly comment: string,
37public readonly initializerText: string | undefined,
38) {}
39}
40
41export class PeerFile {
42readonly peers: Map<string, PeerClass> = new Map()
43readonly enums: EnumEntity[] = []
44// todo maybe declarations should be converted as same as enums - to
45// structs detached from `ts` nodes
46readonly declarations: Set<ts.Declaration> = new Set()
47readonly importFeatures: ImportFeature[] = []
48readonly serializeImportFeatures: ImportFeature[] = []
49constructor(
50public readonly originalFilename: string,
51public readonly declarationTable: DeclarationTable,
52private readonly componentsToGenerate: Set<string>,
53) {}
54
55get peersToGenerate(): PeerClass[] {
56const peers = Array.from(this.peers.values())
57if (!this.componentsToGenerate.size)
58return peers
59return peers.filter(it => this.componentsToGenerate.has(it.componentName))
60}
61
62getOrPutPeer(componentName: string) {
63return getOrPut(this.peers, componentName, () => new PeerClass(this, componentName, this.originalFilename, this.declarationTable))
64}
65
66pushEnum(enumEntity: EnumEntity) {
67this.enums.push(enumEntity)
68}
69}