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

Code before:
        if (child !== node) return

        // At this point we already know that we have a circular dependency.
        //
        // Figure out which candidate caused the circular dependency. This will
        // help to create a useful error message for the end user.
        let candidate = candidates.find((candidate) => {
          let selector = `.${escape(candidate)}`
          let found = false

          for (let rule of candidateAst) {
            if (rule.kind !== 'rule') continue
            if (rule.selector !== selector) continue

            walk(rule.nodes, (child) => {
              if (child === node) {
                found = true
                return WalkAction.Stop
              }
            })

            if (found) return found
          }

          return found
        })

        throw new Error(
          `You cannot \`@apply\` the \`${candidate}\` utility here because it creates a circular dependency.`,
        )
      })

      replaceWith(newNodes)
    }
  })
}

export function __unstable__loadDesignSystem(css: string) {
  // Find all `@theme` declarations
  let theme = new Theme()
  let ast = CSS.parse(css)

  walk(ast, (node) => {
    if (node.kind !== 'rule') return
    if (node.selector !== '@theme' && !node.selector.startsWith('@theme ')) return
    let { isReference, isInline } = parseThemeOptions(node.selector)

    // Record all custom properties in the `@theme` declaration
    walk([node], (node) => {
      if (node.kind !== 'declaration') return
      if (!node.property.startsWith('--')) return

Review comment (performance):
We are walking inside of a `.find()` which could be slow. BUT, this only happens if we already know that we have a circular dependency so we will throw an error anyway. We are just doing this to figure out which candidate the culprit is to improve the error message.

Code after:
        for (let candidate of candidates) {
          let selector = `.${escape(candidate)}`

          for (let rule of candidateAst) {
            if (rule.kind !== 'rule') continue
            if (rule.selector !== selector) continue

            walk(rule.nodes, (child) => {
              if (child !== node) return

              throw new Error(
                `You cannot \`@apply\` the \`${candidate}\` utility here because it creates a circular dependency.`,
              )
            })
          }
        }
      })

      replaceWith(newNodes)
    }
  })
}

export function __unstable__loadDesignSystem(css: string) {
  // Find all `@theme` declarations
  let theme = new Theme()
  let ast = CSS.parse(css)

  walk(ast, (node) => {
    if (node.kind !== 'rule') return
    if (node.selector !== '@theme' && !node.selector.startsWith('@theme ')) return
    let { isReference, isInline } = parseThemeOptions(node.selector)

    // Record all custom properties in the `@theme` declaration
    walk([node], (node) => {
      if (node.kind !== 'declaration') return
      if (!node.property.startsWith('--')) return

      theme.add(node.property, node.value, { isReference, isInline })
    })
  })

  return buildDesignSystem(theme)
}