@@ -11,7 +11,7 @@ export enum EstimationSource {
11
11
}
12
12
13
13
/**
14
- * The result of a type search.
14
+ * The result of a variable type search.
15
15
*/
16
16
export class VariableSearchResult {
17
17
public typeName : string ;
@@ -32,22 +32,22 @@ export class TypeSearch {
32
32
* Searches for a class with the same name as object and returns the name if found.
33
33
*
34
34
* @param object The object.
35
- * @param documentText The text to search
35
+ * @param src The source code to search.
36
36
*/
37
- public static classWithSameName ( object : string , documentText : string ) : string | null {
38
- const clsMatch = new RegExp ( `^ *class +(${ object } )[(:]` , "mi" ) . exec ( documentText ) ;
37
+ public static classWithSameName ( object : string , src : string ) : string | null {
38
+ const clsMatch = new RegExp ( `^ *class +(${ object } )[(:]` , "mi" ) . exec ( src ) ;
39
39
return clsMatch ? clsMatch [ 1 ] : null ;
40
40
}
41
41
42
42
/**
43
43
* Searches for a variable with the same name as the param and detects its type.
44
44
*
45
45
* @param param The parameter name.
46
- * @param documentText The source code of the document .
46
+ * @param src The source code to search .
47
47
* @returns A search result or null.
48
48
*/
49
- public static async variableWithSameName ( param : string , documentText : string ) : Promise < VariableSearchResult | null > {
50
- let match = this . variableSearchRegExp ( param ) . exec ( documentText ) ;
49
+ public static async variableWithSameName ( param : string , src : string ) : Promise < VariableSearchResult | null > {
50
+ let match = this . variableSearchRegExp ( param ) . exec ( src ) ;
51
51
if ( ! match ) {
52
52
return null ;
53
53
}
@@ -65,12 +65,12 @@ export class TypeSearch {
65
65
66
66
if ( match [ 0 ] . endsWith ( "(" ) ) {
67
67
let value = match [ 1 ] ;
68
- if ( this . classWithSameName ( value , documentText ) ) {
68
+ if ( this . classWithSameName ( value , src ) ) {
69
69
return new VariableSearchResult ( value , EstimationSource . ClassDefinition , valueAssignment ) ;
70
70
}
71
71
72
72
if ( this . isProbablyAClass ( value ) ) {
73
- if ( ! new RegExp ( `^[ \t]*def ${ value } \\(` ) . test ( documentText ) ) {
73
+ if ( ! new RegExp ( `^[ \t]*def ${ value } \\(` ) . test ( src ) ) {
74
74
return new VariableSearchResult ( value , EstimationSource . Value , valueAssignment ) ;
75
75
}
76
76
} else {
@@ -81,7 +81,7 @@ export class TypeSearch {
81
81
// Find the function definition and check if the return type is hinted
82
82
const regExp = new RegExp ( `^[ \t]*def ${ value } \\([^)]*\\) *-> *([a-zA-Z_][a-zA-Z0-9_.\\[\\]]+)` , "m" ) ;
83
83
84
- const hintedCallMatch = regExp . exec ( documentText ) ;
84
+ const hintedCallMatch = regExp . exec ( src ) ;
85
85
86
86
if ( hintedCallMatch && hintedCallMatch . length === 2 ) {
87
87
return new VariableSearchResult (
@@ -95,9 +95,9 @@ export class TypeSearch {
95
95
}
96
96
97
97
// Searching the import source document is not supported (yet?)
98
- if ( ! this . isImported ( match [ 1 ] , documentText . substr ( match . index - match . length ) ) ) {
98
+ if ( ! this . isImported ( match [ 1 ] , src . substr ( match . index - match . length ) ) ) {
99
99
100
- if ( match = this . variableSearchRegExp ( match [ 1 ] ) . exec ( documentText ) ) {
100
+ if ( match = this . variableSearchRegExp ( match [ 1 ] ) . exec ( src ) ) {
101
101
const otherType = this . detectType ( match [ 1 ] ) ;
102
102
return otherType
103
103
? new VariableSearchResult ( otherType , EstimationSource . ValueOfOtherVariable , valueAssignment )
@@ -108,9 +108,9 @@ export class TypeSearch {
108
108
}
109
109
110
110
/**
111
- * Detects the type of a value, if it is a built in Python type .
111
+ * Detects the type of a value.
112
112
*
113
- * @returns The type name or null if not found .
113
+ * @returns The type name, or null if it is not a built-in Python type .
114
114
*/
115
115
public static detectType ( value : string ) : string | null {
116
116
const searches = [
@@ -148,13 +148,11 @@ export class TypeSearch {
148
148
* Searches for a previously hinted param with the same name.
149
149
*
150
150
* @param param The parameter name.
151
- * @param documentText The document text to search.
151
+ * @param src The document text to search.
152
152
* @returns The type hint of the found parameter or null.
153
153
*/
154
- public static hintOfSimilarParam ( param : string , documentText : string ) : string | null {
155
- const m = new RegExp (
156
- `^[ \t]*def ${ anyClassOrFunctionName } \\([^)]*\\b${ param } \\b: *([^), ]+)` , "m"
157
- ) . exec ( documentText ) ;
154
+ public static hintOfSimilarParam ( param : string , src : string ) : string | null {
155
+ const m = new RegExp ( `^[ \t]*def ${ anyClassOrFunctionName } \\([^)]*\\b${ param } \\b: *([^), ]+)` , "m" ) . exec ( src ) ;
158
156
if ( m ) {
159
157
let hint = m [ 1 ] . trim ( ) ;
160
158
return hint ? hint : null ;
@@ -166,6 +164,7 @@ export class TypeSearch {
166
164
* Searches the result for a terinary operator that might return 2 or more different types.
167
165
*
168
166
* @param searchResult The search result.
167
+ * @returns False if it returns a single type.
169
168
*/
170
169
public static invalidTernaryOperator ( searchResult : VariableSearchResult ) : boolean {
171
170
@@ -192,14 +191,17 @@ export class TypeSearch {
192
191
}
193
192
194
193
/**
195
- * Detects if an object is imported.
194
+ * Detects if a value is imported in a document .
196
195
*
196
+ * @param value The value.
197
+ * @param src The document text to search.
198
+ * @param considerAsImports Also search for 'import x as value' imports.
197
199
* @returns The imported value.
198
200
*/
199
- public static findImport ( object : string , documentText : string , checkForAsImports : boolean = true ) : string | null {
201
+ public static findImport ( value : string , src : string , considerAsImports : boolean = true ) : string | null {
200
202
201
- if ( object . includes ( "." ) ) {
202
- const s = object . split ( "." ) ;
203
+ if ( value . includes ( "." ) ) {
204
+ const s = value . split ( "." ) ;
203
205
const type = s [ s . length - 1 ] ;
204
206
const module = s . slice ( 0 , s . length - 1 ) . join ( "." ) ;
205
207
@@ -208,30 +210,29 @@ export class TypeSearch {
208
210
if ( s . length === 2 && module !== type ) {
209
211
match = new RegExp (
210
212
`^[ \t]*import +${ module } |^[ \t]*from ${ anyClassOrFunctionName } import (${ module } )` , "m"
211
- ) . exec ( documentText ) ;
213
+ ) . exec ( src ) ;
212
214
if ( match ) {
213
215
// Return 'Object.Type' for 'from x import Object'
214
- return match [ 1 ] ? `${ match [ 1 ] } .${ type } ` : object ;
216
+ return match [ 1 ] ? `${ match [ 1 ] } .${ type } ` : value ;
215
217
}
216
218
}
217
- match = new RegExp ( `^[ \t]*import +${ module } |^[ \t]*from ${ module } import (${ type } )` , "m" )
218
- . exec ( documentText ) ;
219
- return match ? match [ 1 ] ? match [ 1 ] : object : null ;
219
+ match = new RegExp ( `^[ \t]*import +${ module } |^[ \t]*from ${ module } import (${ type } )` , "m" ) . exec ( src ) ;
220
+ return match ? match [ 1 ] ? match [ 1 ] : value : null ;
220
221
}
221
- return this . isImported ( object , documentText , checkForAsImports ) ? object : null ;
222
+ return this . isImported ( value , src , considerAsImports ) ? value : null ;
222
223
}
223
224
224
225
/**
225
- * Detects if an object is imported.
226
+ * Detects if a value is imported.
226
227
*/
227
- private static isImported ( object : string , documentText : string , checkForAsImports : boolean = true ) : boolean {
228
+ private static isImported ( value : string , src : string , checkForAsImports : boolean = true ) : boolean {
228
229
229
- let exp = `^[ \t]*(from +${ anyClassOrFunctionName } +import +${ object } ` ;
230
+ let exp = `^[ \t]*(from +${ anyClassOrFunctionName } +import +${ value } ` ;
230
231
if ( checkForAsImports ) {
231
- exp += `|from +${ anyClassOrFunctionName } +import +${ anyClassOrFunctionName } +as +${ object } ` ;
232
+ exp += `|from +${ anyClassOrFunctionName } +import +${ anyClassOrFunctionName } +as +${ value } ` ;
232
233
}
233
234
exp += ")" ;
234
- return new RegExp ( exp , "m" ) . test ( documentText ) ;
235
+ return new RegExp ( exp , "m" ) . test ( src ) ;
235
236
}
236
237
237
238
private static isProbablyAClass ( lineText : string ) : boolean {
0 commit comments