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
RFC 0004: Add ImportExpression
AST type
#4
RFC 0004: Add ImportExpression
AST type
#4
Conversation
Note this will also likely impact |
Co-authored-by: Brian Ng <bng412@gmail.com>
@ljharb I think any ESLint plugins that relies on |
Co-authored-by: Brian Ng <bng412@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this proposal. Note that I don't think that aligning with ESTree should be a goal for us, but this AST shape is definitely better than our current one since it "automatically" disallows invalid states.
Co-authored-by: Brian Ng <bng412@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ljharb Currently, the @babel/plugin-proposal-dynamic-import
when targeting CommonJS is just a proxy to babel-plugin-dynamic-import
.
Since this AST change makes it harder to have a plugin working with Babel 6-7 and with Babel 8, I was thinking that it would be easier to only support Babel 6-7 in babel-plugin-dynamic-import
and write the new logic for Babel 8 in our monorepo.
Does that sound good to you?
@nicolo-ribaudo that doesn't make sense to me; how does it make it harder? It seems like I just have to add an extra visitor. If babel wants to inline the dependency, that's one thing, but otherwise it seems pretty trivial to support 6, 7, and 8? More to the point, if it's not easy to support both babel 7 and 8 in the same transform, then imo the change is poorly designed and shouldn't happen at all. |
} | ||
``` | ||
|
||
When `@babel/parser` [supported](https://github.com/babel/babylon/pull/163) dynamic import, it was called `import-function` and shared similar syntax structure with `super` call that is parsed as `CallExpression[callee=Super]`. However, it encourages an incorrect mental model, as developers may think of `import()` as a function call. Besides, it was later syntactically limited to one argument, different from other call expressions. Last, it is also problematic when we extend the current AST shape to support stage-1 [module attributes](https://github.com/tc39/proposal-module-attributes) (`import(moduleName, attributes)`) since the arguments (`moduleName` and `attributes` respectively) share the same semantics in the AST. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a bit of discussion in tc39/proposal-import-attributes#52. Honestly, I'd rather we treat it as a regular function call, exactly the same as super()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I very much doubt that import()
will ever get consensus to be treated as a normal function call; there was a lot of discussion about that during its advancement. It’s syntax, and will likely always be special in a number of ways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that the ImportExpression
is also created to be consistent with ImportDeclaration
.
interface ImportDeclaration <: ModuleDeclaration {
type: "ImportDeclaration";
source: Literal;
}
interface ImportExpression <: Expression {
type: "ImportExpression";
source: Expression;
}
Although on the CST level an import expression shares similar syntax features with a function call, but on the AST level I think we should abstract them to common import interface.
@JLHwung is there anything left before merging this PR? |
@ACSAIS
|
Didn't know it, thanks for your answer @JLHwung ! |
Progress? |
This AST change would also make it easier to support https://github.com/tc39/proposal-import-reflection/. I wonder if we can start implementing it in Babel 7 as opt-in with a parser option. |
Though estree uses I wounder what the ast will look like for {
"type": "ImportExpression",
"phase": "source"
} ?
{
"type": "CallExpression",
"callee": {
"type": "MetaProperty"
}
} |
@fisker Good question. Currently the {
"type": "CallExpression",
"callee": {
"type": "ImportSource"
}
} but popular matchers like |
That's reasonable. 👍 Introduce What do you think |
@fisker The Babel AST of import phase will follow the ESTree proposal here. It will be built on top of the |
@JLHwung Can we enable the new mode by default for Babel 8, and then merge this RFC? |
This is enabled by default in Babel 8 🎉 |
ImportExpression
AST type
Woo! :D |
View Rendered Text
This RFC proposes to parse
import()
as a new AST node typeImportExpression
instead ofCallExpression
.Pinging a few people who may be interested in this:
@axetroy - vscode-npm-import-package-version
@devongovett - parcel
@flytam - babel-plugin-resolve-config-json
@kevinbarabash - eslint-plugin-khan
@ljharb - babel-plugin-dynamic-import-node, eslint-plugin-import
@suchipi - transform-imports
@swannodette - closurescript
Please share this RFC to anyone who can contribute to the discussion.