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 2 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
69 changes: 48 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[ast.InternalSymbolNameExportEquals] != 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,33 @@ 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 {
sourceFileMetaData := c.program.GetSourceFileMetaData(file.AsSourceFile().Path())
var targetMode core.ModuleKind
if sourceFileMetaData != nil {
targetMode = sourceFileMetaData.ImpliedNodeFormat
}
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 +13836,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 file.AsSourceFile().ExternalModuleIndicator == nil /* || file.ExternalModuleIndicator == true) */ && 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
@@ -1,32 +1,17 @@
--- old.decoratorMetadataWithImportDeclarationNameCollision4.errors.txt
+++ new.decoratorMetadataWithImportDeclarationNameCollision4.errors.txt
@@= skipped -0, +-1 lines =@@
@@= skipped -0, +0 lines =@@
-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
- ~~
+service.ts(1,8): error TS1192: Module '"db"' has no default export.


==== db.ts (0 errors) ====
@@= skipped -9, +9 lines =@@
==== 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};
-
@@= skipped --1, +1 lines =@@
+<no content>
+!!! error TS1192: Module '"db"' has no default export.
function someDecorator(target) {
return target;
}
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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
client.ts(1,1): error TS1191: An import declaration cannot have modifiers.
client.ts(1,15): error TS1192: Module '"server"' has no default export.


==== server.ts (0 errors) ====
export var a = 10;

==== client.ts (1 errors) ====
==== client.ts (2 errors) ====
export import defaultBinding, * as nameSpaceBinding from "./server";
~~~~~~
!!! error TS1191: An import declaration cannot have modifiers.
~~~~~~~~~~~~~~
!!! error TS1192: Module '"server"' has no default export.
export var x: number = nameSpaceBinding.a;

This file was deleted.

Loading
Loading