Skip to content

Commit 6d85f71

Browse files
author
Noam Kfir
committed
Discover named js functions with "Object." prefixes, simplify symbol info extractors interface
1 parent c603e58 commit 6d85f71

File tree

2 files changed

+58
-21
lines changed

2 files changed

+58
-21
lines changed

src/services/languages/javascript/methodExtractor.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,10 @@ export class JSMethodExtractor implements IMethodExtractor {
9393
for (const symbol of symbols) {
9494
const symbolPath = (parentSymbolPath ? parentSymbolPath + '.' : '') + symbol.name;
9595

96-
const symbolInfo: SymbolInfo | undefined = symbolInfoExtractors.reduce(
97-
(info: SymbolInfo | undefined, extractor) => info || extractor.extract(symbol, codeObjectPath, symbol.name, document, symbolPath),
98-
undefined,
99-
);
100-
if(symbolInfo) {
101-
symbolInfos.push(symbolInfo);
96+
for (const extractor of symbolInfoExtractors) {
97+
symbolInfos.push(
98+
...extractor.extract(symbol, codeObjectPath, symbol.name, document, symbolPath)
99+
);
102100
}
103101

104102
const hasChildren = symbol.children && symbol.children.length > 0;

src/services/languages/javascript/symbolInfoExtractors.ts

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,31 @@ import { SymbolInfo } from '../extractors';
44
import { Token } from '../tokens';
55

66
export abstract class SymbolInfoExtractor {
7-
extract(symbol: DocumentSymbol, codeObjectPath: string, name: string, document: vscode.TextDocument, symbolPath: string): SymbolInfo | undefined {
7+
extract(
8+
symbol: DocumentSymbol,
9+
codeObjectPath: string,
10+
names: string | string[],
11+
document: vscode.TextDocument,
12+
symbolPath: string
13+
): SymbolInfo[] {
814
const range = new vscode.Range(
915
new vscode.Position(symbol.range.start.line, symbol.range.start.character),
1016
new vscode.Position(symbol.range.end.line, symbol.range.end.character)
1117
);
12-
const id = `${codeObjectPath}$_$${name}`;
1318

14-
return {
15-
id,
16-
name,
17-
codeLocation: codeObjectPath,
18-
displayName: symbolPath,
19-
range,
20-
documentUri: document.uri
21-
};
19+
names = Array.isArray(names) ? names : [names];
20+
21+
return names.map(name => {
22+
const id = `${codeObjectPath}$_$${name}`;
23+
return {
24+
id,
25+
name,
26+
codeLocation: codeObjectPath,
27+
displayName: symbolPath,
28+
range,
29+
documentUri: document.uri
30+
};
31+
});
2232
}
2333

2434
protected isOfKind(symbol: DocumentSymbol, kind: number): boolean {
@@ -27,28 +37,49 @@ export abstract class SymbolInfoExtractor {
2737
}
2838

2939
export class MethodSymbolInfoExtractor extends SymbolInfoExtractor {
30-
extract(symbol: DocumentSymbol, codeObjectPath: string, name: string, document: vscode.TextDocument, symbolPath: string): SymbolInfo | undefined {
40+
extract(
41+
symbol: DocumentSymbol,
42+
codeObjectPath: string,
43+
name: string,
44+
document: vscode.TextDocument,
45+
symbolPath: string
46+
): SymbolInfo[] {
3147
if(this.isOfKind(symbol, SymbolKind.Method)) {
3248
return super.extract(symbol, codeObjectPath, name, document, symbolPath);
3349
}
50+
return [];
3451
}
3552
}
3653

3754
export class NamedFunctionDeclarationSymbolInfoExtractor extends SymbolInfoExtractor {
38-
extract(symbol: DocumentSymbol, codeObjectPath: string, name: string, document: vscode.TextDocument, symbolPath: string): SymbolInfo | undefined {
55+
extract(
56+
symbol: DocumentSymbol,
57+
codeObjectPath: string,
58+
name: string,
59+
document: vscode.TextDocument,
60+
symbolPath: string
61+
): SymbolInfo[] {
3962
if (this.isOfKind(symbol, SymbolKind.Function)) {
4063
const textLine = document.lineAt(symbol.range.start.line);
4164
const functionMatch = `\\s*function\\s*${symbol.name}`; //should handle only function declaration, and filter out function call like db.getAll()
4265
const match = textLine.text.match(functionMatch);
4366
if(match !== undefined && match !== null) {
44-
return super.extract(symbol, codeObjectPath, name, document, symbolPath);
67+
const names = [name, `Object.${name}`];
68+
return super.extract(symbol, codeObjectPath, names, document, symbolPath);
4569
}
4670
}
71+
return [];
4772
}
4873
}
4974

5075
export class AnonymousExpressRequestHandlerSymbolInfoExtractor extends SymbolInfoExtractor {
51-
extract(symbol: DocumentSymbol, codeObjectPath: string, name: string, document: vscode.TextDocument, symbolPath: string): SymbolInfo | undefined {
76+
extract(
77+
symbol: DocumentSymbol,
78+
codeObjectPath: string,
79+
name: string,
80+
document: vscode.TextDocument,
81+
symbolPath: string
82+
): SymbolInfo[] {
5283
if (this.isOfKind(symbol, SymbolKind.Function)) {
5384
const pattern = /.*(get|head|post|put|delete|connect|options|trace|patch)\s*\(\s*['"`](.*)['"`]\)\s*callback$/i;
5485
const match = name.match(pattern);
@@ -58,6 +89,7 @@ export class AnonymousExpressRequestHandlerSymbolInfoExtractor extends SymbolInf
5889
return super.extract(symbol, codeObjectPath, name, document, symbolPath);
5990
}
6091
}
92+
return [];
6193
}
6294
}
6395

@@ -69,13 +101,20 @@ export class VariableFunctionSymbolInfoExtractor extends SymbolInfoExtractor {
69101
super();
70102
}
71103

72-
extract(symbol: DocumentSymbol, codeObjectPath: string, name: string, document: vscode.TextDocument, symbolPath: string): SymbolInfo | undefined {
104+
extract(
105+
symbol: DocumentSymbol,
106+
codeObjectPath: string,
107+
name: string,
108+
document: vscode.TextDocument,
109+
symbolPath: string
110+
): SymbolInfo[] {
73111
if (this.isOfKind(symbol, SymbolKind.Variable)) {
74112
const key = this.getKey(symbol.range.start.line, symbol.range.start.character);
75113
const functionToken = this.functionMap[key];
76114
if(functionToken !== undefined) {
77115
return super.extract(symbol, codeObjectPath, name, document, symbolPath);
78116
}
79117
}
118+
return [];
80119
}
81120
}

0 commit comments

Comments
 (0)