The sourceFile getters are not retreiving all targeted objects from their sourceFile
See original GitHub issueThe sourceFile getters are not retrieving all targeted objects from their sourceFile:
- sourceFile.getFunctions() only retrieves some functions of the given sourceFile
- sourceFile.getVariableDeclarations() only retrieves some variable declarations of the given sourceFile
Here is my testing script ( tsm.ts ):
import * as ts from "typescript";
import * as fs from "fs";
import * as tsm from "ts-morph";
function generateDocumentation(fileNames: string[],options: ts.CompilerOptions): void {
const project = new tsm.Project();
const checker = project.getTypeChecker();
for (const fileName of fileNames){
const sourceFile = project.addSourceFileAtPath(fileName);
console.log("printing all functions of the given sourceFile:");
sourceFile.getFunctions().forEach((f) => console.log("name: " + f.getName() + " - type: "+ f.getType().getText()));
console.log("\nPrinting all variables of the given sourceFile");
sourceFile.getVariableDeclarations().forEach((v) => console.log("name: " + v.getName() + " - type: "+ v.getType().getText() ));
}
}
generateDocumentation(process.argv.slice(2), {
target: ts.ScriptTarget.ES5,
module: ts.ModuleKind.CommonJS
});
Here is the source code I use for my tests: test.ts
//Sourcecode test
// WS for Workspace
var WS = (function ($) {
"use strict";
/**
* This is just a test-function
* @param {boolean} bool
* @return {string}
*/
function functionTest(bool) {
return "abc";
}
/* Variables */
var var0 = [];
var var1 = {},
var2 = 0,
var3 = '',
var4 = null,
var5;
/**
* This is just an anonymous test-function
* @return {int}
*/
var anonFunc0 = function () {
return 42;
}
/** Retrieve UserGroups from the server, store them in the variable Grouptest.
* This is just an anonymous test-function with an AJAX style promise
**/
var anonFunc1 = function () {
return new Promise(function (resolve, reject) {
let DataObject = {
"RequestType": "request",
"Session_ID": SessionID
};
let successAjax = function (data) {
data = data.d;
resolve(data);
}
ajax({
url: "WebServices/Workspace_Ajax.asmx/WebMethodSimple",
data: JSON.stringify(DataObject),
origin: "anonFunc1"
}).then(successAjax, reject);
});
};
return {// This dictionnary contains is what is inside the WS object, so it contains references to public methods
anonFunc0 : anonFunc0,
anonFunc1 : anonFunc1,
}
})(jQuery);
$(document).on("click", '#Button1', WS.anonFunc1);
const dateToString = d => `${d.getFullYear()}-${('00' + (d.getMonth() + 1)).slice(-2)}-${('00' + d.getDate()).slice(-2)}`;
const MonthToString = d => `${d.getFullYear()}-${('00' + (d.getMonth() + 1)).slice(-2)}`;
/**
* This is just a test-function outide WS
* @param {boolean} bool
* @return {boolean}
*/
function func0(bool){
console.log("DoSomething");
return bool;
}
/**
* This is just a test-function outide WS returning a promise
*/
function func1() {
return new Promise(function (resolve) {
console.log("DoSomething");
resolve(true);
});
}
To launch the script in cmd (windows 10), I use this command:
tsc tsm.ts --m commonjs && node tsm.js test.ts
Current output printing all functions of the given sourceFile: name: func0 - type: typeof func0 name: func1 - type: typeof func1
Printing all variables of the given sourceFile name: WS - type: ($: any) => void name: dateToString - type: (d: any) => string name: MonthToString - type: (d: any) => string
Expected output Printing all functions of the given sourceFile: [Every functions printed: at least functionTest, func0, func1 and maybe the anonymous function (I don’t know about the anonymous functions, I didn’t find anything about them in the documentation)]
Printing all variables of the given sourceFile [Every variables printed : at least var0, var1, var2, var3, var4, var5 and maybe the anonymous function]
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
findReferences()
andfindReferencesAsNodes()
is just a wrapper around the LanguageService’sfindReferences()
method. It will find all references within the “program” (so it should return all references and not be limited).I think
getDescendantsOfKind
can be used to get JSDoc parameters. Open an issue if not as it would be good to change it to do that. Generally though you can get jsdoc information by having a node (say aFunctionDeclaration
) then calling#getJsDocs()
on it. From there, you can call#getTags()
then filter byNode.isJSDocParameterTag(tag)
.They’re on any node that can have statements. So those methods also exist on
FunctionDeclaration
,NamespaceDeclaration
,MethodDeclaration
, etc. It’s not on every node because not all nodes can have children that are functions, for example.@pierregallard sorry for my delay. It will be slower to do that. You’ll want to exclusively use the AST to be fast.
Something like:
Also, see my answer here for another scenario: https://github.com/dsherret/ts-morph/issues/800#issuecomment-619630622