Skip to content

extractComponent returns the boolean true for a CommonJS namespace whose first key is __esModule #4087

Description

@kojiwakayama

What

src/modules/react-loader/extract-component.ts:19-20:

const firstKey = Object.keys(moduleObj)[0];
const component = moduleObj.default ?? (firstKey ? moduleObj[firstKey] : undefined);

For a transpiled CommonJS namespace shaped { __esModule: true, Named }, moduleObj.default is undefined, firstKey is "__esModule", and the extracted "component" is therefore the boolean true.

That shape is what many transpilers emit for a module with only named exports, so this is not an exotic input.

Impact

The caller receives true where it expects a component. Rendering it fails later and further from the cause than it would if extraction had returned undefined.

Suggested fix

Skip the __esModule marker (and any non-function, non-object value) when falling back to the first key, or fall back only to keys that look like components.

Provenance

Found during a test-quality audit of src/modules (PR #4058). Deliberately not asserted in that PR — writing a test around the current output would enshrine the wrong behavior as correct, which is the opposite of what the audit is for. Reported here instead so the fix can decide the intended contract.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions