Skip to content

Commit a514a04

Browse files
committed
Improve documentation
1 parent d483ee6 commit a514a04

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

src/typeSearch.ts

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export enum EstimationSource {
1111
}
1212

1313
/**
14-
* The result of a type search.
14+
* The result of a variable type search.
1515
*/
1616
export class VariableSearchResult {
1717
public typeName: string;
@@ -32,22 +32,22 @@ export class TypeSearch {
3232
* Searches for a class with the same name as object and returns the name if found.
3333
*
3434
* @param object The object.
35-
* @param documentText The text to search
35+
* @param src The source code to search.
3636
*/
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);
3939
return clsMatch ? clsMatch[1] : null;
4040
}
4141

4242
/**
4343
* Searches for a variable with the same name as the param and detects its type.
4444
*
4545
* @param param The parameter name.
46-
* @param documentText The source code of the document.
46+
* @param src The source code to search.
4747
* @returns A search result or null.
4848
*/
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);
5151
if (!match) {
5252
return null;
5353
}
@@ -65,12 +65,12 @@ export class TypeSearch {
6565

6666
if (match[0].endsWith("(")) {
6767
let value = match[1];
68-
if (this.classWithSameName(value, documentText)) {
68+
if (this.classWithSameName(value, src)) {
6969
return new VariableSearchResult(value, EstimationSource.ClassDefinition, valueAssignment);
7070
}
7171

7272
if (this.isProbablyAClass(value)) {
73-
if (!new RegExp(`^[ \t]*def ${value}\\(` ).test(documentText)) {
73+
if (!new RegExp(`^[ \t]*def ${value}\\(` ).test(src)) {
7474
return new VariableSearchResult(value, EstimationSource.Value, valueAssignment);
7575
}
7676
} else {
@@ -81,7 +81,7 @@ export class TypeSearch {
8181
// Find the function definition and check if the return type is hinted
8282
const regExp = new RegExp(`^[ \t]*def ${value}\\([^)]*\\) *-> *([a-zA-Z_][a-zA-Z0-9_.\\[\\]]+)`, "m");
8383

84-
const hintedCallMatch = regExp.exec(documentText);
84+
const hintedCallMatch = regExp.exec(src);
8585

8686
if (hintedCallMatch && hintedCallMatch.length === 2) {
8787
return new VariableSearchResult(
@@ -95,9 +95,9 @@ export class TypeSearch {
9595
}
9696

9797
// 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))) {
9999

100-
if (match = this.variableSearchRegExp(match[1]).exec(documentText)) {
100+
if (match = this.variableSearchRegExp(match[1]).exec(src)) {
101101
const otherType = this.detectType(match[1]);
102102
return otherType
103103
? new VariableSearchResult(otherType, EstimationSource.ValueOfOtherVariable, valueAssignment)
@@ -108,9 +108,9 @@ export class TypeSearch {
108108
}
109109

110110
/**
111-
* Detects the type of a value, if it is a built in Python type.
111+
* Detects the type of a value.
112112
*
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.
114114
*/
115115
public static detectType(value: string): string | null {
116116
const searches = [
@@ -148,13 +148,11 @@ export class TypeSearch {
148148
* Searches for a previously hinted param with the same name.
149149
*
150150
* @param param The parameter name.
151-
* @param documentText The document text to search.
151+
* @param src The document text to search.
152152
* @returns The type hint of the found parameter or null.
153153
*/
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);
158156
if (m) {
159157
let hint = m[1].trim();
160158
return hint ? hint : null;
@@ -166,6 +164,7 @@ export class TypeSearch {
166164
* Searches the result for a terinary operator that might return 2 or more different types.
167165
*
168166
* @param searchResult The search result.
167+
* @returns False if it returns a single type.
169168
*/
170169
public static invalidTernaryOperator(searchResult: VariableSearchResult): boolean {
171170

@@ -192,14 +191,17 @@ export class TypeSearch {
192191
}
193192

194193
/**
195-
* Detects if an object is imported.
194+
* Detects if a value is imported in a document.
196195
*
196+
* @param value The value.
197+
* @param src The document text to search.
198+
* @param considerAsImports Also search for 'import x as value' imports.
197199
* @returns The imported value.
198200
*/
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 {
200202

201-
if (object.includes(".")) {
202-
const s = object.split(".");
203+
if (value.includes(".")) {
204+
const s = value.split(".");
203205
const type = s[s.length - 1];
204206
const module = s.slice(0, s.length - 1).join(".");
205207

@@ -208,30 +210,29 @@ export class TypeSearch {
208210
if (s.length === 2 && module !== type) {
209211
match = new RegExp(
210212
`^[ \t]*import +${module}|^[ \t]*from ${anyClassOrFunctionName} import (${module})`, "m"
211-
).exec(documentText);
213+
).exec(src);
212214
if (match) {
213215
// Return 'Object.Type' for 'from x import Object'
214-
return match[1] ? `${match[1]}.${type}` : object;
216+
return match[1] ? `${match[1]}.${type}` : value;
215217
}
216218
}
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;
220221
}
221-
return this.isImported(object, documentText, checkForAsImports) ? object : null;
222+
return this.isImported(value, src, considerAsImports) ? value : null;
222223
}
223224

224225
/**
225-
* Detects if an object is imported.
226+
* Detects if a value is imported.
226227
*/
227-
private static isImported(object: string, documentText: string, checkForAsImports: boolean = true): boolean {
228+
private static isImported(value: string, src: string, checkForAsImports: boolean = true): boolean {
228229

229-
let exp = `^[ \t]*(from +${anyClassOrFunctionName} +import +${object}`;
230+
let exp = `^[ \t]*(from +${anyClassOrFunctionName} +import +${value}`;
230231
if (checkForAsImports) {
231-
exp += `|from +${anyClassOrFunctionName} +import +${anyClassOrFunctionName} +as +${object}`;
232+
exp += `|from +${anyClassOrFunctionName} +import +${anyClassOrFunctionName} +as +${value}`;
232233
}
233234
exp += ")";
234-
return new RegExp(exp,"m").test(documentText);
235+
return new RegExp(exp,"m").test(src);
235236
}
236237

237238
private static isProbablyAClass(lineText: string): boolean {

0 commit comments

Comments
 (0)