Skip to content

Commit

Permalink
Revert "refactor(migrations): pass project-relative paths for tsurge …
Browse files Browse the repository at this point in the history
…replacements (angular#57584)"

This reverts commit 2ad9609.
  • Loading branch information
AndrewKushnir committed Aug 29, 2024
1 parent 2ad9609 commit 52b8420
Show file tree
Hide file tree
Showing 24 changed files with 74 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ async function main() {

process.stdout.write(JSON.stringify(mergedResult));
} else if (mode === 'migrate') {
const {replacements, projectDirAbsPath} = await executeMigratePhase(
const replacements = await executeMigratePhase(
migration,
JSON.parse(fs.readFileSync(path.resolve(args[1]), 'utf8')) as CompilationUnitData,
path.resolve(args[0]),
);

writeMigrationReplacements(replacements, projectDirAbsPath);
writeMigrationReplacements(replacements);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ export async function main(absoluteTsconfigPath: string, bestEffortMode: boolean
'Expected upgraded analysis phase results; batch mode is disabled.',
);

const {replacements, projectAbsDirPath} = migration.upgradedAnalysisPhaseResults;

// Apply replacements
writeMigrationReplacements(replacements, projectAbsDirPath);
writeMigrationReplacements(migration.upgradedAnalysisPhaseResults);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {AbsoluteFsPath, FileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {FileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {confirmAsSerializable, Serializable} from '../../../utils/tsurge/helpers/serializable';
import {BaseProgramInfo, ProgramInfo} from '../../../utils/tsurge/program_info';
import {TsurgeComplexMigration} from '../../../utils/tsurge/migration';
Expand Down Expand Up @@ -36,10 +36,7 @@ export class SignalInputMigration extends TsurgeComplexMigration<
CompilationUnitData
> {
upgradeAnalysisPhaseToAvoidBatch = false;
upgradedAnalysisPhaseResults: {
replacements: Replacement[];
projectAbsDirPath: AbsoluteFsPath;
} | null = null;
upgradedAnalysisPhaseResults: Replacement[] | null = null;

// Necessary for language service configuration.
reportProgressFn: ((percentage: number, updateMessage: string) => void) | null = null;
Expand Down Expand Up @@ -105,7 +102,7 @@ export class SignalInputMigration extends TsurgeComplexMigration<
this.reportProgressFn?.(100, 'Completed migration.');

// Expose the upgraded analysis stage results.
this.upgradedAnalysisPhaseResults = {replacements, projectAbsDirPath: info.projectDirAbsPath};
this.upgradedAnalysisPhaseResults = replacements;
}

return confirmAsSerializable(unitData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {ImportManager} from '@angular/compiler-cli/src/ngtsc/translator';
import ts from 'typescript';
import {applyImportManagerChanges} from '../../../../utils/tsurge/helpers/apply_import_manager';
import {MigrationResult} from '../result';
import {AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system';

/**
* Phase that applies all changes recorded by the import manager in
Expand All @@ -20,7 +19,6 @@ export function pass10_applyImportManager(
importManager: ImportManager,
result: MigrationResult,
sourceFiles: readonly ts.SourceFile[],
projectAbsPath: AbsoluteFsPath,
) {
applyImportManagerChanges(importManager, result.replacements, sourceFiles, projectAbsPath);
applyImportManagerChanges(importManager, result.replacements, sourceFiles);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import ts from 'typescript';
import {MigrationResult} from '../result';
import {analyzeControlFlow} from '../flow_analysis';
import {projectRelativePath, Replacement, TextUpdate} from '../../../../utils/tsurge/replacement';
import {Replacement, TextUpdate} from '../../../../utils/tsurge/replacement';
import {InputUniqueKey} from '../utils/input_id';
import {isTsInputReference} from '../utils/input_reference';
import {traverseAccess} from '../utils/traverse_access';
import {KnownInputs} from '../input_detection/known_inputs';
import {UniqueNamesGenerator} from '../utils/unique_names';
import {AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system';
import {absoluteFromSourceFile} from '@angular/compiler-cli/src/ngtsc/file_system';

/**
* Phase that migrates TypeScript input references to be signal compatible.
Expand Down Expand Up @@ -46,7 +46,6 @@ export function pass5__migrateTypeScriptReferences(
result: MigrationResult,
checker: ts.TypeChecker,
knownInputs: KnownInputs,
projectDirAbsPath: AbsoluteFsPath,
) {
const tsReferences = new Map<InputUniqueKey, {accesses: ts.Identifier[]}>();
const seenIdentifiers = new WeakSet<ts.Identifier>();
Expand Down Expand Up @@ -96,7 +95,7 @@ export function pass5__migrateTypeScriptReferences(
// Append `()` to unwrap the signal.
result.replacements.push(
new Replacement(
projectRelativePath(sf, projectDirAbsPath),
absoluteFromSourceFile(sf),
new TextUpdate({
position: originalNode.getEnd(),
end: originalNode.getEnd(),
Expand All @@ -113,7 +112,7 @@ export function pass5__migrateTypeScriptReferences(
const replaceNode = traverseAccess(originalNode);
result.replacements.push(
new Replacement(
projectRelativePath(sf, projectDirAbsPath),
absoluteFromSourceFile(sf),
new TextUpdate({
position: replaceNode.getStart(),
end: replaceNode.getEnd(),
Expand Down Expand Up @@ -148,7 +147,7 @@ export function pass5__migrateTypeScriptReferences(

result.replacements.push(
new Replacement(
projectRelativePath(sf, projectDirAbsPath),
absoluteFromSourceFile(sf),
new TextUpdate({
position: previous.getStart(),
end: previous.getStart(),
Expand All @@ -159,7 +158,7 @@ export function pass5__migrateTypeScriptReferences(

result.replacements.push(
new Replacement(
projectRelativePath(sf, projectDirAbsPath),
absoluteFromSourceFile(sf),
new TextUpdate({
position: replaceNode.getStart(),
end: replaceNode.getEnd(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {convertToSignalInput} from '../convert-input/convert_to_signal';
import assert from 'assert';
import {KnownInputs} from '../input_detection/known_inputs';
import {ImportManager} from '@angular/compiler-cli/src/ngtsc/translator';
import {projectRelativePath, Replacement, TextUpdate} from '../../../../utils/tsurge/replacement';
import {AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system';
import {Replacement, TextUpdate} from '../../../../utils/tsurge/replacement';
import {absoluteFromSourceFile} from '@angular/compiler-cli/src/ngtsc/file_system';

/**
* Phase that migrates `@Input()` declarations to signal inputs and
Expand All @@ -24,7 +24,6 @@ export function pass6__migrateInputDeclarations(
result: MigrationResult,
knownInputs: KnownInputs,
importManager: ImportManager,
projectDirAbsPath: AbsoluteFsPath,
) {
let filesWithMigratedInputs = new Set<ts.SourceFile>();
let filesWithIncompatibleInputs = new WeakSet<ts.SourceFile>();
Expand All @@ -43,7 +42,7 @@ export function pass6__migrateInputDeclarations(
filesWithMigratedInputs.add(sf);
result.replacements.push(
new Replacement(
projectRelativePath(sf, projectDirAbsPath),
absoluteFromSourceFile(sf),
new TextUpdate({
position: input.node.getStart(),
end: input.node.getEnd(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
*/

import {MigrationHost} from '../migration_host';
import {AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system';
import {absoluteFrom} from '@angular/compiler-cli/src/ngtsc/file_system';
import {MigrationResult} from '../result';
import {isTemplateInputReference} from '../utils/input_reference';
import {KnownInputs} from '../input_detection/known_inputs';
import {projectRelativePath, Replacement, TextUpdate} from '../../../../utils/tsurge/replacement';
import {Replacement, TextUpdate} from '../../../../utils/tsurge/replacement';

/**
* Phase that migrates Angular template references to
Expand All @@ -21,7 +21,6 @@ export function pass7__migrateTemplateReferences(
host: MigrationHost,
result: MigrationResult,
knownInputs: KnownInputs,
projectDirAbsPath: AbsoluteFsPath,
) {
const seenFileReferences = new Set<string>();

Expand Down Expand Up @@ -49,7 +48,7 @@ export function pass7__migrateTemplateReferences(

result.replacements.push(
new Replacement(
projectRelativePath(host.idToFilePath(reference.from.templateFileId), projectDirAbsPath),
absoluteFrom(host.idToFilePath(reference.from.templateFileId)),
new TextUpdate({
position: reference.from.read.sourceSpan.end,
end: reference.from.read.sourceSpan.end,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*/

import ts from 'typescript';
import {AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system';
import {projectRelativePath, Replacement, TextUpdate} from '../../../../utils/tsurge/replacement';
import {absoluteFromSourceFile} from '@angular/compiler-cli/src/ngtsc/file_system';
import {Replacement, TextUpdate} from '../../../../utils/tsurge/replacement';
import {MigrationResult} from '../result';
import {isHostBindingInputReference} from '../utils/input_reference';
import {KnownInputs} from '../input_detection/known_inputs';
Expand All @@ -17,11 +17,7 @@ import {KnownInputs} from '../input_detection/known_inputs';
* Phase that migrates Angular host binding references to
* unwrap signals.
*/
export function pass8__migrateHostBindings(
result: MigrationResult,
knownInputs: KnownInputs,
projectDirAbsPath: AbsoluteFsPath,
) {
export function pass8__migrateHostBindings(result: MigrationResult, knownInputs: KnownInputs) {
const seenReferences = new WeakMap<ts.Node, Set<number>>();

for (const reference of result.references) {
Expand Down Expand Up @@ -55,7 +51,7 @@ export function pass8__migrateHostBindings(

result.replacements.push(
new Replacement(
projectRelativePath(bindingField.getSourceFile(), projectDirAbsPath),
absoluteFromSourceFile(bindingField.getSourceFile()),
new TextUpdate({position: readEndPos, end: readEndPos, toInsert: appendText}),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import ts from 'typescript';
import {KnownInputs} from '../input_detection/known_inputs';
import {MigrationResult} from '../result';
import {isTsInputClassTypeReference} from '../utils/input_reference';
import {AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system';
import {projectRelativePath, Replacement, TextUpdate} from '../../../../utils/tsurge/replacement';
import {absoluteFromSourceFile} from '@angular/compiler-cli/src/ngtsc/file_system';
import {Replacement, TextUpdate} from '../../../../utils/tsurge/replacement';
import assert from 'assert';
import {ImportManager} from '@angular/compiler-cli/src/ngtsc/translator';

Expand All @@ -25,7 +25,6 @@ export function pass9__migrateTypeScriptTypeReferences(
result: MigrationResult,
knownInputs: KnownInputs,
importManager: ImportManager,
projectDirAbsPath: AbsoluteFsPath,
) {
const seenTypeNodes = new WeakSet<ts.TypeReferenceNode>();

Expand Down Expand Up @@ -59,7 +58,7 @@ export function pass9__migrateTypeScriptTypeReferences(

result.replacements.push(
new Replacement(
projectRelativePath(sf, projectDirAbsPath),
absoluteFromSourceFile(sf),
new TextUpdate({
position: firstArg.getStart(),
end: firstArg.getStart(),
Expand All @@ -69,7 +68,7 @@ export function pass9__migrateTypeScriptTypeReferences(
);
result.replacements.push(
new Replacement(
projectRelativePath(sf, projectDirAbsPath),
absoluteFromSourceFile(sf),
new TextUpdate({position: firstArg.getEnd(), end: firstArg.getEnd(), toInsert: '>'}),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function executeMigrationPhase(
host: MigrationHost,
knownInputs: KnownInputs,
result: MigrationResult,
{typeChecker, sourceFiles, projectDirAbsPath}: AnalysisProgramInfo,
{typeChecker, sourceFiles}: AnalysisProgramInfo,
) {
const importManager = new ImportManager({
// For the purpose of this migration, we always use `input` and don't alias
Expand All @@ -40,16 +40,10 @@ export function executeMigrationPhase(
});

// Migrate passes.
pass5__migrateTypeScriptReferences(result, typeChecker, knownInputs, projectDirAbsPath);
pass6__migrateInputDeclarations(
typeChecker,
result,
knownInputs,
importManager,
projectDirAbsPath,
);
pass7__migrateTemplateReferences(host, result, knownInputs, projectDirAbsPath);
pass8__migrateHostBindings(result, knownInputs, projectDirAbsPath);
pass9__migrateTypeScriptTypeReferences(result, knownInputs, importManager, projectDirAbsPath);
pass10_applyImportManager(importManager, result, sourceFiles, projectDirAbsPath);
pass5__migrateTypeScriptReferences(result, typeChecker, knownInputs);
pass6__migrateInputDeclarations(typeChecker, result, knownInputs, importManager);
pass7__migrateTemplateReferences(host, result, knownInputs);
pass8__migrateHostBindings(result, knownInputs);
pass9__migrateTypeScriptTypeReferences(result, knownInputs, importManager);
pass10_applyImportManager(importManager, result, sourceFiles);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,16 @@
* found in the LICENSE file at https://angular.io/license
*/

import fs from 'fs';
import {applyTextUpdates, Replacement} from '../../../utils/tsurge/replacement';
import {groupReplacementsByFile} from '../../../utils/tsurge/helpers/group_replacements';
import {AbsoluteFsPath, getFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';

/** Applies the migration result and applies it to the file system. */
export function writeMigrationReplacements(
replacements: Replacement[],
projectDirAbsPath: AbsoluteFsPath,
) {
const fs = getFileSystem();

for (const [projectRelativePath, updates] of groupReplacementsByFile(replacements)) {
const filePath = fs.join(projectDirAbsPath, projectRelativePath);
const fileText = fs.readFile(filePath);
export function writeMigrationReplacements(replacements: Replacement[]) {
for (const [filePath, updates] of groupReplacementsByFile(replacements)) {
const fileText = fs.readFileSync(filePath, 'utf8')!;
const newText = applyTextUpdates(fileText, updates);

fs.writeFile(filePath, newText);
fs.writeFileSync(filePath, newText, 'utf8');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ ts_library(
"//packages/compiler-cli/private",
"//packages/compiler-cli/src/ngtsc/annotations",
"//packages/compiler-cli/src/ngtsc/annotations/directive",
"//packages/compiler-cli/src/ngtsc/file_system",
"//packages/compiler-cli/src/ngtsc/reflection",
"//packages/core/schematics/migrations/signal-migration/src",
"//packages/core/schematics/utils/tsurge",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import ts from 'typescript';
import {ExtractedQuery} from './identify_queries';
import {projectRelativePath, Replacement, TextUpdate} from '../../utils/tsurge';
import {AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system';
import {Replacement, TextUpdate} from '../../utils/tsurge';
import {absoluteFromSourceFile} from '../../../../compiler-cli';
import {ImportManager} from '@angular/compiler-cli/private/migrations';
import assert from 'assert';
import {WrappedNodeExpr} from '@angular/compiler';
Expand Down Expand Up @@ -43,7 +43,6 @@ export function computeReplacementsToMigrateQuery(
node: ts.PropertyDeclaration,
metadata: ExtractedQuery,
importManager: ImportManager,
projectDirAbsPath: AbsoluteFsPath,
): Replacement[] {
const sf = node.getSourceFile();
let newQueryFn = importManager.addImport({
Expand Down Expand Up @@ -134,7 +133,7 @@ export function computeReplacementsToMigrateQuery(

return [
new Replacement(
projectRelativePath(node.getSourceFile(), projectDirAbsPath),
absoluteFromSourceFile(node.getSourceFile()),
new TextUpdate({
position: node.getStart(),
end: node.getEnd(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
* found in the LICENSE file at https://angular.io/license
*/

import {absoluteFromSourceFile} from '@angular/compiler-cli';
import {TypeScriptReflectionHost} from '@angular/compiler-cli/src/ngtsc/reflection';
import ts from 'typescript';
import {
confirmAsSerializable,
ProgramInfo,
projectRelativePath,
Replacement,
Serializable,
TextUpdate,
Expand Down Expand Up @@ -140,7 +140,6 @@ export class SignalQueriesMigration extends TsurgeComplexMigration<
node as ts.PropertyDeclaration,
extractedQuery,
importManager,
projectDirAbsPath,
),
);
return;
Expand All @@ -152,7 +151,7 @@ export class SignalQueriesMigration extends TsurgeComplexMigration<
if (targetId !== null && isMigratedQuery(targetId)) {
replacements.push(
new Replacement(
projectRelativePath(node.getSourceFile(), projectDirAbsPath),
absoluteFromSourceFile(node.getSourceFile()),
new TextUpdate({position: node.getEnd(), end: node.getEnd(), toInsert: '()'}),
),
);
Expand All @@ -174,7 +173,7 @@ export class SignalQueriesMigration extends TsurgeComplexMigration<
}
}

applyImportManagerChanges(importManager, replacements, sourceFiles, projectDirAbsPath);
applyImportManagerChanges(importManager, replacements, sourceFiles);

return replacements;
}
Expand Down
Loading

0 comments on commit 52b8420

Please sign in to comment.