create functions for copying trailing comments and rename previous copyComment function

This commit is contained in:
Gabriela Araujo Britto 2019-02-11 12:01:02 -08:00
parent 7d86fda151
commit 26f8ddd46a
3 changed files with 49 additions and 8 deletions

View file

@ -35,7 +35,7 @@ namespace ts.codefix {
precedingNode = ctorDeclaration.parent.parent; precedingNode = ctorDeclaration.parent.parent;
newClassDeclaration = createClassFromVariableDeclaration(ctorDeclaration as VariableDeclaration); newClassDeclaration = createClassFromVariableDeclaration(ctorDeclaration as VariableDeclaration);
if ((<VariableDeclarationList>ctorDeclaration.parent).declarations.length === 1) { if ((<VariableDeclarationList>ctorDeclaration.parent).declarations.length === 1) {
copyComments(precedingNode, newClassDeclaration!, sourceFile); // TODO: GH#18217 copyLeadingComments(precedingNode, newClassDeclaration!, sourceFile); // TODO: GH#18217
changes.delete(sourceFile, precedingNode); changes.delete(sourceFile, precedingNode);
} }
else { else {
@ -48,7 +48,7 @@ namespace ts.codefix {
return undefined; return undefined;
} }
copyComments(ctorDeclaration, newClassDeclaration, sourceFile); copyLeadingComments(ctorDeclaration, newClassDeclaration, sourceFile);
// Because the preceding node could be touched, we need to insert nodes before delete nodes. // Because the preceding node could be touched, we need to insert nodes before delete nodes.
changes.insertNodeAfter(sourceFile, precedingNode!, newClassDeclaration); changes.insertNodeAfter(sourceFile, precedingNode!, newClassDeclaration);
@ -112,7 +112,7 @@ namespace ts.codefix {
const fullModifiers = concatenate(modifiers, getModifierKindFromSource(functionExpression, SyntaxKind.AsyncKeyword)); const fullModifiers = concatenate(modifiers, getModifierKindFromSource(functionExpression, SyntaxKind.AsyncKeyword));
const method = createMethod(/*decorators*/ undefined, fullModifiers, /*asteriskToken*/ undefined, memberDeclaration.name, /*questionToken*/ undefined, const method = createMethod(/*decorators*/ undefined, fullModifiers, /*asteriskToken*/ undefined, memberDeclaration.name, /*questionToken*/ undefined,
/*typeParameters*/ undefined, functionExpression.parameters, /*type*/ undefined, functionExpression.body); /*typeParameters*/ undefined, functionExpression.parameters, /*type*/ undefined, functionExpression.body);
copyComments(assignmentBinaryExpression, method, sourceFile); copyLeadingComments(assignmentBinaryExpression, method, sourceFile);
return method; return method;
} }
@ -132,7 +132,7 @@ namespace ts.codefix {
const fullModifiers = concatenate(modifiers, getModifierKindFromSource(arrowFunction, SyntaxKind.AsyncKeyword)); const fullModifiers = concatenate(modifiers, getModifierKindFromSource(arrowFunction, SyntaxKind.AsyncKeyword));
const method = createMethod(/*decorators*/ undefined, fullModifiers, /*asteriskToken*/ undefined, memberDeclaration.name, /*questionToken*/ undefined, const method = createMethod(/*decorators*/ undefined, fullModifiers, /*asteriskToken*/ undefined, memberDeclaration.name, /*questionToken*/ undefined,
/*typeParameters*/ undefined, arrowFunction.parameters, /*type*/ undefined, bodyBlock); /*typeParameters*/ undefined, arrowFunction.parameters, /*type*/ undefined, bodyBlock);
copyComments(assignmentBinaryExpression, method, sourceFile); copyLeadingComments(assignmentBinaryExpression, method, sourceFile);
return method; return method;
} }
@ -143,7 +143,7 @@ namespace ts.codefix {
} }
const prop = createProperty(/*decorators*/ undefined, modifiers, memberDeclaration.name, /*questionToken*/ undefined, const prop = createProperty(/*decorators*/ undefined, modifiers, memberDeclaration.name, /*questionToken*/ undefined,
/*type*/ undefined, assignmentBinaryExpression.right); /*type*/ undefined, assignmentBinaryExpression.right);
copyComments(assignmentBinaryExpression.parent, prop, sourceFile); copyLeadingComments(assignmentBinaryExpression.parent, prop, sourceFile);
return prop; return prop;
} }
} }

View file

@ -48,13 +48,13 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
const returnStatement = createReturn(expression); const returnStatement = createReturn(expression);
body = createBlock([returnStatement], /* multiLine */ true); body = createBlock([returnStatement], /* multiLine */ true);
suppressLeadingAndTrailingTrivia(body); suppressLeadingAndTrailingTrivia(body);
copyComments(expression!, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ true); copyLeadingComments(expression!, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ true);
} }
else if (actionName === removeBracesActionName && returnStatement) { else if (actionName === removeBracesActionName && returnStatement) {
const actualExpression = expression || createVoidZero(); const actualExpression = expression || createVoidZero();
body = needsParentheses(actualExpression) ? createParen(actualExpression) : actualExpression; body = needsParentheses(actualExpression) ? createParen(actualExpression) : actualExpression;
suppressLeadingAndTrailingTrivia(body); suppressLeadingAndTrailingTrivia(body);
copyComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false); copyLeadingComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false);
} }
else { else {
Debug.fail("invalid action"); Debug.fail("invalid action");

View file

@ -1831,7 +1831,7 @@ namespace ts {
return lastPos; return lastPos;
} }
export function copyComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile, commentKind?: CommentKind, hasTrailingNewLine?: boolean) { export function copyLeadingComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile, commentKind?: CommentKind, hasTrailingNewLine?: boolean) {
forEachLeadingCommentRange(sourceFile.text, sourceNode.pos, (pos, end, kind, htnl) => { forEachLeadingCommentRange(sourceFile.text, sourceNode.pos, (pos, end, kind, htnl) => {
if (kind === SyntaxKind.MultiLineCommentTrivia) { if (kind === SyntaxKind.MultiLineCommentTrivia) {
// Remove leading /* // Remove leading /*
@ -1847,6 +1847,47 @@ namespace ts {
}); });
} }
export function copyTrailingComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile, commentKind?: CommentKind, hasTrailingNewLine?: boolean) {
forEachTrailingCommentRange(sourceFile.text, sourceNode.end, (pos, end, kind, htnl) => {
if (kind === SyntaxKind.MultiLineCommentTrivia) {
// Remove leading /*
pos += 2;
// Remove trailing */
end -= 2;
}
else {
// Remove leading //
pos += 2;
}
addSyntheticTrailingComment(targetNode, commentKind || kind, sourceFile.text.slice(pos, end), hasTrailingNewLine !== undefined ? hasTrailingNewLine : htnl);
});
}
/**
* This function copies the trailing comments for the token that comes before `sourceNode`, as leading comments of `targetNode`.
* This is useful because sometimes a comment that refers to `sourceNode` will be a leading comment for `sourceNode`, according to the
* notion of trivia ownership, and instead will be a trailing comment for the token before `sourceNode`, e.g.:
* `function foo(\* not leading comment for a *\ a: string) {}`
* The comment refers to `a` but belongs to the `(` token, but we might want to copy it.
*/
export function copyTrailingAsLeadingComments(sourceNode: Node, targetNode: Node, sourceFile: SourceFile, commentKind?: CommentKind, hasTrailingNewLine?: boolean) {
forEachTrailingCommentRange(sourceFile.text, sourceNode.pos, (pos, end, kind, htnl) => {
if (kind === SyntaxKind.MultiLineCommentTrivia) {
// Remove leading /*
pos += 2;
// Remove trailing */
end -= 2;
}
else {
// Remove leading //
pos += 2;
}
addSyntheticLeadingComment(targetNode, commentKind || kind, sourceFile.text.slice(pos, end), hasTrailingNewLine !== undefined ? hasTrailingNewLine : htnl);
});
}
function indexInTextChange(change: string, name: string): number { function indexInTextChange(change: string, name: string): number {
if (startsWith(change, name)) return 0; if (startsWith(change, name)) return 0;
// Add a " " to avoid references inside words // Add a " " to avoid references inside words