Skip to content

Commit c7ed601

Browse files
Comment in and fix reportNonDefaultExport (#676)
1 parent 1ae1771 commit c7ed601

File tree

32 files changed

+342
-294
lines changed

32 files changed

+342
-294
lines changed

internal/ast/utilities.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2384,7 +2384,7 @@ func GetImpliedNodeFormatForEmitWorker(fileName string, options *core.CompilerOp
23842384
return sourceFileMetaData.ImpliedNodeFormat
23852385
}
23862386
if sourceFileMetaData != nil && sourceFileMetaData.ImpliedNodeFormat == core.ModuleKindCommonJS &&
2387-
(sourceFileMetaData.PackageJsonType != "module" ||
2387+
(sourceFileMetaData.PackageJsonType == "commonjs" ||
23882388
tspath.FileExtensionIsOneOf(fileName, []string{tspath.ExtensionCjs, tspath.ExtensionCts})) {
23892389
return core.ModuleKindCommonJS
23902390
}

internal/checker/checker.go

+44-21
Original file line numberDiff line numberDiff line change
@@ -13576,7 +13576,27 @@ func (c *Checker) getTargetOfModuleDefault(moduleSymbol *ast.Symbol, node *ast.N
1357613576
}
1357713577

1357813578
func (c *Checker) reportNonDefaultExport(moduleSymbol *ast.Symbol, node *ast.Node) {
13579-
// !!!
13579+
if moduleSymbol.Exports != nil && moduleSymbol.Exports[node.Symbol().Name] != nil {
13580+
c.error(node, diagnostics.Module_0_has_no_default_export_Did_you_mean_to_use_import_1_from_0_instead, c.symbolToString(moduleSymbol), c.symbolToString(node.Symbol()))
13581+
} else {
13582+
diagnostic := c.error(node.Name(), diagnostics.Module_0_has_no_default_export, c.symbolToString(moduleSymbol))
13583+
var exportStar *ast.Symbol
13584+
if moduleSymbol.Exports != nil {
13585+
exportStar = moduleSymbol.Exports[ast.InternalSymbolNameExportStar]
13586+
}
13587+
if exportStar != nil {
13588+
defaultExport := core.Find(exportStar.Declarations, func(decl *ast.Declaration) bool {
13589+
if !(ast.IsExportDeclaration(decl) && decl.AsExportDeclaration().ModuleSpecifier != nil) {
13590+
return false
13591+
}
13592+
resolvedExternalModuleName := c.resolveExternalModuleName(decl, decl.AsExportDeclaration().ModuleSpecifier, false /*ignoreErrors*/)
13593+
return resolvedExternalModuleName != nil && resolvedExternalModuleName.Exports[ast.InternalSymbolNameDefault] != nil
13594+
})
13595+
if defaultExport != nil {
13596+
diagnostic.AddRelatedInfo(createDiagnosticForNode(defaultExport, diagnostics.X_export_Asterisk_does_not_re_export_a_default))
13597+
}
13598+
}
13599+
}
1358013600
}
1358113601

1358213602
func (c *Checker) resolveExportByName(moduleSymbol *ast.Symbol, name string, sourceNode *ast.Node, dontResolveAlias bool) *ast.Symbol {
@@ -13773,31 +13793,29 @@ func (c *Checker) isOnlyImportableAsDefault(usage *ast.Node, resolvedModule *ast
1377313793
}
1377413794

1377513795
func (c *Checker) canHaveSyntheticDefault(file *ast.Node, moduleSymbol *ast.Symbol, dontResolveAlias bool, usage *ast.Node) bool {
13776-
// !!!
13777-
// var usageMode ResolutionMode
13778-
// if file != nil {
13779-
// usageMode = c.getEmitSyntaxForModuleSpecifierExpression(usage)
13780-
// }
13781-
// if file != nil && usageMode != core.ModuleKindNone {
13782-
// targetMode := host.getImpliedNodeFormatForEmit(file)
13783-
// if usageMode == core.ModuleKindESNext && targetMode == core.ModuleKindCommonJS && core.ModuleKindNode16 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext {
13784-
// // In Node.js, CommonJS modules always have a synthetic default when imported into ESM
13785-
// return true
13786-
// }
13787-
// if usageMode == core.ModuleKindESNext && targetMode == core.ModuleKindESNext {
13788-
// // No matter what the `module` setting is, if we're confident that both files
13789-
// // are ESM, there cannot be a synthetic default.
13790-
// return false
13791-
// }
13792-
// }
13796+
var usageMode core.ResolutionMode
13797+
if file != nil {
13798+
usageMode = c.getEmitSyntaxForModuleSpecifierExpression(usage)
13799+
}
13800+
if file != nil && usageMode != core.ModuleKindNone {
13801+
targetMode := c.program.GetImpliedNodeFormatForEmit(file.AsSourceFile())
13802+
if usageMode == core.ModuleKindESNext && targetMode == core.ModuleKindCommonJS && core.ModuleKindNode16 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext {
13803+
// In Node.js, CommonJS modules always have a synthetic default when imported into ESM
13804+
return true
13805+
}
13806+
if usageMode == core.ModuleKindESNext && targetMode == core.ModuleKindESNext {
13807+
// No matter what the `module` setting is, if we're confident that both files
13808+
// are ESM, there cannot be a synthetic default.
13809+
return false
13810+
}
13811+
}
1379313812
if !c.allowSyntheticDefaultImports {
1379413813
return false
1379513814
}
1379613815
// Declaration files (and ambient modules)
1379713816
if file == nil || file.AsSourceFile().IsDeclarationFile {
1379813817
// Definitely cannot have a synthetic default if they have a syntactic default member specified
13799-
defaultExportSymbol := c.resolveExportByName(moduleSymbol, ast.InternalSymbolNameDefault /*sourceNode*/, nil /*dontResolveAlias*/, true)
13800-
// Dont resolve alias because we want the immediately exported symbol's declaration
13818+
defaultExportSymbol := c.resolveExportByName(moduleSymbol, ast.InternalSymbolNameDefault /*sourceNode*/, nil /*dontResolveAlias*/, true) // Dont resolve alias because we want the immediately exported symbol's declaration
1380113819
if defaultExportSymbol != nil && core.Some(defaultExportSymbol.Declarations, isSyntacticDefault) {
1380213820
return false
1380313821
}
@@ -13814,7 +13832,12 @@ func (c *Checker) canHaveSyntheticDefault(file *ast.Node, moduleSymbol *ast.Symb
1381413832
return true
1381513833
}
1381613834
// TypeScript files never have a synthetic default (as they are always emitted with an __esModule marker) _unless_ they contain an export= statement
13817-
return hasExportAssignmentSymbol(moduleSymbol)
13835+
if !ast.IsInJSFile(file) {
13836+
return hasExportAssignmentSymbol(moduleSymbol)
13837+
}
13838+
13839+
// JS files have a synthetic default if they do not contain ES2015+ module syntax (export = is not valid in js) _and_ do not have an __esModule marker
13840+
return !ast.IsExternalModule(file.AsSourceFile()) && c.resolveExportByName(moduleSymbol, "__esModule", nil /*sourceNode*/, dontResolveAlias) == nil
1381813841
}
1381913842

1382013843
func (c *Checker) getEmitSyntaxForModuleSpecifierExpression(usage *ast.Node) core.ResolutionMode {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
a.ts(1,8): error TS1192: Module '"b"' has no default export.
2+
3+
4+
==== a.ts (1 errors) ====
5+
import Namespace from "./b";
6+
~~~~~~~~~
7+
!!! error TS1192: Module '"b"' has no default export.
8+
export var x = new Namespace.Foo();
9+
10+
==== b.ts (0 errors) ====
11+
export class Foo {
12+
member: string;
13+
}
14+

testdata/baselines/reference/submodule/compiler/allowSyntheticDefaultImports3.errors.txt.diff

-19
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
service.ts(1,8): error TS2613: Module '"db"' has no default export. Did you mean to use 'import { db } from "db"' instead?
2+
3+
4+
==== db.ts (0 errors) ====
5+
export class db {
6+
public doSomething() {
7+
}
8+
}
9+
10+
==== service.ts (1 errors) ====
11+
import db from './db'; // error no default export
12+
~~
13+
!!! error TS2613: Module '"db"' has no default export. Did you mean to use 'import { db } from "db"' instead?
14+
function someDecorator(target) {
15+
return target;
16+
}
17+
@someDecorator
18+
class MyClass {
19+
db: db.db;
20+
21+
constructor(db: db.db) {
22+
this.db = db;
23+
this.db.doSomething();
24+
}
25+
}
26+
export {MyClass};
27+

testdata/baselines/reference/submodule/compiler/decoratorMetadataWithImportDeclarationNameCollision4.errors.txt.diff

-32
This file was deleted.

testdata/baselines/reference/submodule/compiler/decoratorMetadataWithImportDeclarationNameCollision4.types

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ class MyClass {
2727
>MyClass : MyClass
2828

2929
db: db.db;
30-
>db : error
30+
>db : db
3131
>db : any
3232

3333
constructor(db: db.db) {
34-
>db : error
34+
>db : db
3535
>db : any
3636

3737
this.db = db;
38-
>this.db = db : error
39-
>this.db : error
38+
>this.db = db : db
39+
>this.db : db
4040
>this : this
4141
>db : db
42-
>db : error
42+
>db : db
4343

4444
this.db.doSomething();
4545
>this.db.doSomething() : any

testdata/baselines/reference/submodule/compiler/decoratorMetadataWithImportDeclarationNameCollision4.types.diff

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55

66
db: db.db;
77
->db : db.db
8-
+>db : error
8+
+>db : db
99
>db : any
1010

1111
constructor(db: db.db) {
1212
->db : db.db
13-
+>db : error
13+
+>db : db
1414
>db : any
1515

1616
this.db = db;
1717
->this.db = db : db.db
1818
->this.db : db.db
19-
+>this.db = db : error
20-
+>this.db : error
19+
+>this.db = db : db
20+
+>this.db : db
2121
>this : this
2222
->db : db.db
2323
->db : db.db
2424
+>db : db
25-
+>db : error
25+
+>db : db
2626

2727
this.db.doSomething();
2828
>this.db.doSomething() : any
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
client.ts(1,8): error TS1192: Module '"server"' has no default export.
2+
client.ts(2,8): error TS1192: Module '"server"' has no default export.
3+
client.ts(4,8): error TS1192: Module '"server"' has no default export.
4+
client.ts(6,8): error TS1192: Module '"server"' has no default export.
5+
client.ts(9,8): error TS1192: Module '"server"' has no default export.
6+
client.ts(11,8): error TS1192: Module '"server"' has no default export.
7+
8+
9+
==== server.ts (0 errors) ====
10+
export class a { }
11+
export class x { }
12+
export class m { }
13+
export class a11 { }
14+
export class a12 { }
15+
export class x11 { }
16+
17+
==== client.ts (6 errors) ====
18+
import defaultBinding1, { } from "./server";
19+
~~~~~~~~~~~~~~~
20+
!!! error TS1192: Module '"server"' has no default export.
21+
import defaultBinding2, { a } from "./server";
22+
~~~~~~~~~~~~~~~
23+
!!! error TS1192: Module '"server"' has no default export.
24+
export var x1 = new a();
25+
import defaultBinding3, { a11 as b } from "./server";
26+
~~~~~~~~~~~~~~~
27+
!!! error TS1192: Module '"server"' has no default export.
28+
export var x2 = new b();
29+
import defaultBinding4, { x, a12 as y } from "./server";
30+
~~~~~~~~~~~~~~~
31+
!!! error TS1192: Module '"server"' has no default export.
32+
export var x4 = new x();
33+
export var x5 = new y();
34+
import defaultBinding5, { x11 as z, } from "./server";
35+
~~~~~~~~~~~~~~~
36+
!!! error TS1192: Module '"server"' has no default export.
37+
export var x3 = new z();
38+
import defaultBinding6, { m, } from "./server";
39+
~~~~~~~~~~~~~~~
40+
!!! error TS1192: Module '"server"' has no default export.
41+
export var x6 = new m();
42+

testdata/baselines/reference/submodule/compiler/es6ImportDefaultBindingFollowedWithNamedImportDts.errors.txt.diff

-47
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts(1,8): error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
2+
es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts(2,8): error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
3+
es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts(4,8): error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
4+
es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts(6,8): error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
5+
es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts(9,8): error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
6+
es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts(11,8): error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
7+
8+
9+
==== es6ImportDefaultBindingFollowedWithNamedImportInEs5_0.ts (0 errors) ====
10+
export var a = 10;
11+
export var x = a;
12+
export var m = a;
13+
14+
==== es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts (6 errors) ====
15+
import defaultBinding1, { } from "./es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
16+
~~~~~~~~~~~~~~~
17+
!!! error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
18+
import defaultBinding2, { a } from "./es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
19+
~~~~~~~~~~~~~~~
20+
!!! error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
21+
var x1: number = a;
22+
import defaultBinding3, { a as b } from "./es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
23+
~~~~~~~~~~~~~~~
24+
!!! error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
25+
var x1: number = b;
26+
import defaultBinding4, { x, a as y } from "./es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
27+
~~~~~~~~~~~~~~~
28+
!!! error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
29+
var x1: number = x;
30+
var x1: number = y;
31+
import defaultBinding5, { x as z, } from "./es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
32+
~~~~~~~~~~~~~~~
33+
!!! error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
34+
var x1: number = z;
35+
import defaultBinding6, { m, } from "./es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
36+
~~~~~~~~~~~~~~~
37+
!!! error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
38+
var x1: number = m;
39+

0 commit comments

Comments
 (0)