Merge branch 'grabParamsFromInitializers'

This commit is contained in:
Daniel Rosenwasser 2015-09-25 17:11:33 -07:00
commit d9559d58ca
3 changed files with 163 additions and 11 deletions

View file

@ -7059,7 +7059,7 @@ namespace ts {
return undefined;
}
let parameters = isFunctionLike(commentOwner) ? commentOwner.parameters : emptyArray;
let parameters = getParametersForJsDocOwningNode(commentOwner);
let posLineAndChar = sourceFile.getLineAndCharacterOfPosition(position);
let lineStart = sourceFile.getLineStarts()[posLineAndChar.line];
@ -7096,6 +7096,52 @@ namespace ts {
return { newText: result, caretOffset: preamble.length };
}
function getParametersForJsDocOwningNode(commentOwner: Node): ParameterDeclaration[] {
if (isFunctionLike(commentOwner)) {
return commentOwner.parameters;
}
if (commentOwner.kind === SyntaxKind.VariableStatement) {
const varStatement = <VariableStatement>commentOwner;
const varDeclarations = varStatement.declarationList.declarations;
if (varDeclarations.length === 1 && varDeclarations[0].initializer) {
return getParametersFromRightHandSideOfAssignment(varDeclarations[0].initializer);
}
}
return emptyArray;
}
/**
* Digs into an an initializer or RHS operand of an assignment operation
* to get the parameters of an apt signature corresponding to a
* function expression or a class expression.
*
* @param rightHandSide the expression which may contain an appropriate set of parameters
* @returns the parameters of a signature found on the RHS if one exists; otherwise 'emptyArray'.
*/
function getParametersFromRightHandSideOfAssignment(rightHandSide: Expression): ParameterDeclaration[] {
while (rightHandSide.kind === SyntaxKind.ParenthesizedExpression) {
rightHandSide = (<ParenthesizedExpression>rightHandSide).expression;
}
switch (rightHandSide.kind) {
case SyntaxKind.FunctionExpression:
case SyntaxKind.ArrowFunction:
return (<FunctionExpression>rightHandSide).parameters;
case SyntaxKind.ClassExpression:
for (let member of (<ClassExpression>rightHandSide).members) {
if (member.kind === SyntaxKind.Constructor) {
return (<ConstructorDeclaration>member).parameters;
}
}
break;
}
return emptyArray;
}
function getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[] {
// Note: while getting todo comments seems like a syntactic operation, we actually
// treat it as a semantic operation here. This is because we expect our host to call

View file

@ -28,27 +28,43 @@ function confirmNormalizedJsDoc(markerName: string, newTextOffset: number, templ
////const c = 30;
////
/////*d*/
////let d = function d(x, y, z) {
//// return +(x + y + z);
////let d = {
//// foo: 10,
//// bar: "20"
////};
////
/////*e*/
////let e = class E {
////let e = function e(x, y, z) {
//// return +(x + y + z);
////};
////
/////*f*/
////let f = class F {
//// constructor(a, b, c) {
//// this.a = a;
//// this.b = b || (this.c = c);
//// }
////}
////
/////*f*/
////let f = {
//// foo: 10,
//// bar: "20"
////};
for (const varName of "abcdef".split("")) {
for (const varName of "abcd".split("")) {
confirmNormalizedJsDoc(varName, /*newTextOffset*/ 8, `
/**
*
*/`);
}
confirmNormalizedJsDoc("e", /*newTextOffset*/ 8, `
/**
*
* @param x
* @param y
* @param z
*/`);
confirmNormalizedJsDoc("f", /*newTextOffset*/ 8, `
/**
*
* @param a
* @param b
* @param c
*/`);

View file

@ -0,0 +1,90 @@
/// <reference path='fourslash.ts' />
const CRLF = "\r\n";
/**
* @returns the given value with '\n' normalized to '\r\n' and with no leading newline
*/
function useCRLFAndStripLeadingNewline(str: string): string {
str = str.replace(/\r?\n/g, CRLF);
if (str.indexOf(CRLF) === 0) {
str = str.slice(CRLF.length);
}
return str;
}
function confirmNormalizedJsDoc(markerName: string, newTextOffset: number, template: string): void {
goTo.marker(markerName);
const normalized = useCRLFAndStripLeadingNewline(template);
verify.DocCommentTemplate(normalized, newTextOffset);
}
/////*a*/
////var a = x => x
////
/////*b*/
////let b = (x,y,z) => x + y + z;
////
/////*c*/
////const c = ((x => +x))
////
/////*d*/
////let d = (function () { })
////
/////*e*/
////let e = function e([a,b,c]) {
//// return "hello"
////};
////
/////*f*/
////let f = class {
////}
////
/////*g*/
////const g = ((class G {
//// constructor(private x);
//// constructor(x,y,z);
//// constructor(x,y,z, ...okayThatsEnough) {
//// }
////}))
confirmNormalizedJsDoc("a", /*newTextOffset*/ 8, `
/**
*
* @param x
*/`);
confirmNormalizedJsDoc("b", /*newTextOffset*/ 8, `
/**
*
* @param x
* @param y
* @param z
*/`);
confirmNormalizedJsDoc("c", /*newTextOffset*/ 8, `
/**
*
* @param x
*/`);
confirmNormalizedJsDoc("d", /*newTextOffset*/ 8, `
/**
*
*/`);
confirmNormalizedJsDoc("e", /*newTextOffset*/ 8, `
/**
*
* @param param0
*/`);
confirmNormalizedJsDoc("f", /*newTextOffset*/ 8, `
/**
*
*/`);
confirmNormalizedJsDoc("g", /*newTextOffset*/ 8, `
/**
*
* @param x
*/`);