mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
refactor(migrations): make it easier to delete nodes including comments (#58427)
We were repeating the logic that deletes a node together with all its comments in a few different places. These changes consolidate the logic under `ChangeTracker.removeNode`. PR Close #58427
This commit is contained in:
parent
b5b0b4af29
commit
bb6e0aab1b
2 changed files with 10 additions and 12 deletions
|
|
@ -163,14 +163,14 @@ function migrateClass(
|
|||
for (const member of node.members) {
|
||||
if (ts.isConstructorDeclaration(member) && member !== constructor) {
|
||||
removedMembers.add(member);
|
||||
tracker.replaceText(sourceFile, member.getFullStart(), member.getFullWidth(), '');
|
||||
tracker.removeNode(member, true);
|
||||
}
|
||||
}
|
||||
|
||||
if (canRemoveConstructor(options, constructor, removedStatementCount, superCall)) {
|
||||
// Drop the constructor if it was empty.
|
||||
removedMembers.add(constructor);
|
||||
tracker.replaceText(sourceFile, constructor.getFullStart(), constructor.getFullWidth(), '');
|
||||
tracker.removeNode(constructor, true);
|
||||
} else {
|
||||
// If the constructor contains any statements, only remove the parameters.
|
||||
// We always do this no matter what is passed into `backwardsCompatibleConstructors`.
|
||||
|
|
@ -714,12 +714,7 @@ function applyInternalOnlyChanges(
|
|||
property.type,
|
||||
initializer,
|
||||
);
|
||||
tracker.replaceText(
|
||||
statement.getSourceFile(),
|
||||
statement.getFullStart(),
|
||||
statement.getFullWidth(),
|
||||
'',
|
||||
);
|
||||
tracker.removeNode(statement, true);
|
||||
tracker.replaceNode(property, newProperty);
|
||||
removedStatements.add(statement);
|
||||
});
|
||||
|
|
@ -728,7 +723,7 @@ function applyInternalOnlyChanges(
|
|||
prependToClass.push(
|
||||
memberIndentation + printer.printNode(ts.EmitHint.Unspecified, decl, decl.getSourceFile()),
|
||||
);
|
||||
tracker.replaceText(decl.getSourceFile(), decl.getFullStart(), decl.getFullWidth(), '');
|
||||
tracker.removeNode(decl, true);
|
||||
});
|
||||
|
||||
// If we added any hoisted properties, separate them visually with a new line.
|
||||
|
|
|
|||
|
|
@ -97,11 +97,14 @@ export class ChangeTracker {
|
|||
/**
|
||||
* Removes the text of an AST node from a file.
|
||||
* @param node Node whose text should be removed.
|
||||
* @param useFullOffsets Whether to remove the node using its full offset (e.g. `getFullStart`
|
||||
* rather than `fullStart`). This has the advantage of removing any comments that may be tied
|
||||
* to the node, but can lead to too much code being deleted.
|
||||
*/
|
||||
removeNode(node: ts.Node): void {
|
||||
removeNode(node: ts.Node, useFullOffsets = false): void {
|
||||
this._trackChange(node.getSourceFile(), {
|
||||
start: node.getStart(),
|
||||
removeLength: node.getWidth(),
|
||||
start: useFullOffsets ? node.getFullStart() : node.getStart(),
|
||||
removeLength: useFullOffsets ? node.getFullWidth() : node.getWidth(),
|
||||
text: '',
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue