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

Improve non-top-level import/export error, especially for JS #47076

Closed
sandersn opened this issue Dec 9, 2021 · 8 comments · Fixed by #47087
Closed

Improve non-top-level import/export error, especially for JS #47076

sandersn opened this issue Dec 9, 2021 · 8 comments · Fixed by #47087
Labels
Bug Domain: Error Messages Fixed Good First Issue Help Wanted

Comments

@sandersn
Copy link
Member

@sandersn sandersn commented Dec 9, 2021

ES imports and exports can only be used at the top level of a module. This is illegal:

function container() {
  import 'fs'
  export { container }
  namespace N { }
}

The current errors for these three statements are vague and, for JS, contain irrelevant terms:

Actual:

(1) "An import declaration can only be used in a namespace or module."
(2) "An export declaration can only be used in a module."
(3) "A namespace declaration is only allowed in a namespace or module."

Expected:

(1) When the node is in a JS file, "An import declaration can only be used at the top level of a module."
Otherwise, "An import declaration can only be used at the top level of a namespace or module."
(2) When the node is in a JS file, "An export declaration can only be used at the top level of a module."
Otherwise, "An export declaration can only be used at the top level of a namespace or module."
(3) "A namespace declaration is only allowed at the top level of a module."

Implementation:
checkGrammarModuleElementContext issues these errors. I don't know whether it's better to make it smarter or just avoid calling it for case (1).

@sandersn sandersn added Bug Good First Issue Help Wanted labels Dec 9, 2021
@sandersn sandersn added this to the Backlog milestone Dec 9, 2021
@fatcerberus
Copy link

@fatcerberus fatcerberus commented Dec 9, 2021

Isn’t export used inside namespaces too? So (2) should probably be “An export declaration can only be used at the top level of module or namespace.”

@vicente-s
Copy link

@vicente-s vicente-s commented Dec 21, 2021

Hey everyone, I'd like to take on this issue.

@lokicodedaily
Copy link

@lokicodedaily lokicodedaily commented Jan 2, 2022

can I work on this issue?

@sandersn
Copy link
Member Author

@sandersn sandersn commented Jan 11, 2022

@lokicodedaily sure, although you might want to co-ordinate with @vicente-s.

@sandersn
Copy link
Member Author

@sandersn sandersn commented Jan 11, 2022

@fatcerberus Good point. I updated the Expected section.

@islandryu
Copy link
Contributor

@islandryu islandryu commented Jan 17, 2022

@sandersn
I tried to fix it in #47087, but did I do something wrong in the PR procedure?
I would be grateful for any feedback.

@sandersn
Copy link
Member Author

@sandersn sandersn commented Jan 26, 2022

@islandryu I'm backlogged on community PRs. I'll review it when I can.

@DanielRosenwasser DanielRosenwasser removed this from the Backlog milestone Feb 9, 2022
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 4.6.1 milestone Feb 9, 2022
@DanielRosenwasser
Copy link
Member

@DanielRosenwasser DanielRosenwasser commented Feb 9, 2022

Thank you @islandryu!

@DanielRosenwasser DanielRosenwasser added Domain: Error Messages Fixed labels Feb 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Domain: Error Messages Fixed Good First Issue Help Wanted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants