Repository: honojs/hono
File: build/remove-private-fields.ts (TypeScript)

Code before:
import type { PropertyDefinition, ParseResult } from 'oxc-parser'
import { parseSync, Visitor } from 'oxc-parser'
import { readFile, writeFile } from 'fs/promises'

export async function removePrivateFields(files: string[]) {
  const start = performance.now()
  const parsed = await Promise.all(
    files.map(async (file) => {
      const sourceCode = await readFile(file, 'utf-8')
      const ast = parseSync(file, sourceCode)
      return { file, sourceCode, ast }
    })
  )

  await Promise.all(
    parsed.map(async ({ file, sourceCode, ast }) => {
      const sourceCodeWithoutPrivateFields = removePrivateFieldFromSourceCode(ast, sourceCode)
      if (sourceCodeWithoutPrivateFields) await writeFile(file, sourceCodeWithoutPrivateFields)
    })
  )
  const end = performance.now()
  console.log(`Done removing private fields in ${(end - start).toFixed(2)}ms`)
}

export function removePrivateFieldFromSourceCode(ast: ParseResult, sourceCode: string) {
  const removals: PropertyDefinition[] = []
  new Visitor({
    ClassDeclaration: (node) => {
      node.body.body.forEach((elem) => {
        if (elem.type === 'PropertyDefinition' && elem.key.type === 'PrivateIdentifier') {
          removals.push(elem)
        }
      })
    },
  }).visit(ast.program)

  if (removals.length === 0) return

  let sourceCodeWithoutPrivateFields = sourceCode
  for (const elem of removals) {
    sourceCodeWithoutPrivateFields = removeRange(
      sourceCodeWithoutPrivateFields,
      elem.start,

Review comment (performance):
This is a matter of taste (and we might be able to use a lint rule to unify the style), but we don't write `if (true) return` on a single line. So, can you apply the following patch?

```diff
diff --git a/build/remove-private-fields.ts b/build/remove-private-fields.ts
index df949d40..15d7e821 100644
--- a/build/remove-private-fields.ts
+++ b/build/remove-private-fields.ts
@@ -15,7 +15,9 @@ export async function removePrivateFields(files: string[]) {
   await Promise.all(
     parsed.map(async ({ file, sourceCode, ast }) => {
       const sourceCodeWithoutPrivateFields = removePrivateFieldFromSourceCode(ast, sourceCode)
-      if (sourceCodeWithoutPrivateFields) await writeFile(file, sourceCodeWithoutPrivateFields)
+      if (sourceCodeWithoutPrivateFields) {
+        await writeFile(file, sourceCodeWithoutPrivateFields)
+      }
     })
   )
   const end = performance.now()
@@ -34,7 +36,9 @@ export function removePrivateFieldFromSourceCode(ast: ParseResult, sourceCode: s
     },
   }).visit(ast.program)

-  if (removals.length === 0) return
+  if (removals.length === 0) {
+    return
+  }

   let sourceCodeWithoutPrivateFields = sourceCode
   for (const elem of removals) {
```

Code after:
import type { PropertyDefinition, ParseResult } from 'oxc-parser'
import { parseSync, Visitor } from 'oxc-parser'
import { readFile, writeFile } from 'fs/promises'

export async function removePrivateFields(files: string[]) {
  const start = performance.now()
  const parsed = await Promise.all(
    files.map(async (file) => {
      const sourceCode = await readFile(file, 'utf-8')
      const ast = parseSync(file, sourceCode)
      return { file, sourceCode, ast }
    })
  )

  await Promise.all(
    parsed.map(async ({ file, sourceCode, ast }) => {
      const sourceCodeWithoutPrivateFields = removePrivateFieldFromSourceCode(ast, sourceCode)
      if (sourceCodeWithoutPrivateFields) {
        await writeFile(file, sourceCodeWithoutPrivateFields)
      }
    })
  )
  const end = performance.now()
  console.log(`Done removing private fields in ${(end - start).toFixed(2)}ms`)
}

export function removePrivateFieldFromSourceCode(ast: ParseResult, sourceCode: string) {
  const removals: PropertyDefinition[] = []
  new Visitor({
    ClassDeclaration: (node) => {
      node.body.body.forEach((elem) => {
        if (elem.type === 'PropertyDefinition' && elem.key.type === 'PrivateIdentifier') {
          removals.push(elem)
        }
      })
    },
  }).visit(ast.program)

  if (removals.length === 0) {
    return
  }

  let sourceCodeWithoutPrivateFields = sourceCode