From 5fa4ef8b78e018097799be8e0ef33f0dbfaf8825 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Fri, 27 Mar 2026 18:39:39 +0700 Subject: [PATCH] Convert format to TS (#34780) The PR converts format.js (build/commands/) to TypeScript --- PRESUBMIT.py | 2 +- .../commands/scripts/{format.js => format.ts} | 76 +++++++++++++------ package.json | 2 +- 3 files changed, 56 insertions(+), 24 deletions(-) rename build/commands/scripts/{format.js => format.ts} (81%) diff --git a/PRESUBMIT.py b/PRESUBMIT.py index 7bc92196d1a..7fb02056f0e 100644 --- a/PRESUBMIT.py +++ b/PRESUBMIT.py @@ -55,7 +55,7 @@ def CheckLeoVariables(input_api, output_api): def CheckPatchFormatted(input_api, output_api): cmd = [ brave_chromium_utils.wspath( - '//brave/build/commands/scripts/format.js'), '--presubmit' + '//brave/build/commands/scripts/format.ts'), '--presubmit' ] if input_api.PRESUBMIT_ALL_BRAVE: cmd.append('--all-files') diff --git a/build/commands/scripts/format.js b/build/commands/scripts/format.ts similarity index 81% rename from build/commands/scripts/format.js rename to build/commands/scripts/format.ts index c54df787b53..21a63186a8e 100644 --- a/build/commands/scripts/format.js +++ b/build/commands/scripts/format.ts @@ -5,9 +5,12 @@ import path from 'node:path' import fs from 'fs-extra' -import { default as prettier } from 'prettier' import program from 'commander' -import { spawnSync } from 'node:child_process' +import { spawnSync, type SpawnSyncReturns } from 'node:child_process' + +// prettier does not provide a default export in .d.ts file. +// eslint-disable-next-line import/default +import prettier from 'prettier' import config from '../lib/config.js' import util from '../lib/util.js' @@ -47,7 +50,7 @@ program // Replace the first 4 lines of the diff output with the before/after // format header. -const convertDiff = (diffOutput, file) => { +const convertDiff = (diffOutput: string, file: string) => { let pos = -1 for (let i = 0; i < 4; i++) { pos = diffOutput.indexOf('\n', pos + 1) @@ -69,7 +72,7 @@ const getAllFiles = () => { .split('\n') } -const formatOutput = (result) => { +const formatOutput = (result: SpawnSyncReturns) => { return [result.stdout, result.stderr] .filter((v) => v.length) .join('\nstderr:\n') @@ -77,7 +80,14 @@ const formatOutput = (result) => { // A function that formats the code in the current diff with base branch. // It uses git cl format and prettier, then aggregates the results. -async function runFormat(options = {}) { +async function runFormat(options: { + base?: string + full?: boolean + presubmit?: boolean + dryRun?: boolean + onlyPrettier?: boolean + allFiles?: boolean +}) { if (!options.base) { options.base = 'origin/master' } @@ -104,7 +114,7 @@ async function runFormat(options = {}) { args.push('--dry-run', '--diff') } - let formatIssues = [] + const formatIssues: string[] = [] const shouldRunGitClFormat = !options.onlyPrettier const shouldRunPrettier = true @@ -133,10 +143,14 @@ async function runFormat(options = {}) { : util.getChangedFiles(config.braveCoreDir, options.base, skipLogging) if (shouldRunPrettier) { - formatIssues.push(...(await runPrettier(filesToFormat, options.dryRun))) + formatIssues.push( + ...(await runPrettier(filesToFormat, options.dryRun ?? false)), + ) } if (shouldRunMojomFormat) { - formatIssues.push(...(await runMojomFormat(filesToFormat, options.dryRun))) + formatIssues.push( + ...(await runMojomFormat(filesToFormat, options.dryRun ?? false)), + ) } if (options.dryRun && formatIssues.length > 0) { @@ -145,7 +159,11 @@ async function runFormat(options = {}) { } } -const handleDifference = async (file, dryRun, formatted) => { +const handleDifference = async ( + file: string, + dryRun: boolean, + formatted: string, +) => { if (!dryRun) { await fs.writeFile(file, formatted) return @@ -164,14 +182,18 @@ const handleDifference = async (file, dryRun, formatted) => { } } -const runPrettierForFile = async (file, dryRun, ignorePath) => { +const runPrettierForFile = async ( + file: string, + dryRun: boolean, + ignorePath: string, +): Promise => { const fileInfo = await prettier.getFileInfo(file, { - ignorePath: ignorePath, + ignorePath, withNodeModules: false, }) if (fileInfo.ignored || !fileInfo.inferredParser) { - return '' + return undefined } const options = await prettier.resolveConfig(file) @@ -184,17 +206,21 @@ const runPrettierForFile = async (file, dryRun, ignorePath) => { if (content !== formatted) { return await handleDifference(file, dryRun, formatted) } - return '' + + return undefined } -const runPrettier = async (files, dryRun) => { +const runPrettier = async ( + files: string[], + dryRun: boolean, +): Promise => { console.log('run prettier for', files.length, 'files') const ignorePath = path.join(config.braveCoreDir, '.prettierignore') if (!fs.existsSync(ignorePath)) { throw new Error(`${ignorePath} file not found`) } - const prettierIssues = [] + const prettierIssues: string[] = [] for (const file of files) { try { const issue = await runPrettierForFile(file, dryRun, ignorePath) @@ -209,9 +235,12 @@ const runPrettier = async (files, dryRun) => { return prettierIssues } -const runMojomFormatForFile = async (file, dryRun) => { +const runMojomFormatForFile = async ( + file: string, + dryRun: boolean, +): Promise => { if (!file.endsWith('.mojom')) { - return '' + return undefined } // Mojom formatting is experimental. Only these files are formatted by now. const mojomFormatAllowList = ['**/brave_wallet/**/*.mojom'] @@ -219,12 +248,12 @@ const runMojomFormatForFile = async (file, dryRun) => { if ( !mojomFormatAllowList.some((pattern) => path.matchesGlob(file, pattern)) ) { - return '' + return undefined } const content = await fs.readFile(file, { encoding: 'utf-8' }) if (!content) { - return '' + return undefined } const mojomFormatArgs = [ @@ -254,11 +283,14 @@ const runMojomFormatForFile = async (file, dryRun) => { if (content !== formatted) { return await handleDifference(file, dryRun, formatted) } - return '' + return undefined } -const runMojomFormat = async (files, dryRun) => { - const mojomFormatIssues = [] +const runMojomFormat = async ( + files: string[], + dryRun: boolean, +): Promise => { + const mojomFormatIssues: string[] = [] for (const file of files) { try { const issue = await runMojomFormatForFile(file, dryRun) diff --git a/package.json b/package.json index 0a044c75074..a3667581e24 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "pull_l10n": "node ./build/commands/scripts/commands.js pull_l10n", "chromium_rebase_l10n": "node ./build/commands/scripts/commands.js chromium_rebase_l10n", "presubmit": "node ./build/commands/scripts/presubmit.js", - "format": "node ./build/commands/scripts/format.js", + "format": "node ./build/commands/scripts/format.ts", "mass_rename": "node ./build/commands/scripts/commands.js mass_rename", "docs": "node ./build/commands/scripts/commands.js docs", "test": "node ./build/commands/scripts/commands.js test",