idlize

Форк
0
/
synthetic_declaration.ts 
60 строк · 2.5 Кб
1
import * as ts from 'typescript'
2
import { ImportFeature, ImportsCollector } from './ImportsCollector'
3

4
const syntheticDeclarations: Map<string, {node: ts.Declaration, filename: string, dependencies: ImportFeature[]}> = new Map()
5
export function makeSyntheticDeclaration(targetFilename: string, declName: string, factory: () => ts.Declaration): ts.Declaration {
6
    if (!syntheticDeclarations.has(declName))
7
        syntheticDeclarations.set(declName, {node: factory(), filename: targetFilename, dependencies: []})
8
    const decl = syntheticDeclarations.get(declName)!
9
    if (decl.filename !== targetFilename)
10
        throw "Two declarations with same name were declared"
11
    return decl.node
12
}
13

14
export function addSyntheticDeclarationDependency(node: ts.Declaration, dependency: ImportFeature) {
15
    for (const decl of syntheticDeclarations.values())
16
        if (decl.node === node) {
17
            decl.dependencies.push(dependency)
18
            return
19
        }
20
    throw "Declaration is not synthetic"
21
}
22

23
export function makeSyntheticTypeAliasDeclaration(targetFilename: string, declName: string, type: ts.TypeNode): ts.TypeAliasDeclaration {
24
    const decl = makeSyntheticDeclaration(targetFilename, declName, () => {
25
        return ts.factory.createTypeAliasDeclaration(
26
            undefined,
27
            declName,
28
            undefined,
29
            type
30
        )
31
    })
32
    if (!ts.isTypeAliasDeclaration(decl))
33
        throw "Expected declaration to be a TypeAlias"
34
    return decl
35
}
36

37
export function isSyntheticDeclaration(node: ts.Declaration): boolean {
38
    for (const decl of syntheticDeclarations.values())
39
        if (decl.node === node)
40
            return true
41
    return false
42
}
43

44
export function syntheticDeclarationFilename(node: ts.Declaration): string {
45
    for (const decl of syntheticDeclarations.values())
46
        if (decl.node === node)
47
            return decl.filename
48
    throw "Declaration is not synthetic"
49
}
50

51
export function makeSyntheticDeclarationsFiles(): Map<string, {dependencies: ImportFeature[], declarations: ts.Declaration[]}> {
52
    const files = new Map<string, {dependencies: ImportFeature[], declarations: ts.Declaration[]}>()
53
    for (const decl of syntheticDeclarations.values()) {
54
        if (!files.has(decl.filename))
55
            files.set(decl.filename, {dependencies: [], declarations: []})
56
        files.get(decl.filename)!.declarations.push(decl.node)
57
        files.get(decl.filename)!.dependencies.push(...decl.dependencies)
58
    }
59
    return files
60
}

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

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

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

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