Skip to content

Commit 78a7914

Browse files
Get sighelp for arbitrary functions working in .js files.
1 parent 766d34d commit 78a7914

4 files changed

Lines changed: 51 additions & 4 deletions

File tree

src/services/outliningElementsCollector.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module ts {
3535
return isFunctionBlock(node) && node.parent.kind !== SyntaxKind.ArrowFunction;
3636
}
3737

38+
3839
let depth = 0;
3940
let maxDepth = 20;
4041
function walk(n: Node): void {

src/services/services.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2470,10 +2470,6 @@ module ts {
24702470
return program.getSyntacticDiagnostics(getValidSourceFile(fileName));
24712471
}
24722472

2473-
function isJavaScript(fileName: string) {
2474-
return fileExtensionIs(fileName, ".js");
2475-
}
2476-
24772473
/**
24782474
* getSemanticDiagnostiscs return array of Diagnostics. If '-d' is not enabled, only report semantic errors
24792475
* If '-d' enabled, report both semantic and emitter errors

src/services/signatureHelp.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,57 @@ module ts.SignatureHelp {
202202
cancellationToken.throwIfCancellationRequested();
203203

204204
if (!candidates.length) {
205+
// We didn't have any sig help items produced by the TS compiler. If this is a JS
206+
// file, then see if we can figure out anything better.
207+
if (isJavaScript(sourceFile.fileName)) {
208+
return createJavaScriptSignatureHelpItems(argumentInfo);
209+
}
210+
205211
return undefined;
206212
}
207213

208214
return createSignatureHelpItems(candidates, resolvedSignature, argumentInfo);
209215

216+
function createJavaScriptSignatureHelpItems(argumentInfo: ArgumentListInfo): SignatureHelpItems {
217+
if (argumentInfo.invocation.kind !== SyntaxKind.CallExpression) {
218+
return undefined;
219+
}
220+
221+
// See if we can find some symbol with the call expression name that has call signatures.
222+
let callExpression = <CallExpression>argumentInfo.invocation;
223+
let expression = callExpression.expression;
224+
let name = expression.kind === SyntaxKind.Identifier
225+
? <Identifier> expression
226+
: expression.kind === SyntaxKind.PropertyAccessExpression
227+
? (<PropertyAccessExpression>expression).name
228+
: undefined;
229+
230+
if (!name || !name.text) {
231+
return undefined;
232+
}
233+
234+
let typeChecker = program.getTypeChecker();
235+
for (let sourceFile of program.getSourceFiles()) {
236+
let nameToDeclarations = sourceFile.getNamedDeclarations();
237+
let declarations = getProperty(nameToDeclarations, name.text);
238+
239+
if (declarations) {
240+
for (let declaration of declarations) {
241+
let symbol = declaration.symbol;
242+
if (symbol) {
243+
let type = typeChecker.getTypeOfSymbolAtLocation(symbol, declaration);
244+
if (type) {
245+
let callSignatures = type.getCallSignatures();
246+
if (callSignatures && callSignatures.length) {
247+
return createSignatureHelpItems(callSignatures, callSignatures[0], argumentInfo);
248+
}
249+
}
250+
}
251+
}
252+
}
253+
}
254+
}
255+
210256
/**
211257
* Returns relevant information for the argument list and the current argument if we are
212258
* in the argument of an invocation; returns undefined otherwise.

src/services/utilities.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,4 +650,8 @@ module ts {
650650
typechecker.getSymbolDisplayBuilder().buildSignatureDisplay(signature, writer, enclosingDeclaration, flags);
651651
});
652652
}
653+
654+
export function isJavaScript(fileName: string) {
655+
return fileExtensionIs(fileName, ".js");
656+
}
653657
}

0 commit comments

Comments
 (0)