Ts-morph: How to retreive the name property of a FunctionDeclaration ?

Created on 27 Feb 2020  路  2Comments  路  Source: dsherret/ts-morph

I am unable to retrieve the name property of a FunctionDeclaration.

For clarity purposes i will take a minimalistic example with this simple script : ( test.ts )

function example(){
}

As you can here, the FunctionDeclaration node has a name property : https://ts-ast-viewer.com/#code/GYVwdgxgLglg9mABAUwB4EMC2AHANsgCgEoBvAKAF8g

This pure typescript code is able to retrieve and log the name property : ( ts_comp_version.ts )

import * as ts from "typescript";
import * as fs from "fs";

function test(fileNames: string[],options: ts.CompilerOptions): void {
    function visit(node) {
        if (ts.isFunctionDeclaration(node)){
            console.log((node as ts.FunctionDeclaration).name);
        }
        node.getChildren().forEach((child) => visit(child)); //recursively visit every child
    }

    const program = ts.createProgram(fileNames, options);
    for (const sourceFile of program.getSourceFiles()){
        if (!sourceFile.isDeclarationFile){
            sourceFile.forEachChild((child) => visit(child)); //visit every child
        }
    }
}

test(process.argv.slice(2), {
    target: ts.ScriptTarget.ES5,
    module: ts.ModuleKind.CommonJS
});

_To Run the script enter tsc ts_comp_version.ts --m commonjs and then node ts_comp_version.js test.ts in cmd (with nodejs and TypeScript installed)._

But the "ts-morph equivalent" is failing to do so : ( tsm.ts )

import * as ts from "typescript";
import * as fs from "fs";
import * as tsm from "ts-morph";

function test(fileNames: string[],options: ts.CompilerOptions): void {
    function visit(node) {
        if (tsm.TypeGuards.isFunctionDeclaration(node)){ 
            console.log((node as tsm.TypeGuards.FunctionDeclaration).name)
        }
        node.getChildren().forEach((child) => visit(child)); //visit every child
    }

    const project = new tsm.Project();
    for (const fileName of fileNames){
        let sourceFile = project.addSourceFileAtPath(fileName);
        sourceFile.getChildren().forEach((child) => visit(child)); //visit every child
    }
}

test(process.argv.slice(2), {
    target: ts.ScriptTarget.ES5,
    module: ts.ModuleKind.CommonJS
});

_To Run the script enter tsc tsm.ts --m commonjs (it won't work) and then (if the issue was fixed) node tsm.js test.ts in cmd (with nodejs, TypeScript and ts-morph installed)._
error TS2694: Namespace '"C:/(...)/nodejs/node-v12.16.0-win-x64/node_modules/ts-morph/lib/ts-morph"' has no exported member 'TypeGuards'.

What I am missing here, how can I access the name property ?

I need to retrieve it in order to be able to get symbols. There is currently no documentation about ts-morph symbols ( https://ts-morph.com/details/index ) so I suppose that they are retrieved in ts-compiler fashion: const symbol = checker.getSymbolAtLocation((node as ts.FunctionDeclaration).name); ?

question

Most helpful comment

Sorry, I鈥檝e been busy the past few days so haven鈥檛 got to your other questions. I鈥檓 also on my phone. What can be helpful is to search through the ts-morph .d.ts file for things. Ex. There is a .getSymbol() method on every node.

All 2 comments

You should be getting a compile error with that code. Those isX checks are now on Node and not TypeGuards. Use Node.isFunctionDeclaration then you can use getName.

Sorry, I鈥檝e been busy the past few days so haven鈥檛 got to your other questions. I鈥檓 also on my phone. What can be helpful is to search through the ts-morph .d.ts file for things. Ex. There is a .getSymbol() method on every node.

Was this page helpful?
0 / 5 - 0 ratings