TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

emitter extension: how get class baseType full name

Open fabioparra opened this issue 12 years ago • 10 comments

Hi,

I'm tryng rewrite method emitClassDeclaration in emitter.ts class/file:

function emitClassDeclaration(node: ClassDeclaration)

node.baseType is a TypeReferenceNode. In some cases I cannot get full type name information with a TypeReferenceNode. Consider these classes:

module My.ux.panel { export class BasePanel { } } module My.ux.panel { export class Panel extends BasePanel { } }

Emitting class My.ux.panel.Panel how can I get node.baseType My.ux.panel.BasePanel class full name?

Before compiler rewrite I was able to get type name information with a PullSymbol object:

var baseTypeReference = ASTHelpers.getExtendsHeritageClause(classDecl.heritageClauses).typeNames.nonSeparatorAt(0);

fabioparra avatar Nov 24 '14 11:11 fabioparra

I'm not really sure but can't you do something like this?

var current = node;
while(current.parent) {
    if (current.kind == ts.SyntaxKind.ModuleDeclaration) {
       // got the module
    }
    current = node.parent
}

Not really sure if this works though because I'm currently on my phone and can't test it out but I believe there was a parent property.

DickvdBrink avatar Nov 24 '14 12:11 DickvdBrink

Hi DickvdBrink,

Yes, there is a "parent" property, but is this case when emitting "Panel" class node.baseType.parent is the "Panel" class.

fabioparra avatar Nov 24 '14 14:11 fabioparra

consider this more complex example:

module My.ux.bla.panel { export class BasePanel { } } module My.ux.foo.xxx.aaa { export class Panel extends bla.panel.BasePanel { } }

Now "Panel" and BasePanel have different namespace and I cannot get BasePanel full name when emitting "Panel"...

fabioparra avatar Nov 24 '14 14:11 fabioparra

Can you tell me for what TypeScript version you need this? Because when I change the Emitter emitClassDeclaration function with some hacky stuff (version 1.0.1) I get the following:

var current = declaration;
while (current.parent) {
    current = current.parent;
    if (current.kind() == 130) {
        break;
    }
}
debugger;

if (current) {
    console.dir(current);
    var strArray = TypeScript.ASTHelpers.getModuleNames(current.name);
    var name = "";
    for(var i = 0; i < strArray.length; i++) {
        name += strArray[i].text();
    }
    console.log(name);// Myuxblapanel and Myuxfooxxxaaa
}

This isn't fully functional with what you want but if you can get a hold of the declaration instance you can get the name of the module

DickvdBrink avatar Nov 24 '14 16:11 DickvdBrink

I'm using lastest 1.4 version in master branch...

There is no TypeScript.ASTHelpers in this version...

in previous 1.0x versions I was able to get fullname....

fabioparra avatar Nov 24 '14 16:11 fabioparra

I created 2 methods to find fullName. getDeclClassFullName is working fine, but with getTypeFullName I cannot access full type info...

//@extjsemitter helper function getDeclClassFullName(pullDecl : ClassDeclaration) : string { var parts = <string[]>[];

            if(pullDecl){
                parts.push(pullDecl.name.text);
                var moduleDecl = pullDecl.symbol.parent;

                while(moduleDecl){
                    parts.unshift(moduleDecl.name);
                    moduleDecl = moduleDecl.parent;
                }
            }
            return parts.join('.');
        }
        //@extjsemitter helper ***its not working in all cases
        function getTypeFullName(node : TypeReferenceNode) :  string {
            var parts = <string[]>[];
            var tname = <QualifiedName>node.typeName;


            while(tname){
                if((<Identifier><any>tname).text){
                    parts.unshift((<Identifier><any>tname).text);
                }else if(tname.right){
                    parts.unshift(tname.right.text);    
                }

                tname = <QualifiedName>tname.left;
            }
            return parts.join('.');
        }

fabioparra avatar Nov 24 '14 16:11 fabioparra

@fabioparra checker.ts has a function called getFullyQualifiedName. You can expose this on the type EmitResolver and call it from the emitter.

But why do you want to do this? Looking at your example, would getting the fully qualified name give you the correct result in the following case?:

module My.ux.panel { export class BasePanel { } }
module My.ux.panel.My.ux.panel { export class BasePanel { } }
module My.ux.panel { export class Panel extends BasePanel { } }

You really want to pick up the BasePanel from the 1st line, but if you emit the fully qualified name you would pick up the BasePanel from the 2nd line because it shadows the 1st one.

JsonFreeman avatar Nov 26 '14 19:11 JsonFreeman

Hi @JsonFreeman,

You gave me some idea...thanks...

I'm trying change how classes are emitted. I already got it on previous 1.0x versions:

https://github.com/fabioparra/TypeScriptExtJSEmitter https://rawgithub.com/fabioparra/TypeScriptExtJSEmitter/master/TypeScriptExtJSEmitter/index.html

So I need create this js: Ext.define('Ext.ux.panel.Panel', { extend : 'Ext.ux.panel.BasePanel' });

Function getFullyQualifiedName accepts a Symbol as parameter, but I have only a TypeReferenceNode, because its in a ClassDeclaration.baseType field.

Any ideal how get a Symbol from a TypeReferenceNode?

PS: a TypeReferenceNode contains only what is written, eg: My.ux.panel.BasePanel you can get this "full name" by reading fields node.typename.left/right, but if you write only "BasePanel" you will need other object with more info....

fabioparra avatar Nov 27 '14 11:11 fabioparra

Ah I see. There is a function in the checker called getTypeFromTypeReferenceNode. You can call that on your TypeReferenceNode and it will give you a type. Then you can access .symbol on the type, and then call getFullyQualifiedName on that symbol.

That seems like it should work, let me know if you run into more issues.

JsonFreeman avatar Nov 29 '14 07:11 JsonFreeman

It worked!! thanks @JsonFreeman!! Everything I need is in checker.ts file!! You guys rock!!

fabioparra avatar Dec 03 '14 12:12 fabioparra