Skip to content

Commit 82039b6

Browse files
authored
Only export @typedef type aliases in modules (#1999)
1 parent 6fb55b7 commit 82039b6

File tree

156 files changed

+660
-1267
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

156 files changed

+660
-1267
lines changed

internal/ast/ast.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2281,6 +2281,7 @@ func declarationIsWriteAccess(decl *Node) bool {
22812281
KindParameter,
22822282
KindShorthandPropertyAssignment,
22832283
KindTypeAliasDeclaration,
2284+
KindJSTypeAliasDeclaration,
22842285
KindTypeParameter:
22852286
return true
22862287

@@ -5139,7 +5140,7 @@ func (node *ExportAssignment) Clone(f NodeFactoryCoercible) *Node {
51395140
}
51405141

51415142
func (node *ExportAssignment) computeSubtreeFacts() SubtreeFacts {
5142-
return propagateModifierListSubtreeFacts(node.modifiers) | propagateSubtreeFacts(node.Type) | propagateSubtreeFacts(node.Expression)
5143+
return propagateModifierListSubtreeFacts(node.modifiers) | propagateSubtreeFacts(node.Type) | propagateSubtreeFacts(node.Expression) | core.IfElse(node.IsExportEquals, SubtreeContainsTypeScript, SubtreeFactsNone)
51435144
}
51445145

51455146
func IsExportAssignment(node *Node) bool {

internal/ast/utilities.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3894,3 +3894,7 @@ func IsExpandoInitializer(initializer *Node) bool {
38943894
func GetContainingFunction(node *Node) *Node {
38953895
return FindAncestor(node.Parent, IsFunctionLike)
38963896
}
3897+
3898+
func IsImplicitlyExportedJSTypeAlias(node *Node) bool {
3899+
return IsJSTypeAliasDeclaration(node) && IsSourceFile(node.Parent) && IsExternalOrCommonJSModule(node.Parent.AsSourceFile())
3900+
}

internal/binder/binder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ func (b *Binder) declareModuleMember(node *ast.Node, symbolFlags ast.SymbolFlags
380380
if node.Kind == ast.KindCommonJSExport {
381381
container = b.file.AsNode()
382382
}
383-
hasExportModifier := ast.GetCombinedModifierFlags(node)&ast.ModifierFlagsExport != 0
383+
hasExportModifier := ast.GetCombinedModifierFlags(node)&ast.ModifierFlagsExport != 0 || ast.IsImplicitlyExportedJSTypeAlias(node)
384384
if symbolFlags&ast.SymbolFlagsAlias != 0 {
385385
if node.Kind == ast.KindExportSpecifier || (node.Kind == ast.KindImportEqualsDeclaration && hasExportModifier) {
386386
return b.declareSymbol(ast.GetExports(container.Symbol()), container.Symbol(), node, symbolFlags, symbolExcludes)

internal/checker/checker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6805,7 +6805,7 @@ func (c *Checker) checkUnusedClassMembers(node *ast.Node) {
68056805
c.reportUnused(parameter, UnusedKindLocal, NewDiagnosticForNode(parameter.Name(), diagnostics.Property_0_is_declared_but_its_value_is_never_read, ast.SymbolName(parameter.Symbol())))
68066806
}
68076807
}
6808-
case ast.KindIndexSignature, ast.KindSemicolonClassElement, ast.KindClassStaticBlockDeclaration:
6808+
case ast.KindIndexSignature, ast.KindSemicolonClassElement, ast.KindClassStaticBlockDeclaration, ast.KindJSTypeAliasDeclaration:
68096809
// Can't be private
68106810
default:
68116811
panic("Unhandled case in checkUnusedClassMembers")

internal/checker/emitresolver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ func (r *EmitResolver) determineIfDeclarationIsVisible(node *ast.Node) bool {
155155
}
156156
// falls through
157157
}
158-
// external module augmentation is always visible
159-
if ast.IsExternalModuleAugmentation(node) {
158+
// External module augmentation is always visible
159+
// A @typedef at top-level in an external module is always visible
160+
if ast.IsExternalModuleAugmentation(node) || ast.IsImplicitlyExportedJSTypeAlias(node) {
160161
return true
161162
}
162163
parent := ast.GetDeclarationContainer(node)

internal/ls/importTracker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ func getImportOrExportSymbol(node *ast.Node, symbol *ast.Symbol, checker *checke
502502
} else {
503503
exportNode := getExportNode(parent, node)
504504
switch {
505-
case exportNode != nil && ast.HasSyntacticModifier(exportNode, ast.ModifierFlagsExport):
505+
case exportNode != nil && (ast.HasSyntacticModifier(exportNode, ast.ModifierFlagsExport) || ast.IsImplicitlyExportedJSTypeAlias(exportNode)):
506506
if ast.IsImportEqualsDeclaration(exportNode) && exportNode.AsImportEqualsDeclaration().ModuleReference == node {
507507
// We're at `Y` in `export import X = Y`. This is not the exported symbol, the left-hand-side is. So treat this as an import statement.
508508
if comingFromExport {

internal/parser/reparser.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,11 @@ func (p *Parser) reparseTags(parent *ast.Node, jsDoc []*ast.Node) {
7070
func (p *Parser) reparseUnhosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node) {
7171
switch tag.Kind {
7272
case ast.KindJSDocTypedefTag:
73-
// !!! Don't mark typedefs as exported if they are not in a module
7473
typeExpression := tag.AsJSDocTypedefTag().TypeExpression
7574
if typeExpression == nil {
7675
break
7776
}
78-
export := p.factory.NewModifier(ast.KindExportKeyword)
79-
export.Loc = tag.Loc
80-
export.Flags = p.contextFlags | ast.NodeFlagsReparsed
81-
modifiers := p.newModifierList(export.Loc, p.nodeSlicePool.NewSlice1(export))
82-
83-
typeAlias := p.factory.NewJSTypeAliasDeclaration(modifiers, p.factory.DeepCloneReparse(tag.AsJSDocTypedefTag().Name()), nil, nil)
77+
typeAlias := p.factory.NewJSTypeAliasDeclaration(nil, p.factory.DeepCloneReparse(tag.AsJSDocTypedefTag().Name()), nil, nil)
8478
typeAlias.AsTypeAliasDeclaration().TypeParameters = p.gatherTypeParameters(jsDoc, tag)
8579
var t *ast.Node
8680
switch typeExpression.Kind {

internal/transformers/declarations/transform.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,9 @@ func (tx *DeclarationTransformer) ensureModifierFlags(node *ast.Node) ast.Modifi
15821582
mask ^= ast.ModifierFlagsAmbient
15831583
additions = ast.ModifierFlagsNone
15841584
}
1585+
if ast.IsImplicitlyExportedJSTypeAlias(node) {
1586+
additions |= ast.ModifierFlagsExport
1587+
}
15851588
return maskModifierFlags(tx.host, node, mask, additions)
15861589
}
15871590

testdata/baselines/reference/compiler/emitEndOfFileJSDocComments.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ var unrelated;
66
/** @typedef {number} B */
77

88
//// [emitEndOfFileJSDocComments.js]
9-
"use strict";
10-
Object.defineProperty(exports, "__esModule", { value: true });
119
/** @typedef {number} A */
1210
var unrelated;
1311
/** @typedef {number} B */

testdata/baselines/reference/compiler/emitEndOfFileJSDocComments2.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ function test(param) {
2222

2323

2424
//// [emitEndOfFileJSDocComments2.js]
25-
"use strict";
2625
/** @typedef {number} A */
27-
Object.defineProperty(exports, "__esModule", { value: true });
2826
/**
2927
* JSDoc comment for function
3028
* @param {string} param - A string parameter

0 commit comments

Comments
 (0)