Files
react-native-devtools-frontend/scripts/migration/class-fields/migrate.js
T
Tim van der Lippe 286bd5c44a Stop renaming properties in comments
This can break automation such as the localization collection script and
the license headers, as these are sensitive to specific references in
the comments themselves. It is probably safer to not change any of the
comments anyways.

R=jacktfranklin@chromium.org

Bug: 1222126
Change-Id: Ib5223559abc215dee0f4ac16b3a3269ee827702c
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3219238
Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
Reviewed-by: Jack Franklin <jacktfranklin@chromium.org>
2021-10-13 11:48:33 +00:00

68 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const {Project, ts, SyntaxKind, StructureKind} = require('ts-morph');
const path = require('path');
  const project = new Project({
tsConfigFilePath: '../devtools-frontend/tsconfig.json',
});
function isWithinContructor(classNode, reference) {
const constructors = classNode.getConstructors();
if (!constructors.length) {
return false;
}
if (constructors.length !== 1) {
throw new Error('More than one constructor');
}
return reference.getAncestors().includes(constructors[0]);
}
const files = project.getSourceFiles();
for (const file of files) {
const filePath = file.getFilePath();
if (!filePath.startsWith(
path.normalize(path.join(process.cwd(), '..', 'devtools-frontend', 'front_end', process.argv[2])))) {
continue;
}
console.log(filePath);
if (filePath.includes('third_party') || filePath.includes('generated') || filePath.endsWith('.d.ts')) {
continue;
}
const classNodes = file.getClasses();
for (const classNode of classNodes) {
for (const property of classNode.getInstanceProperties()) {
const name = property.getStructure().name;
console.log(name);
if (name.startsWith('#') || !property.hasModifier(SyntaxKind.PrivateKeyword)) {
continue;
}
if (isWithinContructor(classNode, property)) {
continue;
}
// https://github.com/dsherret/ts-morph/issues/1198
const newName = `SOME_STUPID_PREFIX_${name}`;
property.toggleModifier('private', false);
property.rename(newName);
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();
}
// project.save();