Skip to content

Commit ec03e46

Browse files
refactor: migrate bazel, benchpress, elements and misc to prettier formatting (angular#53995)
Migrate formatting to prettier for bazel, benchpress, elements and misc from clang-format PR Close angular#53995
1 parent bbbe477 commit ec03e46

File tree

90 files changed

+3865
-2950
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+3865
-2950
lines changed

.ng-dev/format.mts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export const format: FormatConfig = {
1212
'modules/**/*.{js,ts}',
1313
'scripts/**/*.{js,ts}',
1414
'packages/animations/**/*.{js,ts}',
15+
'packages/bazel/**/*.{js,ts}',
16+
'packages/benchpress/**/*.{js,ts}',
17+
'packages/elements/**/*.{js,ts}',
18+
'packages/misc/**/*.{js,ts}',
1519
],
1620
},
1721
'clang-format': {
@@ -45,6 +49,10 @@ export const format: FormatConfig = {
4549
'!modules/**/*.{js,ts}',
4650
'!scripts/**/*.{js,ts}',
4751
'!packages/animations/**/*.{js,ts}',
52+
'!packages/bazel/**/*.{js,ts}',
53+
'!packages/benchpress/**/*.{js,ts}',
54+
'!packages/elements/**/*.{js,ts}',
55+
'!packages/misc/**/*.{js,ts}',
4856
],
4957
},
5058
'buildifier': true,

packages/bazel/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ pkg_npm(
1010
) + [
1111
"//packages/bazel/src:package_assets",
1212
"//packages/bazel/src/ng_module:package_assets",
13-
"//packages/bazel/src/types_bundle:package_assets",
1413
"//packages/bazel/src/ng_package:package_assets",
1514
"//packages/bazel/src/ngc-wrapped:package_assets",
15+
"//packages/bazel/src/types_bundle:package_assets",
1616
"//packages/bazel/third_party/github.com/bazelbuild/bazel/src/main/protobuf:package_assets",
1717
],
1818
substitutions = {

packages/bazel/src/ng_package/cross_entry_points_imports.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ const skipComment = '// @ng_package: ignore-cross-repo-import';
3434
* forbidden (unless explicitly opted out via comment - {@link skipComment}).
3535
*/
3636
export function analyzeFileAndEnsureNoCrossImports(
37-
file: BazelFileInfo, pkg: PackageMetadata): string[] {
37+
file: BazelFileInfo,
38+
pkg: PackageMetadata,
39+
): string[] {
3840
const content = fs.readFileSync(file.path, 'utf8');
3941
const sf = ts.createSourceFile(file.path, content, ts.ScriptTarget.Latest, true);
4042
const fileDirPath = path.posix.dirname(file.path);
@@ -57,26 +59,28 @@ export function analyzeFileAndEnsureNoCrossImports(
5759
}
5860
// Skip this import if there is an explicit skip comment.
5961
const leadingComments = ts.getLeadingCommentRanges(sf.text, st.getFullStart());
60-
if (leadingComments !== undefined &&
61-
leadingComments.some(c => sf.text.substring(c.pos, c.end) === skipComment)) {
62+
if (
63+
leadingComments !== undefined &&
64+
leadingComments.some((c) => sf.text.substring(c.pos, c.end) === skipComment)
65+
) {
6266
continue;
6367
}
6468

6569
const destinationPath = path.posix.join(fileDirPath, st.moduleSpecifier.text);
6670
const targetPackage = determineOwningEntryPoint({path: destinationPath}, pkg);
6771
if (targetPackage === null) {
6872
failures.push(
69-
`Could not determine owning entry-point package of: ${destinationPath}. Imported from: ${
70-
fileDebugName}. Is this a relative import to another full package?.\n` +
71-
`You can skip this import by adding a comment: ${skipComment}`);
73+
`Could not determine owning entry-point package of: ${destinationPath}. Imported from: ${fileDebugName}. Is this a relative import to another full package?.\n` +
74+
`You can skip this import by adding a comment: ${skipComment}`,
75+
);
7276
continue;
7377
}
7478

7579
if (targetPackage.path !== owningPkg.path) {
7680
failures.push(
77-
`Found relative cross entry-point import in: ${fileDebugName}. Import to: ${
78-
st.moduleSpecifier.text}\n` +
79-
`You can skip this import by adding a comment: ${skipComment}`);
81+
`Found relative cross entry-point import in: ${fileDebugName}. Import to: ${st.moduleSpecifier.text}\n` +
82+
`You can skip this import by adding a comment: ${skipComment}`,
83+
);
8084
}
8185
}
8286

@@ -85,8 +89,10 @@ export function analyzeFileAndEnsureNoCrossImports(
8589

8690
/** Determines the owning entry-point for the given JavaScript file. */
8791
function determineOwningEntryPoint(
88-
file: Pick<BazelFileInfo, 'path'>, pkg: PackageMetadata): EntryPointPackage|null {
89-
let owningEntryPoint: EntryPointPackage|null = null;
92+
file: Pick<BazelFileInfo, 'path'>,
93+
pkg: PackageMetadata,
94+
): EntryPointPackage | null {
95+
let owningEntryPoint: EntryPointPackage | null = null;
9096

9197
for (const [name, info] of Object.entries(pkg.entryPoints)) {
9298
// Entry point directory is assumed because technically the entry-point
@@ -96,8 +102,10 @@ function determineOwningEntryPoint(
96102
// the entry-point.
97103
const assumedEntryPointDir = path.posix.dirname(info.index.path);
98104

99-
if (file.path.startsWith(assumedEntryPointDir) &&
100-
(owningEntryPoint === null || owningEntryPoint.path.length < assumedEntryPointDir.length)) {
105+
if (
106+
file.path.startsWith(assumedEntryPointDir) &&
107+
(owningEntryPoint === null || owningEntryPoint.path.length < assumedEntryPointDir.length)
108+
) {
101109
owningEntryPoint = {name, info, path: assumedEntryPointDir};
102110
}
103111
}

packages/bazel/src/ng_package/packager.ts

Lines changed: 70 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,10 @@ import {analyzeFileAndEnsureNoCrossImports} from './cross_entry_points_imports';
1616
* List of known `package.json` fields which provide information about
1717
* supported package formats and their associated entry paths.
1818
*/
19-
const knownFormatPackageJsonFormatFields = [
20-
'main',
21-
'esm2022',
22-
'esm',
23-
'typings',
24-
'module',
25-
] as const;
19+
const knownFormatPackageJsonFormatFields = ['main', 'esm2022', 'esm', 'typings', 'module'] as const;
2620

2721
/** Union type matching known `package.json` format fields. */
28-
type KnownPackageJsonFormatFields = typeof knownFormatPackageJsonFormatFields[number];
22+
type KnownPackageJsonFormatFields = (typeof knownFormatPackageJsonFormatFields)[number];
2923

3024
/**
3125
* Type describing the conditional exports descriptor for an entry-point.
@@ -41,7 +35,7 @@ type ConditionalExport = {
4135
/** Type describing a `package.json` the packager deals with. */
4236
type PackageJson = {
4337
[key in KnownPackageJsonFormatFields]?: string;
44-
}&{
38+
} & {
4539
name: string;
4640
type?: string;
4741
exports?: Record<string, ConditionalExport>;
@@ -64,29 +58,29 @@ function main(args: string[]): void {
6458
const params = fs.readFileSync(paramFilePath, 'utf-8').split('\n').map(unquoteParameter);
6559

6660
const [
67-
// Output directory for the npm package.
68-
outputDirExecPath,
61+
// Output directory for the npm package.
62+
outputDirExecPath,
6963

70-
// The package segment of the ng_package rule's label (e.g. 'package/common').
71-
owningPackageName,
64+
// The package segment of the ng_package rule's label (e.g. 'package/common').
65+
owningPackageName,
7266

73-
// JSON data capturing metadata of the package being built. See `PackageMetadata`.
74-
metadataArg,
67+
// JSON data capturing metadata of the package being built. See `PackageMetadata`.
68+
metadataArg,
7569

76-
// Path to the package's README.md.
77-
readmeMd,
70+
// Path to the package's README.md.
71+
readmeMd,
7872

79-
// List of rolled-up flat ES2022 modules
80-
fesm2022Arg,
73+
// List of rolled-up flat ES2022 modules
74+
fesm2022Arg,
8175

82-
// List of individual ES2022 modules
83-
esm2022Arg,
76+
// List of individual ES2022 modules
77+
esm2022Arg,
8478

85-
// List of static files that should be copied into the package.
86-
staticFilesArg,
79+
// List of static files that should be copied into the package.
80+
staticFilesArg,
8781

88-
// List of all type definitions that need to packaged into the ng_package.
89-
typeDefinitionsArg,
82+
// List of all type definitions that need to packaged into the ng_package.
83+
typeDefinitionsArg,
9084
] = params;
9185

9286
const fesm2022 = JSON.parse(fesm2022Arg) as BazelFileInfo[];
@@ -105,7 +99,7 @@ function main(args: string[]): void {
10599
* file is written to.
106100
* @param fileContent Content of the file.
107101
*/
108-
function writeFile(outputRelativePath: string, fileContent: string|Buffer) {
102+
function writeFile(outputRelativePath: string, fileContent: string | Buffer) {
109103
const outputPath = path.join(outputDirExecPath, outputRelativePath);
110104

111105
// Always ensure that the target directory exists.
@@ -192,7 +186,7 @@ function main(args: string[]): void {
192186

193187
const crossEntryPointFailures: string[] = [];
194188

195-
esm2022.forEach(file => {
189+
esm2022.forEach((file) => {
196190
crossEntryPointFailures.push(...analyzeFileAndEnsureNoCrossImports(file, metadata));
197191
writeEsm2022File(file);
198192
});
@@ -203,12 +197,12 @@ function main(args: string[]): void {
203197
}
204198

205199
// Copy all FESM files into the package output.
206-
fesm2022.forEach(f => copyFile(f.path, getFlatEsmOutputRelativePath(f)));
200+
fesm2022.forEach((f) => copyFile(f.path, getFlatEsmOutputRelativePath(f)));
207201

208202
// Copy all type definitions into the package, preserving the sub-path from the
209203
// owning package. e.g. a file like `packages/animations/browser/__index.d.ts` will
210204
// end up in `browser/index.d.ts`
211-
typeDefinitions.forEach(f => copyFile(f.path, getTypingOutputRelativePath(f)));
205+
typeDefinitions.forEach((f) => copyFile(f.path, getTypingOutputRelativePath(f)));
212206

213207
for (const file of staticFiles) {
214208
// We copy all files into the package output while preserving the sub-path from
@@ -226,20 +220,25 @@ function main(args: string[]): void {
226220
// Resolution in the package should only be based on the top-level `package.json`.
227221
if (!isPrimaryPackageJson) {
228222
throw Error(
229-
`Found a nested "package.json" file in the package output: ${file.shortPath}.\n` +
230-
`All information of the package should reside in the primary package file.`);
223+
`Found a nested "package.json" file in the package output: ${file.shortPath}.\n` +
224+
`All information of the package should reside in the primary package file.`,
225+
);
231226
}
232227

233228
// Check if the `name` field of the `package.json` files are matching with
234229
// name of the NPM package. This is an additional safety check.
235230
if (packageName !== metadata.npmPackageName) {
236231
throw Error(
237-
`Primary "package.json" has mismatching package name. Expected the ` +
238-
`package to be named "${metadata.npmPackageName}", but is set to: ${packageName}.`);
232+
`Primary "package.json" has mismatching package name. Expected the ` +
233+
`package to be named "${metadata.npmPackageName}", but is set to: ${packageName}.`,
234+
);
239235
}
240236

241-
let newPackageJson =
242-
insertFormatFieldsIntoPackageJson(outputRelativePath, packageJson, false);
237+
let newPackageJson = insertFormatFieldsIntoPackageJson(
238+
outputRelativePath,
239+
packageJson,
240+
false,
241+
);
243242

244243
newPackageJson = updatePrimaryPackageJson(newPackageJson);
245244

@@ -260,8 +259,10 @@ function main(args: string[]): void {
260259
* @param isGeneratedPackageJson Whether the passed package.json has been generated.
261260
*/
262261
function insertFormatFieldsIntoPackageJson(
263-
packageJsonOutRelativePath: string, parsedPackage: Readonly<PackageJson>,
264-
isGeneratedPackageJson: boolean): PackageJson {
262+
packageJsonOutRelativePath: string,
263+
parsedPackage: Readonly<PackageJson>,
264+
isGeneratedPackageJson: boolean,
265+
): PackageJson {
265266
const packageJson: PackageJson = {...parsedPackage};
266267
const packageName = packageJson['name'];
267268
const entryPointInfo = metadata.entryPoints[packageName];
@@ -280,30 +281,37 @@ function main(args: string[]): void {
280281
console.error('WARNING: no module metadata for package', packageName);
281282
console.error(' Not updating the package.json file to point to it');
282283
console.error(
283-
' The ng_module for this package is possibly missing the module_name attribute ');
284+
' The ng_module for this package is possibly missing the module_name attribute ',
285+
);
284286
return packageJson;
285287
}
286288

287289
// If we guessed the index paths for a module, and it contains an explicit `package.json`
288290
// file that already sets format properties, we skip automatic insertion of format
289291
// properties but report a warning in case properties have been set by accident.
290-
if (entryPointInfo.guessedPaths && !isGeneratedPackageJson &&
291-
hasExplicitFormatProperties(packageJson)) {
292+
if (
293+
entryPointInfo.guessedPaths &&
294+
!isGeneratedPackageJson &&
295+
hasExplicitFormatProperties(packageJson)
296+
) {
292297
console.error('WARNING: `package.json` explicitly sets format properties (like `main`).');
293298
console.error(
294-
' Skipping automatic insertion of format properties as explicit ' +
295-
'format properties are set.');
299+
' Skipping automatic insertion of format properties as explicit ' +
300+
'format properties are set.',
301+
);
296302
console.error(' Ignore this warning if explicit properties are set intentionally.');
297303
return packageJson;
298304
}
299305

300306
const fesm2022RelativeOutPath = getFlatEsmOutputRelativePath(entryPointInfo.fesm2022Bundle);
301307
const typingsRelativeOutPath = getTypingOutputRelativePath(entryPointInfo.typings);
302308

303-
packageJson.module =
304-
normalizePath(path.relative(packageJsonContainingDir, fesm2022RelativeOutPath));
305-
packageJson.typings =
306-
normalizePath(path.relative(packageJsonContainingDir, typingsRelativeOutPath));
309+
packageJson.module = normalizePath(
310+
path.relative(packageJsonContainingDir, fesm2022RelativeOutPath),
311+
);
312+
packageJson.typings = normalizePath(
313+
path.relative(packageJsonContainingDir, typingsRelativeOutPath),
314+
);
307315

308316
return packageJson;
309317
}
@@ -315,8 +323,9 @@ function main(args: string[]): void {
315323
function updatePrimaryPackageJson(packageJson: Readonly<PackageJson>): PackageJson {
316324
if (packageJson.type !== undefined) {
317325
throw Error(
318-
'The primary "package.json" file of the package sets the "type" field ' +
319-
'that is controlled by the packager. Please unset it.');
326+
'The primary "package.json" file of the package sets the "type" field ' +
327+
'that is controlled by the packager. Please unset it.',
328+
);
320329
}
321330

322331
const newPackageJson: PackageJson = {...packageJson};
@@ -330,8 +339,9 @@ function main(args: string[]): void {
330339
// Capture all entry-points in the `exports` field using the subpath export declarations:
331340
// https://nodejs.org/api/packages.html#packages_subpath_exports.
332341
for (const [moduleName, entryPoint] of Object.entries(metadata.entryPoints)) {
333-
const subpath =
334-
isSecondaryEntryPoint(moduleName) ? `./${getEntryPointSubpath(moduleName)}` : '.';
342+
const subpath = isSecondaryEntryPoint(moduleName)
343+
? `./${getEntryPointSubpath(moduleName)}`
344+
: '.';
335345
const esmIndexOutRelativePath = getEsm2022OutputRelativePath(entryPoint.index);
336346
const fesm2022OutRelativePath = getFlatEsmOutputRelativePath(entryPoint.fesm2022Bundle);
337347
const typesOutRelativePath = getTypingOutputRelativePath(entryPoint.typings);
@@ -356,7 +366,10 @@ function main(args: string[]): void {
356366
* @throws An error if the mapping is already defined and would conflict.
357367
*/
358368
function insertExportMappingOrError(
359-
packageJson: PackageJson, subpath: string, mapping: ConditionalExport) {
369+
packageJson: PackageJson,
370+
subpath: string,
371+
mapping: ConditionalExport,
372+
) {
360373
if (packageJson.exports === undefined) {
361374
packageJson.exports = {};
362375
}
@@ -372,8 +385,9 @@ function main(args: string[]): void {
372385
for (const conditionName of Object.keys(mapping) as [keyof ConditionalExport]) {
373386
if (subpathExport[conditionName] !== undefined) {
374387
throw Error(
375-
`Found a conflicting export condition for "${subpath}". The "${conditionName}" ` +
376-
`condition would be overridden by the packager. Please unset it.`);
388+
`Found a conflicting export condition for "${subpath}". The "${conditionName}" ` +
389+
`condition would be overridden by the packager. Please unset it.`,
390+
);
377391
}
378392

379393
// **Note**: The order of the conditions is preserved even though we are setting
@@ -384,10 +398,9 @@ function main(args: string[]): void {
384398

385399
/** Whether the package explicitly sets any of the format properties (like `main`). */
386400
function hasExplicitFormatProperties(parsedPackage: Readonly<PackageJson>): boolean {
387-
return Object.keys(parsedPackage)
388-
.some(
389-
(fieldName: string) => knownFormatPackageJsonFormatFields.includes(
390-
fieldName as KnownPackageJsonFormatFields));
401+
return Object.keys(parsedPackage).some((fieldName: string) =>
402+
knownFormatPackageJsonFormatFields.includes(fieldName as KnownPackageJsonFormatFields),
403+
);
391404
}
392405

393406
/**

0 commit comments

Comments
 (0)