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

AST of import declaration without attributes #314

Open
sosukesuzuki opened this issue Feb 3, 2024 · 5 comments
Open

AST of import declaration without attributes #314

sosukesuzuki opened this issue Feb 3, 2024 · 5 comments

Comments

@sosukesuzuki
Copy link
Contributor

The following AST is defined for import attributes:

extend interface ImportDeclaration {
    attributes: [ ImportAttribute ];
}

This means import foo from "mod"; and import foo from "mod" with {}; have the same AST.

This isn't good for implementing code formatters. For example, Prettier's implementation reads the original text of the input to print attributes( https://github.com/prettier/prettier/blob/f5e339351e792cea0e8a6b111b336d5efa6441ea/src/language-js/print/module.js#L251-L272 ).

So I propose representing attributes as an empty array in import foo from "mod" with {}; and as null in import foo from "mod";.

What do you think?

@bradzacher
Copy link

One point against doing this is that the import specifiers follow the same pattern - the array is always defined, regardless of the presence of the braces. So keeping it like this is standardised with that, at least, for better or worse.

Another point against this is that there are already implementations of the current spec. Things can change though and (AFAIK) acorn/eslint don't have support yet.

@sosukesuzuki
Copy link
Contributor Author

One point against doing this is that the import specifiers follow the same pattern - the array is always defined, regardless of the presence of the braces. So keeping it like this is standardised with that, at least, for better or worse.

Yes, Prettier actually determines whether to print {} for import specifiers in the same way ( https://github.com/prettier/prettier/blob/6db66c53d948d0a70dbc7abfa3f7d6f01f544242/src/language-js/print/module.js#L233-L249 ). If it's still possible to change the spec at this point, as someone implementing a code formatter, I would appreciate it if the import specifiers for import {} from "mod"; and import "mod"; could be distinguished as an empty array and null.

Another point against this is that there are already implementations of the current spec. Things can change though and (AFAIK) acorn/eslint don't have support yet.

Since import attributes is still stage 3 syntax, I think we can still be able to change the estree spec.

@sosukesuzuki
Copy link
Contributor Author

@JLHwung What do you think?

@JLHwung
Copy link
Contributor

JLHwung commented Feb 5, 2024

I agree with @bradzacher, like he said the import specifiers follows the always-array pattern even if the empty braces {} are optional in the syntax.

This rule is not limited to empty braces. For example ESTree does not differentiate new A with new A() as both of them provide an empty arguments array. In this case, Prettier formats new A into new A(). Can prettier prefer import foo from "foo" over import foo from "foo" with {}?

@nzakas
Copy link
Contributor

nzakas commented Feb 5, 2024

Agree with @bradzacher and @JLHwung here. ESTree is an abstract syntax tree, which necessarily means that not every token is represented, but rather, the meaning of the syntax is represented. I think the meaning is properly conveyed with the empty array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants