-
Notifications
You must be signed in to change notification settings - Fork 7
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
"Magic parallel scope" alternative: import.parent
#27
Comments
Interesting suggestion. Raw train of thought:
While writing this I have been tempted to suggest hiding the
|
The word “parent” might be confusing; node’s CJS has module.parent and it refers to whoever first required the module. Lexical scoping isn’t really a parent-child relationship. |
Name bikeshed: |
Separate from bikeshedding, it’s very unideal to be forced to expose something in my API just so i can use it inside a module declaration/block - is there a reason things can’t be explicitly referenced in the syntax? like: module A {}
module B {}
module C with A, B { } |
One could always use an inner module to enforce that boundary; i.e. export const foo = ...;
module A {}
module B {}
module C with A, B {} could be written as module inner {
export const foo = ...;
export module A {}
export module B {}
module C {
import { A, B } from import.parent;
}
}
export { foo } from inner; |
This comment was marked as duplicate.
This comment was marked as duplicate.
It's already mentioned in the OP. The problem is that in order for this to work in dynamic imports we have to make |
|
Module declarations/expressions right now capture bindings of other module declarations:
When advancing this proposal to Stage 2, different concerns have been raised due to the different scoping behavior of this new binding type.
Solution
While discussing with @lucacasonato about this problem, we came up with a possible solution:
import.parent
meta property, that:null
Module
object that syntactically encloses the current moduleimport ... from import.parent;
to import from the parent module.Drawbacks
Module declarations need to be exported to expose them to children modules:
It's not possible to directly import from ancestors other than the parent, unless they re explicitly re-exported by the parent module (for example, using the import reflection proposal).
Currently module declarations allow doing this:
and it would need to be re-written to this:
However, the vast majority of use cases for module declarations is with no nesting, so this shouldn't hurt usability much in practice. Additionally, it simplifies refactoring because you only have to pay attention to
import.parent
rather than to all the module bindings higher in the scope chain.Example
Consider this example with the current proposal:
it would be rewritten as follows:
FAQ
Why
import.parent
?We considered different alternatives, such as just a
parent
identifier (with a restriction that prevents module declarations from being namedparent
), orsuper
(proposed in How to import from the parent module? #20). However, the meta-property-based syntax has the advantage that it can also work in dynamic imports:How does this interact with the
Module
constructor?Compartments Layer 0 expands the
Module
constructor so that it can be used to customize the linking behavior of modules:Integrating the module declarations proposal with such constructor was incredibly challenging, because we needed a static way of representing the module declarations captured by the outer scope. Something like the following:
and
A
would have been magically injected as visible in the constructed module's scope.With this
import.parent
simplification, theModule
constructor could simply accept an optionalparentModule
property, whose value is then exposed asimport.meta
without affecting the visible bindings:The text was updated successfully, but these errors were encountered: