Skip to content

Comment in and fix reportNonDefaultExport #676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 19, 2025
Merged
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
2 changes: 1 addition & 1 deletion internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -2384,7 +2384,7 @@ func GetImpliedNodeFormatForEmitWorker(fileName string, options *core.CompilerOp
return sourceFileMetaData.ImpliedNodeFormat
}
if sourceFileMetaData != nil && sourceFileMetaData.ImpliedNodeFormat == core.ModuleKindCommonJS &&
(sourceFileMetaData.PackageJsonType != "module" ||
(sourceFileMetaData.PackageJsonType == "commonjs" ||
tspath.FileExtensionIsOneOf(fileName, []string{tspath.ExtensionCjs, tspath.ExtensionCts})) {
return core.ModuleKindCommonJS
}
Expand Down
65 changes: 44 additions & 21 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13576,7 +13576,27 @@ func (c *Checker) getTargetOfModuleDefault(moduleSymbol *ast.Symbol, node *ast.N
}

func (c *Checker) reportNonDefaultExport(moduleSymbol *ast.Symbol, node *ast.Node) {
// !!!
if moduleSymbol.Exports != nil && moduleSymbol.Exports[node.Symbol().Name] != nil {
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()))
} else {
diagnostic := c.error(node.Name(), diagnostics.Module_0_has_no_default_export, c.symbolToString(moduleSymbol))
var exportStar *ast.Symbol
if moduleSymbol.Exports != nil {
exportStar = moduleSymbol.Exports[ast.InternalSymbolNameExportStar]
}
if exportStar != nil {
defaultExport := core.Find(exportStar.Declarations, func(decl *ast.Declaration) bool {
if !(ast.IsExportDeclaration(decl) && decl.AsExportDeclaration().ModuleSpecifier != nil) {
return false
}
resolvedExternalModuleName := c.resolveExternalModuleName(decl, decl.AsExportDeclaration().ModuleSpecifier, false /*ignoreErrors*/)
return resolvedExternalModuleName != nil && resolvedExternalModuleName.Exports[ast.InternalSymbolNameDefault] != nil
})
if defaultExport != nil {
diagnostic.AddRelatedInfo(createDiagnosticForNode(defaultExport, diagnostics.X_export_Asterisk_does_not_re_export_a_default))
}
}
}
}

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

func (c *Checker) canHaveSyntheticDefault(file *ast.Node, moduleSymbol *ast.Symbol, dontResolveAlias bool, usage *ast.Node) bool {
// !!!
// var usageMode ResolutionMode
// if file != nil {
// usageMode = c.getEmitSyntaxForModuleSpecifierExpression(usage)
// }
// if file != nil && usageMode != core.ModuleKindNone {
// targetMode := host.getImpliedNodeFormatForEmit(file)
// if usageMode == core.ModuleKindESNext && targetMode == core.ModuleKindCommonJS && core.ModuleKindNode16 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext {
// // In Node.js, CommonJS modules always have a synthetic default when imported into ESM
// return true
// }
// if usageMode == core.ModuleKindESNext && targetMode == core.ModuleKindESNext {
// // No matter what the `module` setting is, if we're confident that both files
// // are ESM, there cannot be a synthetic default.
// return false
// }
// }
var usageMode core.ResolutionMode
if file != nil {
usageMode = c.getEmitSyntaxForModuleSpecifierExpression(usage)
}
if file != nil && usageMode != core.ModuleKindNone {
targetMode := c.program.GetImpliedNodeFormatForEmit(file.AsSourceFile())
if usageMode == core.ModuleKindESNext && targetMode == core.ModuleKindCommonJS && core.ModuleKindNode16 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext {
// In Node.js, CommonJS modules always have a synthetic default when imported into ESM
return true
}
if usageMode == core.ModuleKindESNext && targetMode == core.ModuleKindESNext {
// No matter what the `module` setting is, if we're confident that both files
// are ESM, there cannot be a synthetic default.
return false
}
}
if !c.allowSyntheticDefaultImports {
return false
}
// Declaration files (and ambient modules)
if file == nil || file.AsSourceFile().IsDeclarationFile {
// Definitely cannot have a synthetic default if they have a syntactic default member specified
defaultExportSymbol := c.resolveExportByName(moduleSymbol, ast.InternalSymbolNameDefault /*sourceNode*/, nil /*dontResolveAlias*/, true)
// Dont resolve alias because we want the immediately exported symbol's declaration
defaultExportSymbol := c.resolveExportByName(moduleSymbol, ast.InternalSymbolNameDefault /*sourceNode*/, nil /*dontResolveAlias*/, true) // Dont resolve alias because we want the immediately exported symbol's declaration
if defaultExportSymbol != nil && core.Some(defaultExportSymbol.Declarations, isSyntacticDefault) {
return false
}
Expand All @@ -13814,7 +13832,12 @@ func (c *Checker) canHaveSyntheticDefault(file *ast.Node, moduleSymbol *ast.Symb
return true
}
// TypeScript files never have a synthetic default (as they are always emitted with an __esModule marker) _unless_ they contain an export= statement
return hasExportAssignmentSymbol(moduleSymbol)
if !ast.IsInJSFile(file) {
return hasExportAssignmentSymbol(moduleSymbol)
}

// 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
return !ast.IsExternalModule(file.AsSourceFile()) && c.resolveExportByName(moduleSymbol, "__esModule", nil /*sourceNode*/, dontResolveAlias) == nil
}

func (c *Checker) getEmitSyntaxForModuleSpecifierExpression(usage *ast.Node) core.ResolutionMode {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
a.ts(1,8): error TS1192: Module '"b"' has no default export.


==== a.ts (1 errors) ====
import Namespace from "./b";
~~~~~~~~~
!!! error TS1192: Module '"b"' has no default export.
export var x = new Namespace.Foo();

==== b.ts (0 errors) ====
export class Foo {
member: string;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
service.ts(1,8): error TS2613: Module '"db"' has no default export. Did you mean to use 'import { db } from "db"' instead?


==== db.ts (0 errors) ====
export class db {
public doSomething() {
}
}

==== service.ts (1 errors) ====
import db from './db'; // error no default export
~~
!!! error TS2613: Module '"db"' has no default export. Did you mean to use 'import { db } from "db"' instead?
function someDecorator(target) {
return target;
}
@someDecorator
class MyClass {
db: db.db;

constructor(db: db.db) {
this.db = db;
this.db.doSomething();
}
}
export {MyClass};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ class MyClass {
>MyClass : MyClass

db: db.db;
>db : error
>db : db
>db : any

constructor(db: db.db) {
>db : error
>db : db
>db : any

this.db = db;
>this.db = db : error
>this.db : error
>this.db = db : db
>this.db : db
>this : this
>db : db
>db : error
>db : db

this.db.doSomething();
>this.db.doSomething() : any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@

db: db.db;
->db : db.db
+>db : error
+>db : db
>db : any

constructor(db: db.db) {
->db : db.db
+>db : error
+>db : db
>db : any

this.db = db;
->this.db = db : db.db
->this.db : db.db
+>this.db = db : error
+>this.db : error
+>this.db = db : db
+>this.db : db
>this : this
->db : db.db
->db : db.db
+>db : db
+>db : error
+>db : db

this.db.doSomething();
>this.db.doSomething() : any
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
client.ts(1,8): error TS1192: Module '"server"' has no default export.
client.ts(2,8): error TS1192: Module '"server"' has no default export.
client.ts(4,8): error TS1192: Module '"server"' has no default export.
client.ts(6,8): error TS1192: Module '"server"' has no default export.
client.ts(9,8): error TS1192: Module '"server"' has no default export.
client.ts(11,8): error TS1192: Module '"server"' has no default export.


==== server.ts (0 errors) ====
export class a { }
export class x { }
export class m { }
export class a11 { }
export class a12 { }
export class x11 { }

==== client.ts (6 errors) ====
import defaultBinding1, { } from "./server";
~~~~~~~~~~~~~~~
!!! error TS1192: Module '"server"' has no default export.
import defaultBinding2, { a } from "./server";
~~~~~~~~~~~~~~~
!!! error TS1192: Module '"server"' has no default export.
export var x1 = new a();
import defaultBinding3, { a11 as b } from "./server";
~~~~~~~~~~~~~~~
!!! error TS1192: Module '"server"' has no default export.
export var x2 = new b();
import defaultBinding4, { x, a12 as y } from "./server";
~~~~~~~~~~~~~~~
!!! error TS1192: Module '"server"' has no default export.
export var x4 = new x();
export var x5 = new y();
import defaultBinding5, { x11 as z, } from "./server";
~~~~~~~~~~~~~~~
!!! error TS1192: Module '"server"' has no default export.
export var x3 = new z();
import defaultBinding6, { m, } from "./server";
~~~~~~~~~~~~~~~
!!! error TS1192: Module '"server"' has no default export.
export var x6 = new m();

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts(1,8): error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts(2,8): error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts(4,8): error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts(6,8): error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts(9,8): error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts(11,8): error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.


==== es6ImportDefaultBindingFollowedWithNamedImportInEs5_0.ts (0 errors) ====
export var a = 10;
export var x = a;
export var m = a;

==== es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts (6 errors) ====
import defaultBinding1, { } from "./es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
~~~~~~~~~~~~~~~
!!! error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
import defaultBinding2, { a } from "./es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
~~~~~~~~~~~~~~~
!!! error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
var x1: number = a;
import defaultBinding3, { a as b } from "./es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
~~~~~~~~~~~~~~~
!!! error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
var x1: number = b;
import defaultBinding4, { x, a as y } from "./es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
~~~~~~~~~~~~~~~
!!! error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
var x1: number = x;
var x1: number = y;
import defaultBinding5, { x as z, } from "./es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
~~~~~~~~~~~~~~~
!!! error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
var x1: number = z;
import defaultBinding6, { m, } from "./es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
~~~~~~~~~~~~~~~
!!! error TS1192: Module '"es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"' has no default export.
var x1: number = m;

Loading