@@ -1257,7 +1257,7 @@ func (c *Checker) initializeChecker() {
1257
1257
c.autoArrayType = c.newAnonymousType(nil, nil, nil, nil, nil)
1258
1258
}
1259
1259
c.globalReadonlyArrayType = c.getGlobalType("ReadonlyArray", 1 /*arity*/, false /*reportErrors*/)
1260
- if c.globalReadonlyArrayType == nil {
1260
+ if c.globalReadonlyArrayType == c.emptyGenericType {
1261
1261
c.globalReadonlyArrayType = c.globalArrayType
1262
1262
}
1263
1263
c.anyReadonlyArrayType = c.createTypeFromGenericGlobalType(c.globalReadonlyArrayType, []*Type{c.anyType})
@@ -5028,7 +5028,7 @@ func (c *Checker) checkImportAttributes(declaration *ast.Node) {
5028
5028
if importAttributesType != c.emptyObjectType {
5029
5029
c.checkTypeAssignableTo(c.getTypeFromImportAttributes(node), c.getNullableType(importAttributesType, TypeFlagsUndefined), node, nil)
5030
5030
}
5031
- isTypeOnly := ast.IsTypeOnlyImportOrExportDeclaration (declaration)
5031
+ isTypeOnly := isExclusivelyTypeOnlyImportOrExport (declaration)
5032
5032
override := c.getResolutionModeOverride(node.AsImportAttributes(), isTypeOnly)
5033
5033
isImportAttributes := node.AsImportAttributes().Token == ast.KindWithKeyword
5034
5034
if isTypeOnly && override != core.ResolutionModeNone {
@@ -5066,6 +5066,22 @@ func (c *Checker) checkImportAttributes(declaration *ast.Node) {
5066
5066
}
5067
5067
}
5068
5068
5069
+ func isExclusivelyTypeOnlyImportOrExport(node *ast.Node) bool {
5070
+ switch node.Kind {
5071
+ case ast.KindExportDeclaration:
5072
+ return node.AsExportDeclaration().IsTypeOnly
5073
+ case ast.KindImportDeclaration:
5074
+ if importClause := node.AsImportDeclaration().ImportClause; importClause != nil {
5075
+ return importClause.AsImportClause().IsTypeOnly
5076
+ }
5077
+ case ast.KindJSDocImportTag:
5078
+ if importClause := node.AsJSDocImportTag().ImportClause; importClause != nil {
5079
+ return importClause.AsImportClause().IsTypeOnly
5080
+ }
5081
+ }
5082
+ return false
5083
+ }
5084
+
5069
5085
func (c *Checker) getTypeFromImportAttributes(node *ast.Node) *Type {
5070
5086
links := c.typeNodeLinks.Get(node)
5071
5087
if links.resolvedType == nil {
@@ -5787,7 +5803,7 @@ func (c *Checker) getIterationTypesOfGeneratorFunctionReturnType(t *Type, isAsyn
5787
5803
if result.hasTypes() {
5788
5804
return result
5789
5805
}
5790
- return c.getIterationTypesOfIterator(t, resolver, nil /*errorNode*/)
5806
+ return c.getIterationTypesOfIterator(t, resolver, nil /*errorNode*/, nil /*diagnosticOutput*/ )
5791
5807
}
5792
5808
5793
5809
// Gets the requested "iteration type" from an `Iterable`-like or `AsyncIterable`-like type.
@@ -5853,14 +5869,15 @@ func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, e
5853
5869
return iterationTypes
5854
5870
}
5855
5871
}
5872
+ var diags []*ast.Diagnostic
5856
5873
if use&IterationUseAllowsAsyncIterablesFlag != 0 {
5857
- iterationTypes := c.getIterationTypesOfIterableSlow(t, c.asyncIterationTypesResolver, errorNode)
5874
+ iterationTypes := c.getIterationTypesOfIterableSlow(t, c.asyncIterationTypesResolver, errorNode, &diags )
5858
5875
if iterationTypes.hasTypes() {
5859
5876
return iterationTypes
5860
5877
}
5861
5878
}
5862
5879
if use&IterationUseAllowsSyncIterablesFlag != 0 {
5863
- iterationTypes := c.getIterationTypesOfIterableSlow(t, c.syncIterationTypesResolver, errorNode)
5880
+ iterationTypes := c.getIterationTypesOfIterableSlow(t, c.syncIterationTypesResolver, errorNode, &diags )
5864
5881
if iterationTypes.hasTypes() {
5865
5882
if use&IterationUseAllowsAsyncIterablesFlag != 0 {
5866
5883
return c.getAsyncFromSyncIterationTypes(iterationTypes, errorNode)
@@ -5869,7 +5886,10 @@ func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, e
5869
5886
}
5870
5887
}
5871
5888
if errorNode != nil {
5872
- c.reportTypeNotIterableError(errorNode, t, use&IterationUseAllowsAsyncIterablesFlag != 0)
5889
+ diagnostic := c.reportTypeNotIterableError(errorNode, t, use&IterationUseAllowsAsyncIterablesFlag != 0)
5890
+ for _, d := range diags {
5891
+ diagnostic.AddRelatedInfo(d)
5892
+ }
5873
5893
}
5874
5894
return IterationTypes{}
5875
5895
}
@@ -5977,15 +5997,15 @@ func (c *Checker) getAsyncFromSyncIterationTypes(iterationTypes IterationTypes,
5977
5997
//
5978
5998
// NOTE: You probably don't want to call this directly and should be calling
5979
5999
// `getIterationTypesOfIterable` instead.
5980
- func (c *Checker) getIterationTypesOfIterableSlow(t *Type, r *IterationTypesResolver, errorNode *ast.Node) IterationTypes {
6000
+ func (c *Checker) getIterationTypesOfIterableSlow(t *Type, r *IterationTypesResolver, errorNode *ast.Node, diagnosticOutput *[]*ast.Diagnostic ) IterationTypes {
5981
6001
if method := c.getPropertyOfType(t, c.getPropertyNameForKnownSymbolName(r.iteratorSymbolName)); method != nil && method.Flags&ast.SymbolFlagsOptional == 0 {
5982
6002
methodType := c.getTypeOfSymbol(method)
5983
6003
if IsTypeAny(methodType) {
5984
6004
return IterationTypes{c.anyType, c.anyType, c.anyType}
5985
6005
}
5986
6006
if signatures := c.getSignaturesOfType(methodType, SignatureKindCall); len(signatures) != 0 {
5987
6007
iteratorType := c.getIntersectionType(core.Map(signatures, c.getReturnTypeOfSignature))
5988
- return c.getIterationTypesOfIteratorWorker(iteratorType, r, errorNode)
6008
+ return c.getIterationTypesOfIteratorWorker(iteratorType, r, errorNode, diagnosticOutput )
5989
6009
}
5990
6010
}
5991
6011
return IterationTypes{}
@@ -5995,8 +6015,8 @@ func (c *Checker) getIterationTypesOfIterableSlow(t *Type, r *IterationTypesReso
5995
6015
//
5996
6016
// If we successfully found the *yield*, *return*, and *next* types, an `IterationTypes` with non-nil
5997
6017
// members is returned. Otherwise, a default `IterationTypes{}` is returned.
5998
- func (c *Checker) getIterationTypesOfIterator(t *Type, r *IterationTypesResolver, errorNode *ast.Node) IterationTypes {
5999
- return c.getIterationTypesOfIteratorWorker(t, r, errorNode)
6018
+ func (c *Checker) getIterationTypesOfIterator(t *Type, r *IterationTypesResolver, errorNode *ast.Node, diagnosticOutput *[]*ast.Diagnostic ) IterationTypes {
6019
+ return c.getIterationTypesOfIteratorWorker(t, r, errorNode, diagnosticOutput )
6000
6020
}
6001
6021
6002
6022
// Gets the *yield*, *return*, and *next* types from an `Iterator`-like or `AsyncIterator`-like type.
@@ -6005,15 +6025,15 @@ func (c *Checker) getIterationTypesOfIterator(t *Type, r *IterationTypesResolver
6005
6025
// members is returned. Otherwise, a default `IterationTypes{}` is returned.
6006
6026
//
6007
6027
// NOTE: You probably don't want to call this directly and should be calling `getIterationTypesOfIterator` instead.
6008
- func (c *Checker) getIterationTypesOfIteratorWorker(t *Type, r *IterationTypesResolver, errorNode *ast.Node) IterationTypes {
6028
+ func (c *Checker) getIterationTypesOfIteratorWorker(t *Type, r *IterationTypesResolver, errorNode *ast.Node, diagnosticOutput *[]*ast.Diagnostic ) IterationTypes {
6009
6029
if IsTypeAny(t) {
6010
6030
return IterationTypes{c.anyType, c.anyType, c.anyType}
6011
6031
}
6012
6032
iterationTypes := c.getIterationTypesOfIteratorFast(t, r)
6013
6033
if iterationTypes.hasTypes() {
6014
6034
return iterationTypes
6015
6035
}
6016
- return c.getIterationTypesOfIteratorSlow(t, r, errorNode)
6036
+ return c.getIterationTypesOfIteratorSlow(t, r, errorNode, diagnosticOutput )
6017
6037
}
6018
6038
6019
6039
func (c *Checker) getIterationTypesOfIteratorFast(t *Type, r *IterationTypesResolver) IterationTypes {
@@ -6043,15 +6063,15 @@ func (c *Checker) getIterationTypesOfIteratorFast(t *Type, r *IterationTypesReso
6043
6063
return IterationTypes{}
6044
6064
}
6045
6065
6046
- func (c *Checker) getIterationTypesOfIteratorSlow(t *Type, r *IterationTypesResolver, errorNode *ast.Node) IterationTypes {
6066
+ func (c *Checker) getIterationTypesOfIteratorSlow(t *Type, r *IterationTypesResolver, errorNode *ast.Node, diagnosticOutput *[]*ast.Diagnostic ) IterationTypes {
6047
6067
return c.combineIterationTypes([]IterationTypes{
6048
- c.getIterationTypesOfMethod(t, r, "next", errorNode),
6049
- c.getIterationTypesOfMethod(t, r, "return", errorNode),
6050
- c.getIterationTypesOfMethod(t, r, "throw", errorNode),
6068
+ c.getIterationTypesOfMethod(t, r, "next", errorNode, diagnosticOutput ),
6069
+ c.getIterationTypesOfMethod(t, r, "return", errorNode, diagnosticOutput ),
6070
+ c.getIterationTypesOfMethod(t, r, "throw", errorNode, diagnosticOutput ),
6051
6071
})
6052
6072
}
6053
6073
6054
- func (c *Checker) getIterationTypesOfMethod(t *Type, resolver *IterationTypesResolver, methodName string, errorNode *ast.Node) IterationTypes {
6074
+ func (c *Checker) getIterationTypesOfMethod(t *Type, resolver *IterationTypesResolver, methodName string, errorNode *ast.Node, diagnosticOutput *[]*ast.Diagnostic ) IterationTypes {
6055
6075
method := c.getPropertyOfType(t, methodName)
6056
6076
// Ignore 'return' or 'throw' if they are missing.
6057
6077
if method == nil && methodName != "next" {
@@ -6076,7 +6096,7 @@ func (c *Checker) getIterationTypesOfMethod(t *Type, resolver *IterationTypesRes
6076
6096
if len(methodSignatures) == 0 {
6077
6097
if errorNode != nil {
6078
6098
diagnostic := core.IfElse(methodName == "next", resolver.mustHaveANextMethodDiagnostic, resolver.mustBeAMethodDiagnostic)
6079
- c.error( errorNode, diagnostic, methodName)
6099
+ c.reportDiagnostic(NewDiagnosticForNode( errorNode, diagnostic, methodName), diagnosticOutput )
6080
6100
}
6081
6101
return IterationTypes{}
6082
6102
}
@@ -6143,7 +6163,7 @@ func (c *Checker) getIterationTypesOfMethod(t *Type, resolver *IterationTypesRes
6143
6163
iterationTypes := c.getIterationTypesOfIteratorResult(resolvedMethodReturnType)
6144
6164
if !iterationTypes.hasTypes() {
6145
6165
if errorNode != nil {
6146
- c.error( errorNode, resolver.mustHaveAValueDiagnostic, methodName)
6166
+ c.reportDiagnostic(NewDiagnosticForNode( errorNode, resolver.mustHaveAValueDiagnostic, methodName), diagnosticOutput )
6147
6167
}
6148
6168
yieldType = c.anyType
6149
6169
returnTypes = append(returnTypes, c.anyType)
@@ -6209,7 +6229,7 @@ func (c *Checker) isIteratorResult(t *Type, kind IterationTypeKind) bool {
6209
6229
return c.isTypeAssignableTo(core.IfElse(kind == IterationTypeKindYield, c.falseType, c.trueType), doneType)
6210
6230
}
6211
6231
6212
- func (c *Checker) reportTypeNotIterableError(errorNode *ast.Node, t *Type, allowAsyncIterables bool) {
6232
+ func (c *Checker) reportTypeNotIterableError(errorNode *ast.Node, t *Type, allowAsyncIterables bool) *ast.Diagnostic {
6213
6233
var message *diagnostics.Message
6214
6234
if allowAsyncIterables {
6215
6235
message = diagnostics.Type_0_must_have_a_Symbol_asyncIterator_method_that_returns_an_async_iterator
@@ -6221,7 +6241,7 @@ func (c *Checker) reportTypeNotIterableError(errorNode *ast.Node, t *Type, allow
6221
6241
errorNode.Parent.Expression() == errorNode &&
6222
6242
c.getGlobalAsyncIterableType() != c.emptyGenericType &&
6223
6243
c.isTypeAssignableTo(t, c.createTypeFromGenericGlobalType(c.getGlobalAsyncIterableType(), []*Type{c.anyType, c.anyType, c.anyType})))
6224
- c.errorAndMaybeSuggestAwait(errorNode, suggestAwait, message, c.TypeToString(t))
6244
+ return c.errorAndMaybeSuggestAwait(errorNode, suggestAwait, message, c.TypeToString(t))
6225
6245
}
6226
6246
6227
6247
func (c *Checker) getIterationDiagnosticDetails(use IterationUse, inputType *Type, allowsStrings bool, downlevelIteration bool) (*diagnostics.Message, bool) {
@@ -13129,11 +13149,12 @@ func (c *Checker) errorOrSuggestion(isError bool, location *ast.Node, message *d
13129
13149
c.addErrorOrSuggestion(isError, NewDiagnosticForNode(location, message, args...))
13130
13150
}
13131
13151
13132
- func (c *Checker) errorAndMaybeSuggestAwait(location *ast.Node, maybeMissingAwait bool, message *diagnostics.Message, args ...any) {
13152
+ func (c *Checker) errorAndMaybeSuggestAwait(location *ast.Node, maybeMissingAwait bool, message *diagnostics.Message, args ...any) *ast.Diagnostic {
13133
13153
diagnostic := c.error(location, message, args...)
13134
13154
if maybeMissingAwait {
13135
13155
diagnostic.AddRelatedInfo(createDiagnosticForNode(location, diagnostics.Did_you_forget_to_use_await))
13136
13156
}
13157
+ return diagnostic
13137
13158
}
13138
13159
13139
13160
func (c *Checker) addErrorOrSuggestion(isError bool, diagnostic *ast.Diagnostic) {
@@ -13656,8 +13677,8 @@ func (c *Checker) checkAndReportErrorForResolvingImportAliasToTypeOnlySymbol(nod
13656
13677
diagnostics.X_0_was_imported_here)
13657
13678
// TODO: how to get name for export *?
13658
13679
name := "*"
13659
- if typeOnlyDeclaration.Kind == ast.KindImportDeclaration {
13660
- name = getNameFromImportDeclaration(typeOnlyDeclaration).AsIdentifier().Text
13680
+ if ! ast.IsExportDeclaration(typeOnlyDeclaration) {
13681
+ name = getNameFromImportDeclaration(typeOnlyDeclaration).Text()
13661
13682
}
13662
13683
c.error(decl.ModuleReference, message).AddRelatedInfo(createDiagnosticForNode(typeOnlyDeclaration, relatedMessage, name))
13663
13684
}
0 commit comments