idlize
61 строка · 2.3 Кб
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 { IndentedPrinter } from "../../IndentedPrinter";
18import { createLanguageWriter } from "../LanguageWriters";
19import { PeerLibrary } from "../PeerLibrary";
20import { DeclarationNameConvertor } from "../dependencies_collector";
21import { convertDeclaration } from '../TypeNodeConvertor';
22
23class ConflictedDeclarationsVisitor {
24readonly writer = createLanguageWriter(this.library.declarationTable.language)
25
26constructor(
27private readonly library: PeerLibrary
28) {}
29
30print() {
31const printedNames = new Set<string>()
32for (const decl of this.library.conflictedDeclarations) {
33const name = convertDeclaration(DeclarationNameConvertor.I, decl)
34if (printedNames.has(name)) continue
35printedNames.add(name)
36
37const parent = decl.parent
38if (ts.isModuleBlock(parent)) {
39this.writer.print(`export namespace ${parent.parent.name.text} {`)
40this.writer.pushIndent()
41}
42
43let maybeGenerics = ''
44if (ts.isClassDeclaration(decl) || ts.isInterfaceDeclaration(decl))
45if (decl.typeParameters?.length)
46maybeGenerics = `<${decl.typeParameters.map((_, i) => `T${i}=undefined`).join(',')}>`
47this.writer.print(`export type ${name}${maybeGenerics} = object;`)
48
49if (ts.isModuleBlock(parent)) {
50this.writer.popIndent()
51this.writer.print('}')
52}
53}
54}
55}
56
57export function printConflictedDeclarations(library: PeerLibrary): string {
58const visitor = new ConflictedDeclarationsVisitor(library)
59visitor.print()
60return visitor.writer.getOutput().join('\n')
61}