nodejs / import-in-the-middle

Like `require-in-the-middle`, but for ESM import
https://www.npmjs.com/package/import-in-the-middle
Apache License 2.0
55 stars 22 forks source link

Does not handle CallExpression in ExportDefaultDeclaration node #77

Closed mohd-akram closed 1 month ago

mohd-akram commented 1 month ago

Expected Behavior

Works.

Actual Behavior

TypeError: Cannot read properties of undefined (reading 'name')
    at getEsmExports (/Users/user/code/project/node_modules/import-in-the-middle/lib/get-esm-exports.js:71:59)
    at getExports (/Users/user/code/project/node_modules/import-in-the-middle/lib/get-exports.js:80:12)
    at async processModule (/Users/user/code/project/node_modules/import-in-the-middle/hook.js:134:23)
    at async processModule (/Users/user/code/project/node_modules/import-in-the-middle/hook.js:160:20)
    at async processModule (/Users/user/code/project/node_modules/import-in-the-middle/hook.js:160:20)
    at async processModule (/Users/user/code/project/node_modules/import-in-the-middle/hook.js:160:20)
    at async processModule (/Users/user/code/project/node_modules/import-in-the-middle/hook.js:160:20)
    at async processModule (/Users/user/code/project/node_modules/import-in-the-middle/hook.js:160:20)
    at async getSource (/Users/user/code/project/node_modules/import-in-the-middle/hook.js:269:60)
    at async load (/Users/user/code/project/node_modules/import-in-the-middle/hook.js:335:26)

Steps to Reproduce the Problem

I'm getting this after updating Sentry to 8.x. I'm not sure where exactly import-in-the-middle is being called from, but the gist of it is that it is crashing on this file (node_modules/adminjs/lib/frontend/components/app/sort-link.js) because it has a function call in the export.

  1. require('import-in-the-middle/lib/get-esm-exports')({moduleSource: 'export default parseInt("1");', defaultAs: 'SortLink'})

Specifications

timfish commented 1 month ago

I haven't seen one before!

Can you try applying this patch which combines a load of outstanding PRs and see if that fixes it?

mohd-akram commented 1 month ago

Tried it, it doesn't work. It only patches hook.js while the problem is in get-esm-exports.js.

timfish commented 1 month ago

I've tried reproducing this with a simple export default parseInt("1") but it parses without error. Do you have a more full example to reproduce this?

mohd-akram commented 1 month ago

The issue only happens when defaultAs is provided, as in the one-liner I mentioned. I imagine it comes from here in the library where this happens. The code expects a function node but doesn't check the type: https://github.com/DataDog/import-in-the-middle/blob/bf3a4fbb0b0a4fd806aefb8d2a598894303a0ebf/lib/get-esm-exports.js#L67-L70