Get sighelp for arbitrary functions working in .js files.

This commit is contained in:
Cyrus Najmabadi
2015-04-06 14:10:04 -07:00
parent 766d34d0dc
commit 78a79140b3
4 changed files with 51 additions and 4 deletions
@@ -35,6 +35,7 @@ module ts {
return isFunctionBlock(node) && node.parent.kind !== SyntaxKind.ArrowFunction;
}
let depth = 0;
let maxDepth = 20;
function walk(n: Node): void {
-4
View File
@@ -2470,10 +2470,6 @@ module ts {
return program.getSyntacticDiagnostics(getValidSourceFile(fileName));
}
function isJavaScript(fileName: string) {
return fileExtensionIs(fileName, ".js");
}
/**
* getSemanticDiagnostiscs return array of Diagnostics. If '-d' is not enabled, only report semantic errors
* If '-d' enabled, report both semantic and emitter errors
+46
View File
@@ -202,11 +202,57 @@ module ts.SignatureHelp {
cancellationToken.throwIfCancellationRequested();
if (!candidates.length) {
// We didn't have any sig help items produced by the TS compiler. If this is a JS
// file, then see if we can figure out anything better.
if (isJavaScript(sourceFile.fileName)) {
return createJavaScriptSignatureHelpItems(argumentInfo);
}
return undefined;
}
return createSignatureHelpItems(candidates, resolvedSignature, argumentInfo);
function createJavaScriptSignatureHelpItems(argumentInfo: ArgumentListInfo): SignatureHelpItems {
if (argumentInfo.invocation.kind !== SyntaxKind.CallExpression) {
return undefined;
}
// See if we can find some symbol with the call expression name that has call signatures.
let callExpression = <CallExpression>argumentInfo.invocation;
let expression = callExpression.expression;
let name = expression.kind === SyntaxKind.Identifier
? <Identifier> expression
: expression.kind === SyntaxKind.PropertyAccessExpression
? (<PropertyAccessExpression>expression).name
: undefined;
if (!name || !name.text) {
return undefined;
}
let typeChecker = program.getTypeChecker();
for (let sourceFile of program.getSourceFiles()) {
let nameToDeclarations = sourceFile.getNamedDeclarations();
let declarations = getProperty(nameToDeclarations, name.text);
if (declarations) {
for (let declaration of declarations) {
let symbol = declaration.symbol;
if (symbol) {
let type = typeChecker.getTypeOfSymbolAtLocation(symbol, declaration);
if (type) {
let callSignatures = type.getCallSignatures();
if (callSignatures && callSignatures.length) {
return createSignatureHelpItems(callSignatures, callSignatures[0], argumentInfo);
}
}
}
}
}
}
}
/**
* Returns relevant information for the argument list and the current argument if we are
* in the argument of an invocation; returns undefined otherwise.
+4
View File
@@ -650,4 +650,8 @@ module ts {
typechecker.getSymbolDisplayBuilder().buildSignatureDisplay(signature, writer, enclosingDeclaration, flags);
});
}
export function isJavaScript(fileName: string) {
return fileExtensionIs(fileName, ".js");
}
}