Repository: storybookjs/storybook
File: code/core/src/common/js-package-manager/JsPackageManager.ts (TypeScript)

Code before:
      logger.debug(`Using cached package.json for ${absolutePath}...`);
      return cached;
    }

    // Read from disk if not in cache
    const jsonContent = readFileSync(absolutePath, 'utf8');
    const packageJSON = JSON.parse(jsonContent);
    packageJSON[Symbol.for('indent')] = detectIndent(jsonContent).indent ?? 2;

    const result: PackageJsonWithDepsAndDevDeps = {
      ...packageJSON,
      dependencies: { ...(packageJSON.dependencies || {}) },
      devDependencies: { ...(packageJSON.devDependencies || {}) },
      peerDependencies: { ...(packageJSON.peerDependencies || {}) },
    };

    // Store in cache
    JsPackageManager.packageJsonCache.set(absolutePath, result);

    return result;
  }

  #getIndent(filePath: string): string | number {
    try {
      const packageJson = JsPackageManager.getPackageJson(filePath);
      return packageJson[Symbol.for('indent')];
    } catch (e) {
      return 2;
    }
  }

  writePackageJson(packageJson: PackageJson, directory = this.cwd) {
    const packageJsonToWrite = { ...packageJson };
    const dependencyTypes = ['dependencies', 'devDependencies', 'peerDependencies'] as const;

    // Remove empty dependency objects
    dependencyTypes.forEach((type) => {
      if (packageJsonToWrite[type] && Object.keys(packageJsonToWrite[type]).length === 0) {
        delete packageJsonToWrite[type];
      }
    });
    const filePath = join(directory, 'package.json');
    const indent = this.#getIndent(filePath);
    const packageJsonPath = normalize(resolve(directory, 'package.json'));
    const content = `${JSON.stringify(packageJsonToWrite, null, indent)}\n`;
    writeFileSync(packageJsonPath, content, 'utf8');

    // Update cache with the written content
    // Ensure dependencies and devDependencies exist (even if empty) to match PackageJsonWithDepsAndDevDeps type
    const cachedPackageJson: PackageJsonWithDepsAndDevDeps = {
      ...packageJsonToWrite,

Review comment (suggestion):
There is no need to use `Symbol.for`. Just define it as a constant at the top of the file and reference that.
```suggestion
      return packageJson[indentSymbol];
```

With at the top of the file:
```js
const indentSymbol = Symbol('indent');
```

Code after:
    // resolve() normalizes absolute paths too, ensuring consistent cache keys
    const absolutePath = normalize(resolve(packageJsonPath));

    // Check cache first
    const cached = JsPackageManager.packageJsonCache.get(absolutePath);
    if (cached) {
      logger.debug(`Using cached package.json for ${absolutePath}...`);
      return cached;
    }

    // Read from disk if not in cache
    const jsonContent = readFileSync(absolutePath, 'utf8');
    const packageJSON = JSON.parse(jsonContent);
    // Symbol key keeps this metadata non-enumerable so JSON.stringify omits it
    packageJSON[indentSymbol] = detectIndent(jsonContent).indent ?? 2;

    const result: PackageJsonWithIndent = {
      ...packageJSON,
      dependencies: { ...(packageJSON.dependencies || {}) },
      devDependencies: { ...(packageJSON.devDependencies || {}) },
      peerDependencies: { ...(packageJSON.peerDependencies || {}) },
    };

    // Store in cache
    JsPackageManager.packageJsonCache.set(absolutePath, result);

    return result;
  }

  #getIndent(filePath: string): string | number {
    try {
      const packageJson = JsPackageManager.getPackageJson(filePath);
      return packageJson[indentSymbol];
    } catch (e) {
      return 2;
    }
  }

  writePackageJson(packageJson: PackageJson, directory = this.cwd) {
    const packageJsonToWrite = { ...packageJson };
    const dependencyTypes = ['dependencies', 'devDependencies', 'peerDependencies'] as const;

    // Remove empty dependency objects
    dependencyTypes.forEach((type) => {
      if (packageJsonToWrite[type] && Object.keys(packageJsonToWrite[type]).length === 0) {
        delete packageJsonToWrite[type];
      }
    });
    const filePath = join(directory, 'package.json');
    const indent = this.#getIndent(filePath);
    const packageJsonPath = normalize(resolve(directory, 'package.json'));