1
1
import { CommandLineValues } from './Common/CommandLineValues' ;
2
2
import { ProgramFeatures } from './FeaturesEntities/ProgramFeatures' ;
3
3
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' ;
5
5
import { ts , SourceFile , Project } from 'ts-morph'
6
6
7
7
export class FeatureExtractor {
8
8
private m_CommandLineValues : CommandLineValues ;
9
9
private filePath : string ;
10
10
private project : Project ;
11
11
private sourceFile : SourceFile ;
12
- private root ;
13
- private program ;
14
- public checker ;
12
+ private root : Node ;
13
+ private program : Program ;
14
+ public checker : TypeChecker ;
15
15
private functionAndMethodEntries : Array < ts . Symbol > ;
16
16
private functions : Array < Node > ;
17
17
private static s_ParentTypeToAddChildId : Set < String > = new Set < String > ( ) ;
@@ -166,9 +166,9 @@ export class FeatureExtractor {
166
166
programFeatures . setVariableName ( identifier . getText ( ) ) ;
167
167
168
168
// 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 ] ;
170
170
if ( idSymbol == null ) return programFeatures ;
171
- let currType = this . checker . getTypeOfSymbolAtLocation ( idSymbol , identifier ) ;
171
+ let currType : Type = this . checker . getTypeOfSymbolAtLocation ( idSymbol , identifier ) ;
172
172
if ( currType . flags === TypeFlags . Object ) {
173
173
programFeatures . setVariableType ( currType . symbol . name ) ;
174
174
}
@@ -179,6 +179,21 @@ export class FeatureExtractor {
179
179
return programFeatures ;
180
180
}
181
181
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
+
182
197
// The following loop will create paths between the identifier's leaves themselves:
183
198
for ( let i : number = 0 ; i < idLeaves . length ; i ++ ) {
184
199
for ( let j : number = i + 1 ; j < idLeaves . length ; j ++ ) {
0 commit comments