Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5278,6 +5278,7 @@ func (c *Checker) checkImportDeclaration(node *ast.ImportDeclarationNode) {
}
if c.checkGrammarModuleElementContext(node, diagnostic) {
// If we hit an import declaration in an illegal context, just bail out to avoid cascading errors.
c.checkExternalModuleNameInGlobalScope(node)
return
}
if !c.checkGrammarModifiers(node) && node.Modifiers() != nil {
Expand Down Expand Up @@ -5468,6 +5469,7 @@ func (c *Checker) checkImportEqualsDeclaration(node *ast.Node) {
diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_module,
diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_namespace_or_module)
if c.checkGrammarModuleElementContext(node, diagnostic) {
c.checkExternalModuleNameInGlobalScope(node)
return // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors.
}
c.checkGrammarModifiers(node)
Expand Down Expand Up @@ -5510,6 +5512,7 @@ func (c *Checker) checkExportDeclaration(node *ast.ExportDeclarationNode) {
diagnostics.An_export_declaration_can_only_be_used_at_the_top_level_of_a_module,
diagnostics.An_export_declaration_can_only_be_used_at_the_top_level_of_a_namespace_or_module)
if c.checkGrammarModuleElementContext(node, diagnostic) {
c.checkExternalModuleNameInGlobalScope(node)
return // If we hit an export in an illegal context, just bail out to avoid cascading errors.
}
exportDecl := node.AsExportDeclaration()
Expand Down Expand Up @@ -5553,6 +5556,15 @@ func (c *Checker) checkExportDeclaration(node *ast.ExportDeclarationNode) {
c.checkImportAttributes(node)
}

func (c *Checker) checkExternalModuleNameInGlobalScope(node *ast.Node) {
if getEnclosingContainer(node).Kind != ast.KindSourceFile || (ast.IsImportDeclarationOrJSImportDeclaration(node) && node.ImportClause() == nil) {
return
}
if moduleName := ast.GetExternalModuleName(node); moduleName != nil {
c.resolveExternalModuleName(node, moduleName, false)
}
}

func (c *Checker) checkExportSpecifier(node *ast.ExportSpecifierNode) {
c.checkAliasSymbol(node)
hasModuleSpecifier := node.Parent.Parent.ModuleSpecifier() != nil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
moduleDeclarationsInNonScopeBlock.ts(2,5): error TS1233: An export declaration can only be used at the top level of a namespace or module.
moduleDeclarationsInNonScopeBlock.ts(2,23): error TS2307: Cannot find module 'exportNamed' or its corresponding type declarations.
moduleDeclarationsInNonScopeBlock.ts(3,5): error TS1233: An export declaration can only be used at the top level of a namespace or module.
moduleDeclarationsInNonScopeBlock.ts(3,19): error TS2307: Cannot find module 'exportStar' or its corresponding type declarations.
moduleDeclarationsInNonScopeBlock.ts(4,5): error TS1232: An import declaration can only be used at the top level of a namespace or module.
moduleDeclarationsInNonScopeBlock.ts(4,23): error TS2307: Cannot find module 'importNamed' or its corresponding type declarations.
moduleDeclarationsInNonScopeBlock.ts(5,5): error TS1232: An import declaration can only be used at the top level of a namespace or module.
moduleDeclarationsInNonScopeBlock.ts(5,24): error TS2307: Cannot find module 'importEquals' or its corresponding type declarations.
moduleDeclarationsInNonScopeBlock.ts(6,5): error TS1232: An import declaration can only be used at the top level of a namespace or module.


==== moduleDeclarationsInNonScopeBlock.ts (9 errors) ====
{
export { a } from "exportNamed";
~~~~~~
!!! error TS1233: An export declaration can only be used at the top level of a namespace or module.
~~~~~~~~~~~~~
!!! error TS2307: Cannot find module 'exportNamed' or its corresponding type declarations.
export * from "exportStar";
~~~~~~
!!! error TS1233: An export declaration can only be used at the top level of a namespace or module.
~~~~~~~~~~~~
!!! error TS2307: Cannot find module 'exportStar' or its corresponding type declarations.
import { b } from "importNamed";
~~~~~~
!!! error TS1232: An import declaration can only be used at the top level of a namespace or module.
~~~~~~~~~~~~~
!!! error TS2307: Cannot find module 'importNamed' or its corresponding type declarations.
import c = require("importEquals");
~~~~~~
!!! error TS1232: An import declaration can only be used at the top level of a namespace or module.
~~~~~~~~~~~~~~
!!! error TS2307: Cannot find module 'importEquals' or its corresponding type declarations.
import "sideEffect";
~~~~~~
!!! error TS1232: An import declaration can only be used at the top level of a namespace or module.
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [tests/cases/compiler/moduleDeclarationsInNonScopeBlock.ts] ////

//// [moduleDeclarationsInNonScopeBlock.ts]
{
export { a } from "exportNamed";
export * from "exportStar";
import { b } from "importNamed";
import c = require("importEquals");
import "sideEffect";
}


//// [moduleDeclarationsInNonScopeBlock.js]
"use strict";
{
export { a } from "exportNamed";
export * from "exportStar";
import { b } from "importNamed";
import c = require("importEquals");
import "sideEffect";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [tests/cases/compiler/moduleDeclarationsInNonScopeBlock.ts] ////

=== moduleDeclarationsInNonScopeBlock.ts ===
{
export { a } from "exportNamed";
>a : Symbol(a, Decl(moduleDeclarationsInNonScopeBlock.ts, 1, 12))

export * from "exportStar";
import { b } from "importNamed";
>b : Symbol(b, Decl(moduleDeclarationsInNonScopeBlock.ts, 3, 12))

import c = require("importEquals");
>c : Symbol(c, Decl(moduleDeclarationsInNonScopeBlock.ts, 3, 36))

import "sideEffect";
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [tests/cases/compiler/moduleDeclarationsInNonScopeBlock.ts] ////

=== moduleDeclarationsInNonScopeBlock.ts ===
{
export { a } from "exportNamed";
>a : any

export * from "exportStar";
import { b } from "importNamed";
>b : any

import c = require("importEquals");
>c : any

import "sideEffect";
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ moduleElementsInWrongContext.ts(21,5): error TS1184: Modifiers cannot appear her
moduleElementsInWrongContext.ts(22,5): error TS1184: Modifiers cannot appear here.
moduleElementsInWrongContext.ts(23,5): error TS1232: An import declaration can only be used at the top level of a namespace or module.
moduleElementsInWrongContext.ts(24,5): error TS1232: An import declaration can only be used at the top level of a namespace or module.
moduleElementsInWrongContext.ts(24,25): error TS2307: Cannot find module 'foo' or its corresponding type declarations.
moduleElementsInWrongContext.ts(25,5): error TS1232: An import declaration can only be used at the top level of a namespace or module.
moduleElementsInWrongContext.ts(26,5): error TS1232: An import declaration can only be used at the top level of a namespace or module.
moduleElementsInWrongContext.ts(27,5): error TS1232: An import declaration can only be used at the top level of a namespace or module.
moduleElementsInWrongContext.ts(28,5): error TS1232: An import declaration can only be used at the top level of a namespace or module.


==== moduleElementsInWrongContext.ts (17 errors) ====
==== moduleElementsInWrongContext.ts (18 errors) ====
{
namespace M { }
~~~~~~~~~
Expand Down Expand Up @@ -68,6 +69,8 @@ moduleElementsInWrongContext.ts(28,5): error TS1232: An import declaration can o
import I2 = require("foo");
~~~~~~
!!! error TS1232: An import declaration can only be used at the top level of a namespace or module.
~~~~~
!!! error TS2307: Cannot find module 'foo' or its corresponding type declarations.
import * as Foo from "ambient";
~~~~~~
!!! error TS1232: An import declaration can only be used at the top level of a namespace or module.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--- old.moduleElementsInWrongContext.errors.txt
+++ new.moduleElementsInWrongContext.errors.txt
@@= skipped -10, +10 lines =@@
moduleElementsInWrongContext.ts(22,5): error TS1184: Modifiers cannot appear here.
moduleElementsInWrongContext.ts(23,5): error TS1232: An import declaration can only be used at the top level of a namespace or module.
moduleElementsInWrongContext.ts(24,5): error TS1232: An import declaration can only be used at the top level of a namespace or module.
+moduleElementsInWrongContext.ts(24,25): error TS2307: Cannot find module 'foo' or its corresponding type declarations.
moduleElementsInWrongContext.ts(25,5): error TS1232: An import declaration can only be used at the top level of a namespace or module.
moduleElementsInWrongContext.ts(26,5): error TS1232: An import declaration can only be used at the top level of a namespace or module.
moduleElementsInWrongContext.ts(27,5): error TS1232: An import declaration can only be used at the top level of a namespace or module.
moduleElementsInWrongContext.ts(28,5): error TS1232: An import declaration can only be used at the top level of a namespace or module.


-==== moduleElementsInWrongContext.ts (17 errors) ====
+==== moduleElementsInWrongContext.ts (18 errors) ====
{
namespace M { }
~~~~~~~~~
@@= skipped -57, +58 lines =@@
import I2 = require("foo");
~~~~~~
!!! error TS1232: An import declaration can only be used at the top level of a namespace or module.
+ ~~~~~
+!!! error TS2307: Cannot find module 'foo' or its corresponding type declarations.
import * as Foo from "ambient";
~~~~~~
!!! error TS1232: An import declaration can only be used at the top level of a namespace or module.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
export { a } from "exportNamed";
export * from "exportStar";
import { b } from "importNamed";
import c = require("importEquals");
import "sideEffect";
}