/* eslint-disable @typescript-eslint/consistent-type-assertions */ import type { ModuleLike } from './types'; /** * Formats the shape of an imported module to align with the * ES module specification. * * For ES or ES-like modules, returns the shape as-is. * * For CommonJS modules, returns an object with the following: * - `module.exports` under the `default` property. * - `exports.` under properties of the same name, * and also under a `default` object. */ export function interopModule( result: unknown, ): ModuleLike { if (typeof result !== 'object' || result === null) { return { default: result } as ModuleLike; } // Already a module, so return early if ('__esModule' in result || 'default' in result) { return result as ModuleLike; } return { ...result, default: result } as unknown as ModuleLike; }