Repository: tailwindlabs/tailwindcss
File: packages/@tailwindcss-upgrade/src/index.ts (TypeScript)

Code before:
    )

    for (let result of migrateResults) {
      if (result.status === 'rejected') {
        error(`${result.reason}`)
      }
    }

    // Split up stylesheets (as needed)
    try {
      await splitStylesheets(stylesheets)
    } catch (e: unknown) {
      error(`${e}`)
    }

    // Cleanup `@import "…" layer(utilities)`
    for (let sheet of stylesheets) {
      // If the `@import` contains an injected `layer(…)` we need to remove it
      if (!Array.from(sheet.importRules).some((node) => node.raws.tailwind_injected_layer)) {
        continue
      }

      let hasAtUtility = false

      // Only remove the `layer(…)` next to the import, if any of the children
      // contains an `@utility`. Otherwise the `@utility` will not be top-level.
      {
        sheet.root.walkAtRules('utility', () => {
          hasAtUtility = true
          return false
        })

        if (!hasAtUtility) {
          for (let child of sheet.descendants()) {
            child.root.walkAtRules('utility', () => {
              hasAtUtility = true
              return false
            })

            if (hasAtUtility) {
              break
            }
          }
        }
      }

      // No `@utility` found, we can keep the `layer(…)` next to the import
      if (!hasAtUtility) continue

      for (let importNode of sheet.importRules) {
        if (importNode.raws.tailwind_injected_layer) {

Review comment (refactor):
```suggestion
        // Only remove the `layer(…)` next to the import if any of the children
        // contain `@utility`. Otherwise `@utility` will not be top-level.
```

Code after:
    )

    for (let result of migrateResults) {
      if (result.status === 'rejected') {
        error(`${result.reason}`)
      }
    }

    // Split up stylesheets (as needed)
    try {
      await splitStylesheets(stylesheets)
    } catch (e: unknown) {
      error(`${e}`)
    }

    // Cleanup `@import "…" layer(utilities)`
    for (let sheet of stylesheets) {
      for (let importRule of sheet.importRules) {
        if (!importRule.raws.tailwind_injected_layer) continue
        let importedSheet = stylesheets.find(
          (sheet) => sheet.id === importRule.raws.tailwind_destination_sheet_id,
        )
        if (!importedSheet) continue

        // Only remove the `layer(…)` next to the import, if any of the children
        // contains an `@utility`. Otherwise the `@utility` will not be top-level.
        if (
          !importedSheet.containsRule((node) => node.type === 'atrule' && node.name === 'utility')
        ) {
          continue
        }

        // Make sure to remove the `layer(…)` from the `@import` at-rule
        importRule.params = importRule.params.replace(/ layer\([^)]+\)/, '').trim()
      }
    }

    // Format nodes
    for (let sheet of stylesheets) {
      await postcss([formatNodes()]).process(sheet.root!, { from: sheet.file! })
    }

    // Write all files to disk
    for (let sheet of stylesheets) {
      if (!sheet.file) continue

      await fs.writeFile(sheet.file, sheet.root.toString())
    }

    success('Stylesheet migration complete.')
  }