镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

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).

Activity

  1. fatcerberus commented on Dec 9, 2021

    @fatcerberus

    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.”

  2. vicente-s commented on Dec 21, 2021

    @vicente-s

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

  3. lokicodedaily commented on Jan 2, 2022

    @lokicodedaily

    can I work on this issue?

  4. sandersn commented on Jan 11, 2022

    @sandersn
    MemberAuthor

    Lokesh Digari (@lokicodedaily) sure, although you might want to co-ordinate with Vicente Soriano (@vicente-s).

  5. sandersn commented on Jan 11, 2022

    @sandersn
    MemberAuthor

    Bruce Pascoe (@fatcerberus) Good point. I updated the Expected section.

  6. islandryu commented on Jan 17, 2022

    @islandryu
    Contributor

    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.

  7. sandersn commented on Jan 26, 2022

    @sandersn
    MemberAuthor

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

  8. DanielRosenwasser commented on Feb 9, 2022

    @DanielRosenwasser
    Member

    Thank you islandryu!

  9. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingFixedA PR has been merged for this issueGood First IssueWell scoped, documented and has the green lightHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions