Skip to content

Commit 5be6aab

Browse files
Copilotgabrittojakebailey
authored
Port PR #63200: Add symbol name to unsafe import error (TS2883) (#2931)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: gabritto <19519347+gabritto@users.noreply.github.com> Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent 49eb28a commit 5be6aab

33 files changed

Lines changed: 59 additions & 273 deletions

File tree

internal/checker/nodebuilderimpl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ func (b *NodeBuilderImpl) symbolToTypeNode(symbol *ast.Symbol, mask ast.SymbolFl
578578
// If ultimately we can only name the symbol with a reference that dives into a `node_modules` folder, we should error
579579
// since declaration files with these kinds of references are liable to fail when published :(
580580
b.ctx.encounteredError = true
581-
b.ctx.tracker.ReportLikelyUnsafeImportRequiredError(oldSpecifier)
581+
b.ctx.tracker.ReportLikelyUnsafeImportRequiredError(oldSpecifier, symbol.Name)
582582
}
583583
}
584584

internal/checker/symboltracker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ func (this *SymbolTrackerImpl) ReportCyclicStructureError() {
7171
this.inner.ReportCyclicStructureError()
7272
}
7373

74-
func (this *SymbolTrackerImpl) ReportLikelyUnsafeImportRequiredError(specifier string) {
74+
func (this *SymbolTrackerImpl) ReportLikelyUnsafeImportRequiredError(specifier string, symbolName string) {
7575
this.onDiagnosticReported()
7676
if this.inner == nil {
7777
return
7878
}
79-
this.inner.ReportLikelyUnsafeImportRequiredError(specifier)
79+
this.inner.ReportLikelyUnsafeImportRequiredError(specifier, symbolName)
8080
}
8181

8282
func (this *SymbolTrackerImpl) ReportTruncationError() {

internal/nodebuilder/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type SymbolTracker interface {
1212
ReportPrivateInBaseOfClassExpression(propertyName string)
1313
ReportInaccessibleUniqueSymbolError()
1414
ReportCyclicStructureError()
15-
ReportLikelyUnsafeImportRequiredError(specifier string)
15+
ReportLikelyUnsafeImportRequiredError(specifier string, symbolName string)
1616
ReportTruncationError()
1717
ReportNonlocalAugmentation(containingFile *ast.SourceFile, parentSymbol *ast.Symbol, augmentingSymbol *ast.Symbol)
1818
ReportNonSerializableProperty(propertyName string)

internal/transformers/declarations/tracker.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,14 @@ func (s *SymbolTrackerImpl) ReportInferenceFallback(node *ast.Node) {
6767
}
6868

6969
// ReportLikelyUnsafeImportRequiredError implements checker.SymbolTracker.
70-
func (s *SymbolTrackerImpl) ReportLikelyUnsafeImportRequiredError(specifier string) {
70+
func (s *SymbolTrackerImpl) ReportLikelyUnsafeImportRequiredError(specifier string, symbolName string) {
7171
location := s.errorLocation()
7272
if location != nil {
73-
s.state.addDiagnostic(createDiagnosticForNode(location, diagnostics.The_inferred_type_of_0_cannot_be_named_without_a_reference_to_1_This_is_likely_not_portable_A_type_annotation_is_necessary, s.errorDeclarationNameWithFallback(), specifier))
73+
if symbolName != "" {
74+
s.state.addDiagnostic(createDiagnosticForNode(location, diagnostics.The_inferred_type_of_0_cannot_be_named_without_a_reference_to_2_from_1_This_is_likely_not_portable_A_type_annotation_is_necessary, s.errorDeclarationNameWithFallback(), specifier, symbolName))
75+
} else {
76+
s.state.addDiagnostic(createDiagnosticForNode(location, diagnostics.The_inferred_type_of_0_cannot_be_named_without_a_reference_to_1_This_is_likely_not_portable_A_type_annotation_is_necessary, s.errorDeclarationNameWithFallback(), specifier))
77+
}
7478
}
7579
}
7680

testdata/baselines/reference/submodule/compiler/declarationEmitCommonJsModuleReferencedType.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
r/entry.ts(3,14): error TS2742: The inferred type of 'x' cannot be named without a reference to 'foo/node_modules/nested'. This is likely not portable. A type annotation is necessary.
1+
r/entry.ts(3,14): error TS2883: The inferred type of 'x' cannot be named without a reference to 'NestedProps' from 'foo/node_modules/nested'. This is likely not portable. A type annotation is necessary.
22

33

44
==== r/node_modules/foo/node_modules/nested/index.d.ts (0 errors) ====
@@ -23,6 +23,6 @@ r/entry.ts(3,14): error TS2742: The inferred type of 'x' cannot be named without
2323
import { bar } from "root";
2424
export const x = foo();
2525
~
26-
!!! error TS2742: The inferred type of 'x' cannot be named without a reference to 'foo/node_modules/nested'. This is likely not portable. A type annotation is necessary.
26+
!!! error TS2883: The inferred type of 'x' cannot be named without a reference to 'NestedProps' from 'foo/node_modules/nested'. This is likely not portable. A type annotation is necessary.
2727
export const y = bar();
2828

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

Lines changed: 0 additions & 16 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/declarationEmitObjectAssignedDefaultExport.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
index.ts(7,1): error TS2742: The inferred type of 'default' cannot be named without a reference to 'styled-components/node_modules/hoist-non-react-statics'. This is likely not portable. A type annotation is necessary.
1+
index.ts(7,1): error TS2883: The inferred type of 'default' cannot be named without a reference to 'NonReactStatics' from 'styled-components/node_modules/hoist-non-react-statics'. This is likely not portable. A type annotation is necessary.
22

33

44
==== node_modules/styled-components/node_modules/hoist-non-react-statics/index.d.ts (0 errors) ====
@@ -43,5 +43,5 @@ index.ts(7,1): error TS2742: The inferred type of 'default' cannot be named with
4343
~~~~~
4444
});
4545
~~~
46-
!!! error TS2742: The inferred type of 'default' cannot be named without a reference to 'styled-components/node_modules/hoist-non-react-statics'. This is likely not portable. A type annotation is necessary.
46+
!!! error TS2883: The inferred type of 'default' cannot be named without a reference to 'NonReactStatics' from 'styled-components/node_modules/hoist-non-react-statics'. This is likely not portable. A type annotation is necessary.
4747

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

Lines changed: 0 additions & 15 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/declarationEmitReexportedSymlinkReference3.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
monorepo/pkg3/src/keys.ts(3,14): error TS2742: The inferred type of 'ADMIN' cannot be named without a reference to '../../pkg2/node_modules/@raymondfeng/pkg1/dist'. This is likely not portable. A type annotation is necessary.
1+
monorepo/pkg3/src/keys.ts(3,14): error TS2883: The inferred type of 'ADMIN' cannot be named without a reference to 'IdType' from '../../pkg2/node_modules/@raymondfeng/pkg1/dist'. This is likely not portable. A type annotation is necessary.
22

33

44
==== monorepo/pkg3/tsconfig.json (0 errors) ====
@@ -21,7 +21,7 @@ monorepo/pkg3/src/keys.ts(3,14): error TS2742: The inferred type of 'ADMIN' cann
2121

2222
export const ADMIN = MetadataAccessor.create<boolean>('1');
2323
~~~~~
24-
!!! error TS2742: The inferred type of 'ADMIN' cannot be named without a reference to '../../pkg2/node_modules/@raymondfeng/pkg1/dist'. This is likely not portable. A type annotation is necessary.
24+
!!! error TS2883: The inferred type of 'ADMIN' cannot be named without a reference to 'IdType' from '../../pkg2/node_modules/@raymondfeng/pkg1/dist'. This is likely not portable. A type annotation is necessary.
2525
==== monorepo/pkg1/dist/index.d.ts (0 errors) ====
2626
export * from './types';
2727
==== monorepo/pkg1/dist/types.d.ts (0 errors) ====

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

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)