Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(angular): format files after migrations #13237

Merged
merged 1 commit into from Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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(),
]);
}
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
@@ -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);
}
@@ -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);
}