Automatically replace delete statements for private class fields

Delete statements are disallowed for private class fields. Previously, I
used regexes to replace these, but we can implement it safely in the
migration script itself.

R=jacktfranklin@chromium.org

Bug: 1222126
Change-Id: Icab2cdaca290d40ea7d50710209cc84ea27208ba
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3219231
Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
Commit-Queue: Jack Franklin <jacktfranklin@chromium.org>
Auto-Submit: Tim van der Lippe <tvanderlippe@chromium.org>
Reviewed-by: Jack Franklin <jacktfranklin@chromium.org>
This commit is contained in:
Tim van der Lippe
2021-10-12 15:55:27 +00:00
committed by Devtools-frontend LUCI CQ
parent f68a12a5c9
commit 0404c7edc0
+15 -2
View File
@@ -44,11 +44,24 @@ for (const file of files) {
continue;
}
property.toggleModifier('private', false);
// https://github.com/dsherret/ts-morph/issues/1198
property.rename(`SOME_STUPID_PREFIX_${name}`, {
const newName = `SOME_STUPID_PREFIX_${name}`;
property.toggleModifier('private', false);
property.rename(newName, {
renameInComments: true,
});
for (const reference of property.findReferencesAsNodes()) {
// The first ancestor is the property access on `this.`.
const containingNode = reference.getAncestors()[1];
// Replace all `delete this.#somePrivateVariable;` (since that is illegal on private class fields)
// and replace it with an assignment to `undefined`.
if (containingNode.getKind() === SyntaxKind.DeleteExpression) {
console.log(`Replacing delete statement on line ${reference.getStartLineNumber()}`);
// We should replace the statement, not the expression itself.
containingNode.getFirstAncestor().replaceWithText(`this.${newName} = undefined;`);
}
}
}
}
file.saveSync();