Rename module identifier by wrapping program.body with IIFE. #229
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Before this change, in order to obtain a reliable reference to the original
module
object, the Reify compiler would inject a variable declaration at the beginning of the generated code that bound some other (uniquely named) variable to themodule
object, and then that unique variable was used in all the other Reify-generated expressions, such asmodule1.export(...)
andmodule1.link(...)
.The trouble with this strategy was that the injected variable declaration could still collide with other declarations in the module scope that involve the
module
identifer.A better way to solve that problem is to introduce another scope by wrapping the module code in an immediately invoked function expression. The original
module
object can be passed as an argument to that function, and the corresponding parameter can be uniquely named:However, this wrapping should happen as late as possible, in some cases after multiple invocations of the Reify compiler, so the wrapping should not be the responsibility of the Reify compiler itself. Instead, the compiler should just use whatever
options.moduleAlias
is passed in, and not second-guess it or attempt to inject any variable bindings.In this PR, the Babel plugin (
reify/plugins/babel
) becomes responsible for the IIFE wrapping shown above, while also gaining the ability to transform the AST twice: upon entering and upon exiting theProgram
AST node. This double transformation is necessary to transform bothimport
andexport
declarations found in the source code, and those that were injected later by other transforms, such as@babel/plugin-transform-runtime
.