Repository navigation
Improve non-top-level import/export error, especially for JS #47076
Copy link
Copy link
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingThe issue relates to error messagingFixedA PR has been merged for this issueA PR has been merged for this issueGood First IssueWell scoped, documented and has the green lightWell scoped, documented and has the green lightHelp WantedYou can do thisYou can do this
Milestone
Description
Activity
- addedBugA bug in TypeScriptA bug in TypeScriptGood First IssueWell scoped, documented and has the green lightWell scoped, documented and has the green lightHelp WantedYou can do thisYou can do this
on Dec 9, 2021 Isn’t
exportused inside namespaces too? So (2) should probably be “An export declaration can only be used at the top level of module or namespace.”Hey everyone, I'd like to take on this issue.
can I work on this issue?
sandersn commented
on Jan 11, 2022 MemberAuthorMore actionsLokesh Digari (@lokicodedaily) sure, although you might want to co-ordinate with Vicente Soriano (@vicente-s).
sandersn commented
on Jan 11, 2022 MemberAuthorMore actionsBruce Pascoe (@fatcerberus) Good point. I updated the Expected section.
Nathan Shively-Sanders (@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 commented
on Jan 26, 2022 MemberAuthorMore actionsislandryu I'm backlogged on community PRs. I'll review it when I can.
Reacted by Omri LuzonReacted by islandryuDanielRosenwasser commented
on Feb 9, 2022 MemberMore actionsThank you islandryu!
- addedDomain: Error MessagesThe issue relates to error messagingThe issue relates to error messagingFixedA PR has been merged for this issueA PR has been merged for this issue
on Feb 9, 2022 - locked as resolved and limited conversation to collaborators
on Oct 22, 2025
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingThe issue relates to error messagingFixedA PR has been merged for this issueA PR has been merged for this issueGood First IssueWell scoped, documented and has the green lightWell scoped, documented and has the green lightHelp WantedYou can do thisYou can do this
ES imports and exports can only be used at the top level of a module. This is illegal:
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:
checkGrammarModuleElementContextissues these errors. I don't know whether it's better to make it smarter or just avoid calling it for case (1).