idlize

Форк
0
/
ConflictedDeclarationsPrinter.ts 
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

16
import * as ts from 'typescript'
17
import { IndentedPrinter } from "../../IndentedPrinter";
18
import { createLanguageWriter } from "../LanguageWriters";
19
import { PeerLibrary } from "../PeerLibrary";
20
import { DeclarationNameConvertor } from "../dependencies_collector";
21
import { convertDeclaration } from '../TypeNodeConvertor';
22

23
class ConflictedDeclarationsVisitor {
24
    readonly writer = createLanguageWriter(this.library.declarationTable.language)
25

26
    constructor(
27
        private readonly library: PeerLibrary
28
    ) {}
29

30
    print() {
31
        const printedNames = new Set<string>()
32
        for (const decl of this.library.conflictedDeclarations) {
33
            const name = convertDeclaration(DeclarationNameConvertor.I, decl)
34
            if (printedNames.has(name)) continue
35
            printedNames.add(name)
36

37
            const parent = decl.parent
38
            if (ts.isModuleBlock(parent)) {
39
                this.writer.print(`export namespace ${parent.parent.name.text} {`)
40
                this.writer.pushIndent()
41
            }
42

43
            let maybeGenerics = ''
44
            if (ts.isClassDeclaration(decl) || ts.isInterfaceDeclaration(decl))
45
                if (decl.typeParameters?.length)
46
                    maybeGenerics = `<${decl.typeParameters.map((_, i) => `T${i}=undefined`).join(',')}>`
47
            this.writer.print(`export type ${name}${maybeGenerics} = object;`)
48

49
            if (ts.isModuleBlock(parent)) {
50
                this.writer.popIndent()
51
                this.writer.print('}')
52
            }
53
        }
54
    }
55
}
56

57
export function printConflictedDeclarations(library: PeerLibrary): string {
58
    const visitor = new ConflictedDeclarationsVisitor(library)
59
    visitor.print()
60
    return visitor.writer.getOutput().join('\n')
61
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.