idlize
193 строки · 7.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 { DeclarationConvertor, TypeNodeConvertor, convertDeclaration, convertTypeNode } from "./TypeNodeConvertor";18import { getDeclarationsByNode } from '../util';19import { mapType } from './TypeNodeNameConvertor';20
21export class TypeDependenciesCollector implements TypeNodeConvertor<ts.Declaration[]> {22constructor(protected readonly typeChecker: ts.TypeChecker) {}23
24convertUnion(node: ts.UnionTypeNode): ts.Declaration[] {25return node.types.flatMap(type => convertTypeNode(this, type))26}27convertTypeLiteral(node: ts.TypeLiteralNode): ts.Declaration[] {28return node.members.flatMap(it => {29if (ts.isPropertySignature(it)) {30return convertTypeNode(this, it.type!)31}32if (ts.isIndexSignatureDeclaration(it))33return [34...it.parameters.flatMap(it => this.convert(it.type)),35...this.convert(it.type),36]37throw new Error(`Not implemented ${ts.SyntaxKind[it.kind]}`)38})39}40convertLiteralType(node: ts.LiteralTypeNode): ts.Declaration[] {41return []42}43convertTuple(node: ts.TupleTypeNode): ts.Declaration[] {44return node.elements.flatMap(it => {45if (ts.isNamedTupleMember(it))46return convertTypeNode(this, it.type)47return convertTypeNode(this, it)48})49}50convertArray(node: ts.ArrayTypeNode): ts.Declaration[] {51return convertTypeNode(this, node.elementType)52}53convertOptional(node: ts.OptionalTypeNode): ts.Declaration[] {54return convertTypeNode(this, node.type)55}56convertFunction(node: ts.FunctionTypeNode): ts.Declaration[] {57return [58...node.parameters.flatMap(it => convertTypeNode(this, it.type!)),59...convertTypeNode(this, node.type),60]61}62convertTemplateLiteral(node: ts.TemplateLiteralTypeNode): ts.Declaration[] {63return []64}65convertImport(node: ts.ImportTypeNode): ts.Declaration[] {66return []67}68convertTypeReference(node: ts.TypeReferenceNode): ts.Declaration[] {69let declarations = getDeclarationsByNode(this.typeChecker, node.typeName)70if (declarations.length > 1) {71console.log(`WARNING: Duplicate declarations temporary unsupported: ${mapType(node)}`)72declarations = [declarations[0]]73}74return [75...(node.typeArguments?.flatMap(it => convertTypeNode(this, it)) ?? []),76...declarations.map(it => {77return ts.isEnumMember(it) ? it.parent : it78}),79]80}81convertParenthesized(node: ts.ParenthesizedTypeNode): ts.Declaration[] {82return convertTypeNode(this, node.type)83}84convertIndexedAccess(node: ts.IndexedAccessTypeNode): ts.Declaration[] {85throw new Error('Method not implemented.');86}87convertStringKeyword(node: ts.TypeNode): ts.Declaration[] {88return []89}90convertNumberKeyword(node: ts.TypeNode): ts.Declaration[] {91return []92}93convertBooleanKeyword(node: ts.TypeNode): ts.Declaration[] {94return []95}96convertUndefinedKeyword(node: ts.TypeNode): ts.Declaration[] {97return []98}99convertVoidKeyword(node: ts.TypeNode): ts.Declaration[] {100return []101}102convertObjectKeyword(node: ts.TypeNode): ts.Declaration[] {103return []104}105convertAnyKeyword(node: ts.TypeNode): ts.Declaration[] {106return []107}108convertUnknownKeyword(node: ts.TypeNode): ts.Declaration[] {109return []110}111
112convert(node: ts.TypeNode | undefined): ts.Declaration[] {113if (node === undefined)114return []115return convertTypeNode(this, node)116}117}
118
119export class DeclarationDependenciesCollector implements DeclarationConvertor<ts.Declaration[]> {120constructor(121private readonly typeChecker: ts.TypeChecker,122private readonly typeDepsCollector: TypeDependenciesCollector,123) {}124
125convertClass(node: ts.ClassDeclaration): ts.Declaration[] {126return [127...(node.heritageClauses?.flatMap(heritage => this.convertHeritageClause(heritage)) ?? []),128...node.members.flatMap(member => this.convertMember(member)),129]130}131convertInterface(node: ts.InterfaceDeclaration): ts.Declaration[] {132return [133...(node.heritageClauses?.flatMap(heritage => this.convertHeritageClause(heritage)) ?? []),134...node.members.flatMap(member => this.convertMember(member)),135]136}137protected convertHeritageClause(clause: ts.HeritageClause): ts.Declaration[] {138return clause.types.flatMap(expression => this.convertExpression(expression))139}140private convertMember(member: ts.TypeElement | ts.ClassElement): ts.Declaration[] {141if (ts.isPropertyDeclaration(member) ||142ts.isPropertySignature(member))143return this.typeDepsCollector.convert(member.type)144if (ts.isMethodDeclaration(member) ||145ts.isMethodSignature(member) ||146ts.isCallSignatureDeclaration(member) ||147ts.isGetAccessorDeclaration(member) ||148ts.isSetAccessorDeclaration(member))149return [150...member.parameters.flatMap(param => this.typeDepsCollector.convert(param.type)),151...this.typeDepsCollector.convert(member.type)152]153if (ts.isConstructorDeclaration(member) || ts.isConstructSignatureDeclaration(member))154return member.parameters.flatMap(param => this.typeDepsCollector.convert(param.type))155
156throw new Error(`Not implemented ${ts.SyntaxKind[member.kind]}`)157}158private convertExpression(expression: ts.ExpressionWithTypeArguments) {159return [160...getDeclarationsByNode(this.typeChecker, expression.expression),161...expression.typeArguments?.flatMap(type => this.typeDepsCollector.convert(type)) ?? []162]163}164convertEnum(node: ts.EnumDeclaration): ts.Declaration[] {165return []166}167convertTypeAlias(node: ts.TypeAliasDeclaration): ts.Declaration[] {168return convertTypeNode(this.typeDepsCollector, node.type)169}170
171convert(node: ts.Declaration | undefined): ts.Declaration[] {172if (node === undefined)173return []174return convertDeclaration(this, node)175}176}
177
178export class DeclarationNameConvertor implements DeclarationConvertor<string> {179convertClass(node: ts.ClassDeclaration): string {180return node.name!.text181}182convertInterface(node: ts.InterfaceDeclaration): string {183return node.name!.text184}185convertEnum(node: ts.EnumDeclaration): string {186return node.name!.text187}188convertTypeAlias(node: ts.TypeAliasDeclaration): string {189return node.name!.text190}191
192static readonly I = new DeclarationNameConvertor()193}