Skip to content

Commit

Permalink
fix(angular): format files after migrations (#13237)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Nov 17, 2022
1 parent 204e64a commit 7554dda
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
* found in the LICENSE file at https://angular.io/license
*/

import { DirEntry, Rule, UpdateRecorder } from '@angular-devkit/schematics';
import {
chain,
DirEntry,
Rule,
UpdateRecorder,
} from '@angular-devkit/schematics';
import * as ts from 'typescript';
import { formatFiles } from '@nrwl/workspace';

function* visit(directory: DirEntry): IterableIterator<ts.SourceFile> {
for (const path of directory.subfiles) {
Expand Down Expand Up @@ -42,68 +48,71 @@ function* visit(directory: DirEntry): IterableIterator<ts.SourceFile> {
}

export default function (): Rule {
return (tree) => {
for (const sourceFile of visit(tree.root)) {
let recorder: UpdateRecorder | undefined;
let printer: ts.Printer | undefined;
return chain([
(tree) => {
for (const sourceFile of visit(tree.root)) {
let recorder: UpdateRecorder | undefined;
let printer: ts.Printer | undefined;

ts.forEachChild(sourceFile, function analyze(node) {
if (
!(
ts.isExportDeclaration(node) &&
node.moduleSpecifier &&
ts.isStringLiteral(node.moduleSpecifier) &&
node.moduleSpecifier.text === '@angular/platform-server' &&
node.exportClause &&
ts.isNamedExports(node.exportClause)
)
) {
// Not a @angular/platform-server named export.
return;
}
ts.forEachChild(sourceFile, function analyze(node) {
if (
!(
ts.isExportDeclaration(node) &&
node.moduleSpecifier &&
ts.isStringLiteral(node.moduleSpecifier) &&
node.moduleSpecifier.text === '@angular/platform-server' &&
node.exportClause &&
ts.isNamedExports(node.exportClause)
)
) {
// Not a @angular/platform-server named export.
return;
}

const exportClause = node.exportClause;
const newElements: ts.ExportSpecifier[] = [];
for (const element of exportClause.elements) {
if (element.name.text !== 'renderModule') {
newElements.push(element);
const exportClause = node.exportClause;
const newElements: ts.ExportSpecifier[] = [];
for (const element of exportClause.elements) {
if (element.name.text !== 'renderModule') {
newElements.push(element);
}
}
}

if (newElements.length === exportClause.elements.length) {
// No changes
return;
}
if (newElements.length === exportClause.elements.length) {
// No changes
return;
}

recorder ??= tree.beginUpdate(sourceFile.fileName);
recorder ??= tree.beginUpdate(sourceFile.fileName);

if (newElements.length) {
// Update named exports as there are leftovers.
const newExportClause = ts.factory.updateNamedExports(
exportClause,
newElements
);
printer ??= ts.createPrinter();
const fix = printer.printNode(
ts.EmitHint.Unspecified,
newExportClause,
sourceFile
);
if (newElements.length) {
// Update named exports as there are leftovers.
const newExportClause = ts.factory.updateNamedExports(
exportClause,
newElements
);
printer ??= ts.createPrinter();
const fix = printer.printNode(
ts.EmitHint.Unspecified,
newExportClause,
sourceFile
);

const index = exportClause.getStart();
const length = exportClause.getWidth();
recorder.remove(index, length).insertLeft(index, fix);
} else {
// Delete export as no exports remain.
recorder.remove(node.getStart(), node.getWidth());
}
const index = exportClause.getStart();
const length = exportClause.getWidth();
recorder.remove(index, length).insertLeft(index, fix);
} else {
// Delete export as no exports remain.
recorder.remove(node.getStart(), node.getWidth());
}

ts.forEachChild(node, analyze);
});
ts.forEachChild(node, analyze);
});

if (recorder) {
tree.commitUpdate(recorder);
if (recorder) {
tree.commitUpdate(recorder);
}
}
}
};
},
formatFiles(),
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@
* found in the LICENSE file at https://angular.io/license
*/

import { Rule, Tree } from '@angular-devkit/schematics';
import { chain, Rule, Tree } from '@angular-devkit/schematics';
import * as ts from 'typescript';
import { readWorkspace } from '@schematics/angular/utility';
import { Builders } from '@schematics/angular/utility/workspace-models';
import { allTargetOptions } from '@schematics/angular/utility/workspace';
import { formatFiles } from '@nrwl/workspace';

export default function (): Rule {
return async (host) => {
for (const file of await findTestMainFiles(host)) {
updateTestFile(host, file);
}
};
return chain([
async (host) => {
for (const file of await findTestMainFiles(host)) {
updateTestFile(host, file);
}
},
formatFiles(),
]);
}

async function findTestMainFiles(host: Tree): Promise<Set<string>> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Tree } from '@nrwl/devkit';
import { getProjects, updateJson } from '@nrwl/devkit';
import { formatFiles, getProjects, updateJson } from '@nrwl/devkit';
import { Builders } from '@schematics/angular/utility/workspace-models';

function updateTarget(tree: Tree, tsconfigPath: string) {
Expand Down Expand Up @@ -50,4 +50,6 @@ export default async function updateTypescriptTarget(tree: Tree) {
}
}
}

await formatFiles(tree);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { Tree } from '@nrwl/devkit';
import { getProjects, updateProjectConfiguration } from '@nrwl/devkit';
import {
formatFiles,
getProjects,
updateProjectConfiguration,
} from '@nrwl/devkit';
import { Builders } from '@schematics/angular/utility/workspace-models';

export default function updateWorkspaceConfigurations(tree: Tree) {
export default async function updateWorkspaceConfigurations(tree: Tree) {
const projects = getProjects(tree);

const supportedExecutors: Set<string> = new Set([Builders.Server]);
Expand All @@ -25,4 +29,6 @@ export default function updateWorkspaceConfigurations(tree: Tree) {
updateProjectConfiguration(tree, name, project);
}
}

await formatFiles(tree);
}

0 comments on commit 7554dda

Please sign in to comment.