Navigation Menu

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

Parse import reflection #14926

Merged
merged 16 commits into from Oct 26, 2022
Merged

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Sep 13, 2022

Q                       A
Fixed Issues? Implement parsing / printing support of the stage-2 Import reflection proposal
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature? Yes
Tests Added + Pass? Yes
Documentation PR Link babel/website#2684
Any Dependency Changes?
License MIT

This PR also introduces the new syntax plugin and adds it to babel-standalone. The AST shape is still preliminary, for discussions about the AST shape, please visit estree/estree#287.

I will update the PR should the AST change.

@JLHwung JLHwung added PR: New Feature 🚀 A type of pull request used for our changelog categories Spec: Import Reflection labels Sep 13, 2022
@JLHwung JLHwung force-pushed the parse-import-reflection branch 2 times, most recently from 0fdd80c to 4f9b91e Compare September 13, 2022 20:15
@babel-bot
Copy link
Collaborator

babel-bot commented Sep 13, 2022

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/53242/

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this PR preserve parsing of import module from "x"?

@JLHwung
Copy link
Contributor Author

JLHwung commented Sep 20, 2022

Does this PR preserve parsing of import module from "x"?

Good catch. I thought tt._module were a keyword before. I am working on a fix.

@JLHwung JLHwung force-pushed the parse-import-reflection branch 2 times, most recently from 612d89c to a81f8bc Compare September 20, 2022 19:55
@nicolo-ribaudo
Copy link
Member

(fyi @lucacasonato @guybedford)

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this new plugin interact with import assertions?

Can we throw if both module and asserts as used in the same declaration, while we wait for the two proposals to decide how to integrate?

@liuxingbaoyu liuxingbaoyu added this to the 7.20.0 milestone Sep 21, 2022
@@ -855,6 +855,7 @@ export interface ImportDeclaration extends BaseNode {
source: StringLiteral;
assertions?: Array<ImportAttribute> | null;
importKind?: "type" | "typeof" | "value" | null;
module?: boolean | null;

This comment was marked as outdated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.ImportDeclaration will assign null to module because it is an optional property.

@@ -179,11 +179,15 @@ export function ExportDefaultDeclaration(
export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) {
this.word("import");
this.space();
this.printInnerComments(node);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to use indent=false here?

Comment on lines +2671 to +2680
if (nextNextTokenFirstChar === charCodes.lowercaseF) {
// import module from from ...
isImportReflection = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for import module from foo, just to verify that it errors even if foo starts with f?

Comment on lines +2677 to +2687
// import module { x } ...
// This is invalid, we will continue parsing and throw
// a recoverable error later
isImportReflection = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test for import module "foo"?

@nicolo-ribaudo
Copy link
Member

ping

@liuxingbaoyu
Copy link
Member

image
It doesn't seem to be expected here.

@JLHwung
Copy link
Contributor Author

JLHwung commented Oct 25, 2022

It doesn't seem to be expected here.

It is expected but not ideal, because an import declaration has more than one whitespace that can not be attached to any adjacent AST nodes:

import /* 1 */ { x } /* 2 */ from "foo" assert /* 3 */ { type: "json" };
import /* 1 */ type x from "foo";

In this example, all comments are inner comments of the import declaration. But the generator does not know where it should print inner comments. Currently what we can do is to infer such position from the AST structure (whether the specifier is an ImportSpecifier, whether it contains import assertions, etc). To precisely regenerate sources from AST, we will need structures more refined than innerComments or change the AST to avoid such whitespaces.

@liuxingbaoyu
Copy link
Member

I mean the comment positions are being swapped, which is kind of weird.

@@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected \"{\" (1:14)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error isn't the best, since { is not allowed here, but we can iterate on it.

@nicolo-ribaudo nicolo-ribaudo added PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release and removed PR: Needs Docs labels Oct 26, 2022
@nicolo-ribaudo nicolo-ribaudo merged commit dfc4b61 into babel:main Oct 26, 2022
@nicolo-ribaudo nicolo-ribaudo deleted the parse-import-reflection branch October 26, 2022 19:35
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jan 26, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: New Feature 🚀 A type of pull request used for our changelog categories PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release Spec: Import Reflection
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants