mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-16 15:45:27 -05:00
Don't delete comments when deleting unused declarations (#37467)
* don't delete comment on variable declaration * add more declaration kinds * don't copy comment in convertes6 class * don't copy comments in convertToES6Class * add tests * use isAnyImportSyntax * handle mixed comment types * update tests
This commit is contained in:
@@ -48,7 +48,10 @@ namespace ts.codefix {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
copyLeadingComments(ctorDeclaration, newClassDeclaration, sourceFile);
|
||||
// Deleting a declaration only deletes JSDoc style comments, so only copy those to the new node.
|
||||
if (hasJSDocNodes(ctorDeclaration)) {
|
||||
copyLeadingComments(ctorDeclaration, newClassDeclaration, sourceFile);
|
||||
}
|
||||
|
||||
// Because the preceding node could be touched, we need to insert nodes before delete nodes.
|
||||
changes.insertNodeAfter(sourceFile, precedingNode!, newClassDeclaration);
|
||||
|
||||
@@ -42,6 +42,15 @@ namespace ts.textChanges {
|
||||
* include all trivia between the node and the previous token
|
||||
*/
|
||||
IncludeAll,
|
||||
/**
|
||||
* Include attached JSDoc comments
|
||||
*/
|
||||
JSDoc,
|
||||
/**
|
||||
* Only delete trivia on the same line as getStart().
|
||||
* Used to avoid deleting leading comments
|
||||
*/
|
||||
StartLine,
|
||||
}
|
||||
|
||||
export enum TrailingTriviaOption {
|
||||
@@ -162,6 +171,15 @@ namespace ts.textChanges {
|
||||
if (leadingTriviaOption === LeadingTriviaOption.Exclude) {
|
||||
return node.getStart(sourceFile);
|
||||
}
|
||||
if (leadingTriviaOption === LeadingTriviaOption.StartLine) {
|
||||
return getLineStartPositionForPosition(node.getStart(sourceFile), sourceFile);
|
||||
}
|
||||
if (leadingTriviaOption === LeadingTriviaOption.JSDoc) {
|
||||
const JSDocComments = getJSDocCommentRanges(node, sourceFile.text);
|
||||
if (JSDocComments?.length) {
|
||||
return getLineStartPositionForPosition(JSDocComments[0].pos, sourceFile);
|
||||
}
|
||||
}
|
||||
const fullStart = node.getFullStart();
|
||||
const start = node.getStart(sourceFile);
|
||||
if (fullStart === start) {
|
||||
@@ -1249,10 +1267,11 @@ namespace ts.textChanges {
|
||||
}
|
||||
|
||||
case SyntaxKind.ImportDeclaration:
|
||||
const isFirstImport = sourceFile.imports.length && node === first(sourceFile.imports).parent || node === find(sourceFile.statements, isImportDeclaration);
|
||||
case SyntaxKind.ImportEqualsDeclaration:
|
||||
const isFirstImport = sourceFile.imports.length && node === first(sourceFile.imports).parent || node === find(sourceFile.statements, isAnyImportSyntax);
|
||||
// For first import, leave header comment in place, otherwise only delete JSDoc comments
|
||||
deleteNode(changes, sourceFile, node,
|
||||
// For first import, leave header comment in place
|
||||
isFirstImport ? { leadingTriviaOption: LeadingTriviaOption.Exclude } : undefined);
|
||||
{ leadingTriviaOption: isFirstImport ? LeadingTriviaOption.Exclude : hasJSDocNodes(node) ? LeadingTriviaOption.JSDoc : LeadingTriviaOption.StartLine });
|
||||
break;
|
||||
|
||||
case SyntaxKind.BindingElement:
|
||||
@@ -1296,6 +1315,11 @@ namespace ts.textChanges {
|
||||
deleteNode(changes, sourceFile, node, { leadingTriviaOption: LeadingTriviaOption.Exclude });
|
||||
break;
|
||||
|
||||
case SyntaxKind.ClassDeclaration:
|
||||
case SyntaxKind.FunctionDeclaration:
|
||||
deleteNode(changes, sourceFile, node, { leadingTriviaOption: hasJSDocNodes(node) ? LeadingTriviaOption.JSDoc : LeadingTriviaOption.StartLine });
|
||||
break;
|
||||
|
||||
default:
|
||||
if (isImportClause(node.parent) && node.parent.name === node) {
|
||||
deleteDefaultImport(changes, sourceFile, node.parent);
|
||||
@@ -1372,7 +1396,7 @@ namespace ts.textChanges {
|
||||
break;
|
||||
|
||||
case SyntaxKind.VariableStatement:
|
||||
deleteNode(changes, sourceFile, gp);
|
||||
deleteNode(changes, sourceFile, gp, { leadingTriviaOption: hasJSDocNodes(gp) ? LeadingTriviaOption.JSDoc : LeadingTriviaOption.StartLine });
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user