Skip to content

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

Closed
@sandersn

Description

@sandersn

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

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