Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cjs-module-lexer doesn't provide information regarding multiple re-exports #54260

Closed
dario-piotrowicz opened this issue Aug 7, 2024 · 2 comments
Labels
wrong repo Issues that should be opened in another repository.

Comments

@dario-piotrowicz
Copy link

dario-piotrowicz commented Aug 7, 2024

This issue (or feature request, I'm not sure) is related to the cjs-module-lexer.

The problem I'm facing is that it only reports the last re-export of cjs modules completely loosing the information about pre-existing ones.

For example by parsing the following code:

if (true) {
  module.exports = require("./a.cjs");
} else {
  module.exports = require("./b.cjs");
}

only the last re-export assignment is recognized and reported to the caller (reexports = ['./b.cjs' ]).

It would be very useful if all the re-exports in the code could be recognized and reported to the caller

For example as: reexports = ['./a.cjs', './b.cjs']

The above anyways could be considered ambiguous since the same result could be obtained by parsing

module.exports = {...require('./a.cjs'), ...require('./b,cjs')};

so maybe reexports could/should actually be an array or array of strings?
Meaning that parsing

if (true) {
  module.exports = {...require('./a.cjs'), ...require('./b,cjs')};
} else {
  module.exports = {...require('./c.cjs'), ...require('./d,cjs')};
}

could yield reexports = [['./a.cjs', './b.cjs' ], ['./c.cjs', './d.cjs' ]]

Minimal Reproduction

For convenience I've created a reproduction here: https://github.com/dario-piotrowicz/cjs-module-lexer-reexports-repro

Additional information

Bug?

As I mentioned, I am not sure if this could be considered a bug since the reexport resetting seems to be intentional.

Solving this might generate different problems

If this issue were to be addressed, I would imagine that the following code:

module.exports = require('./a.cjs');
module.exports = require('./b.cjs');

would also produce two re-exports... although only one is actually valid... so this is definitely something to keep in mind 😕

The lexer is content-unaware so I don't think much could be done to avoid the above arguably incorrect result, even if nesting was kept into account (which might be a stretch in the lexer) that would still not always generate perfect results, for example when parsing code like this:

if(true){
   module.exports = require('./a.cjs');
}

module.exports = require('./b.cjs');

(maybe instead of expanding reexports there could be a new field in the parse result? as extra-reexports or something like that that could host all the reexports excluding the last one? so keeping more or less the same exact functionality the lexer has now whilst also providing callers with the extra bit of information?)

More context

To give a bit more context, I am simply trying to parse cjs code and gather informations about all the exports and re-exports that the code contains, unfortunately as mentioned in the issue the lexer currently doesn't return all possible re-exports but only the last one encountered, this is problematic in my use case as I would want to know all possible re-exports as well (and that information gets, as far as I can tell, completely lost).

@RedYetiDev RedYetiDev added the wrong repo Issues that should be opened in another repository. label Aug 8, 2024
@RedYetiDev
Copy link
Member

Can a member transfer this to https://github.com/nodejs/cjs-module-lexer?

@dario-piotrowicz
Copy link
Author

dario-piotrowicz commented Aug 8, 2024

@RedYetiDev sorry I had no idea this had its own repo 😓

I've closed this issue and recreated it there (nodejs/cjs-module-lexer#99) 👍

@dario-piotrowicz dario-piotrowicz closed this as not planned Won't fix, can't repro, duplicate, stale Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wrong repo Issues that should be opened in another repository.
Projects
None yet
Development

No branches or pull requests

2 participants