Skip to content

Commit

Permalink
Resolve imported statements in inheritance clauses
Browse files Browse the repository at this point in the history
Among other things this leads to unaliasing of inherited clauses,
which ideally should be fixed but in any case this fix would be
a part of separate commit.
  • Loading branch information
Schahen committed Jan 17, 2020
1 parent 3788406 commit 76050f8
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export interface A {
ping(a: string);
}

export type B = A;
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
@file:Suppress("INTERFACE_WITH_SUPERCLASS", "OVERRIDING_FINAL_MEMBER", "RETURN_TYPE_MISMATCH_ON_OVERRIDE", "CONFLICTING_OVERLOADS", "EXTERNAL_DELEGATION")

import kotlin.js.*
import kotlin.js.Json
import org.khronos.webgl.*
import org.w3c.dom.*
import org.w3c.dom.events.*
import org.w3c.dom.parsing.*
import org.w3c.dom.svg.*
import org.w3c.dom.url.*
import org.w3c.fetch.*
import org.w3c.files.*
import org.w3c.notifications.*
import org.w3c.performance.*
import org.w3c.workers.*
import org.w3c.xhr.*

external open class C : A {
open fun ping(c: Any)
override fun ping(a: String)
}

external open class D : A {
open fun ping(d: Any)
override fun ping(a: String)
}

// ------------------------------------------------------------------------------------------
@file:Suppress("INTERFACE_WITH_SUPERCLASS", "OVERRIDING_FINAL_MEMBER", "RETURN_TYPE_MISMATCH_ON_OVERRIDE", "CONFLICTING_OVERLOADS", "EXTERNAL_DELEGATION")

import kotlin.js.*
import kotlin.js.Json
import org.khronos.webgl.*
import org.w3c.dom.*
import org.w3c.dom.events.*
import org.w3c.dom.parsing.*
import org.w3c.dom.svg.*
import org.w3c.dom.url.*
import org.w3c.fetch.*
import org.w3c.files.*
import org.w3c.notifications.*
import org.w3c.performance.*
import org.w3c.workers.*
import org.w3c.xhr.*

external interface A {
fun ping(a: String)
}

typealias B = A
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {A, B} from "./_importedApi";

declare class C implements A {
ping(c: any);
}

declare class D implements A {
ping(d: any);
}
66 changes: 39 additions & 27 deletions typescript/ts-converter/src/AstConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,27 @@ export class AstConverter {
}
}

private createTypeReferenceFromSymbol(symbol: ts.Symbol | null, declaration: ts.Declaration | null): ReferenceEntity | null {
if ((symbol == null) || (declaration == null)) {
return null;
}
let typeReference: ReferenceEntity | null = null;
if (ts.isImportSpecifier(declaration)) {
let typeOsSymbol = this.typeChecker.getDeclaredTypeOfSymbol(symbol);
if (typeOsSymbol && typeOsSymbol.symbol && Array.isArray(typeOsSymbol.symbol.declarations)) {
let declarationFromSymbol = typeOsSymbol.symbol.declarations[0];
//TODO: encountered in @types/express, need to work on a separate test case
let uidContext = (declarationFromSymbol.parent && ts.isTypeAliasDeclaration(declarationFromSymbol.parent))?
declarationFromSymbol.parent : declarationFromSymbol;
typeReference = this.astFactory.createReferenceEntity(this.exportContext.getUID(uidContext));
}
} else {
typeReference = this.astFactory.createReferenceEntity(this.exportContext.getUID(declaration));
}

return typeReference;
}

convertType(type: ts.TypeNode | undefined): TypeDeclaration {
if (type == undefined) {
return this.createTypeDeclaration("Any")
Expand Down Expand Up @@ -375,19 +396,7 @@ export class AstConverter {
return this.astFactory.createTypeParamReferenceDeclarationAsParamValue(entity);
}

if (ts.isImportSpecifier(declaration)) {
let typeOsSymbol = this.typeChecker.getDeclaredTypeOfSymbol(symbol);
if (typeOsSymbol && typeOsSymbol.symbol && Array.isArray(typeOsSymbol.symbol.declarations)) {
let declarationFromSymbol = typeOsSymbol.symbol.declarations[0];
//TODO: encountered in @types/express, need to work on a separate test case
let uidContext = (declarationFromSymbol.parent && ts.isTypeAliasDeclaration(declarationFromSymbol.parent))?
declarationFromSymbol.parent : declarationFromSymbol;
typeReference = this.astFactory.createReferenceEntity(this.exportContext.getUID(uidContext));
}
} else {
typeReference = this.astFactory.createReferenceEntity(this.exportContext.getUID(declaration));
}

typeReference = this.createTypeReferenceFromSymbol(symbol, declaration);
}
}
}
Expand Down Expand Up @@ -673,6 +682,18 @@ export class AstConverter {
return this.astFactory.createQualifiedNameDeclaration(convertedExpression, name);
}

private getFirstDeclaration(symbol: ts.Symbol | null): ts.Declaration | null {
if (symbol == null) {
return null;
}

if (Array.isArray(symbol.declarations)) {
return symbol.declarations[0];
}

return null;
}

convertHeritageClauses(heritageClauses: ts.NodeArray<ts.HeritageClause> | undefined, parent: ts.Node): Array<HeritageClauseDeclaration> {
let parentEntities: Array<HeritageClauseDeclaration> = [];

Expand All @@ -699,23 +720,14 @@ export class AstConverter {
name = this.astFactory.createIdentifierDeclarationAsNameEntity(expression.getText());
}

let uid: string | null = null;

let symbol = this.typeChecker.getSymbolAtLocation(type.expression);
if (symbol) {
if (Array.isArray(symbol.declarations)) {
let declaration = symbol.declarations[0];
if (declaration) {
this.astVisitor.visitType(declaration);
uid = this.exportContext.getUID(declaration);
}
}
let declaration = this.getFirstDeclaration(symbol);
if (declaration) {
this.astVisitor.visitType(declaration);
}

let parentUid = this.exportContext.getUID(parent);

if (parentUid != uid) {
let typeReference = uid ? this.astFactory.createReferenceEntity(uid) : null;
if (declaration != parent) {
let typeReference = this.createTypeReferenceFromSymbol(symbol, declaration);

if (name) {
this.registerDeclaration(
Expand Down

0 comments on commit 76050f8

Please sign in to comment.