Skip to content

Commit c0d12e7

Browse files
authored
Add files via upload
1 parent c65ac4f commit c0d12e7

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

TSExtractor/TSExtractor/FeaturesExtractor.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { CommandLineValues } from './Common/CommandLineValues';
22
import { ProgramFeatures } from './FeaturesEntities/ProgramFeatures';
33
import { Common } from './Common/Common';
4-
import { Node, SyntaxKind, getDefaultCompilerOptions, createProgram, TypeFlags } from 'typescript';
4+
import { Node, SyntaxKind, getDefaultCompilerOptions, createProgram, TypeFlags, Program, TypeChecker, Type } from 'typescript';
55
import { ts, SourceFile, Project } from 'ts-morph'
66

77
export class FeatureExtractor {
88
private m_CommandLineValues: CommandLineValues;
99
private filePath: string;
1010
private project: Project;
1111
private sourceFile: SourceFile;
12-
private root;
13-
private program;
14-
public checker;
12+
private root: Node;
13+
private program: Program;
14+
public checker: TypeChecker;
1515
private functionAndMethodEntries: Array<ts.Symbol>;
1616
private functions: Array<Node>;
1717
private static s_ParentTypeToAddChildId: Set<String>= new Set<String>();
@@ -166,9 +166,9 @@ export class FeatureExtractor {
166166
programFeatures.setVariableName(identifier.getText());
167167

168168
// Find and set the identifier's type:
169-
let idSymbol = identifiersSymbols.filter(id => id.getName()===identifier.getText())[0];
169+
let idSymbol: ts.Symbol = identifiersSymbols.filter(id => id.getName()===identifier.getText())[0];
170170
if (idSymbol == null) return programFeatures;
171-
let currType = this.checker.getTypeOfSymbolAtLocation(idSymbol, identifier);
171+
let currType: Type = this.checker.getTypeOfSymbolAtLocation(idSymbol, identifier);
172172
if (currType.flags === TypeFlags.Object) {
173173
programFeatures.setVariableType(currType.symbol.name);
174174
}
@@ -179,6 +179,21 @@ export class FeatureExtractor {
179179
return programFeatures;
180180
}
181181

182+
/**
183+
* TODO: get the identifier's type in the following way:
184+
* First get the identifier's symbol (=entry in the symbols table) using getSymbolAtLocation;
185+
* then get the Type using getTypeOfSymbolAtLocation;
186+
* then, get the actua type using typeToString.
187+
* Notice that typeToString might get special forms of types for different type of identifiers.
188+
* For example, for functions it returns also the types of the parameters yet we need to extract only
189+
* the return type.
190+
*/
191+
/* let idSymbol: ts.Symbol = this.checker.getSymbolAtLocation(identifier);
192+
if (idSymbol == null) return programFeatures;
193+
let currType: Type = this.checker.getTypeOfSymbolAtLocation(idSymbol, identifier);
194+
let type: string = this.checker.typeToString(currType);
195+
programFeatures.setVariableType(type); */
196+
182197
// The following loop will create paths between the identifier's leaves themselves:
183198
for (let i: number = 0; i < idLeaves.length; i++) {
184199
for (let j: number = i+1; j < idLeaves.length; j++) {

0 commit comments

Comments
 (0)