Skip to content
Closed
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/checker/nodebuilderimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ func (b *NodeBuilderImpl) symbolToTypeNode(symbol *ast.Symbol, mask ast.SymbolFl
// If ultimately we can only name the symbol with a reference that dives into a `node_modules` folder, we should error
// since declaration files with these kinds of references are liable to fail when published :(
b.ctx.encounteredError = true
b.ctx.tracker.ReportLikelyUnsafeImportRequiredError(oldSpecifier)
b.ctx.tracker.ReportLikelyUnsafeImportRequiredError(oldSpecifier, symbol.Name)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't symbol be nil like the original PR?

}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/checker/symboltracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ func (this *SymbolTrackerImpl) ReportCyclicStructureError() {
this.inner.ReportCyclicStructureError()
}

func (this *SymbolTrackerImpl) ReportLikelyUnsafeImportRequiredError(specifier string) {
func (this *SymbolTrackerImpl) ReportLikelyUnsafeImportRequiredError(specifier string, symbolName string) {
this.onDiagnosticReported()
if this.inner == nil {
return
}
this.inner.ReportLikelyUnsafeImportRequiredError(specifier)
this.inner.ReportLikelyUnsafeImportRequiredError(specifier, symbolName)
}

func (this *SymbolTrackerImpl) ReportTruncationError() {
Expand Down
4 changes: 4 additions & 0 deletions internal/diagnostics/diagnostics_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/nodebuilder/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type SymbolTracker interface {
ReportPrivateInBaseOfClassExpression(propertyName string)
ReportInaccessibleUniqueSymbolError()
ReportCyclicStructureError()
ReportLikelyUnsafeImportRequiredError(specifier string)
ReportLikelyUnsafeImportRequiredError(specifier string, symbolName string)
ReportTruncationError()
ReportNonlocalAugmentation(containingFile *ast.SourceFile, parentSymbol *ast.Symbol, augmentingSymbol *ast.Symbol)
ReportNonSerializableProperty(propertyName string)
Expand Down
8 changes: 6 additions & 2 deletions internal/transformers/declarations/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ func (s *SymbolTrackerImpl) ReportInferenceFallback(node *ast.Node) {
}

// ReportLikelyUnsafeImportRequiredError implements checker.SymbolTracker.
func (s *SymbolTrackerImpl) ReportLikelyUnsafeImportRequiredError(specifier string) {
func (s *SymbolTrackerImpl) ReportLikelyUnsafeImportRequiredError(specifier string, symbolName string) {
location := s.errorLocation()
if location != nil {
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))
if symbolName != "" {
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))
} else {
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))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
r/entry.ts(2,14): error TS2883: The inferred type of 'special' cannot be named without a reference to 'MySpecialType' from 'foo/node_modules/nested'. This is likely not portable. A type annotation is necessary.


==== r/node_modules/foo/node_modules/nested/index.d.ts (0 errors) ====
export interface MySpecialType {
val: string;
}
==== r/node_modules/foo/index.d.ts (0 errors) ====
import { MySpecialType } from "nested";
export function getSpecial(): MySpecialType;
==== r/entry.ts (1 errors) ====
import { getSpecial } from "foo";
export const special = getSpecial();
~~~~~~~
!!! error TS2883: The inferred type of 'special' cannot be named without a reference to 'MySpecialType' from 'foo/node_modules/nested'. This is likely not portable. A type annotation is necessary.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [tests/cases/compiler/declarationEmitUnsafeImportSymbolName.ts] ////

//// [index.d.ts]
export interface MySpecialType {
val: string;
}
//// [index.d.ts]
import { MySpecialType } from "nested";
export function getSpecial(): MySpecialType;
//// [entry.ts]
import { getSpecial } from "foo";
export const special = getSpecial();


//// [entry.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.special = void 0;
const foo_1 = require("foo");
exports.special = (0, foo_1.getSpecial)();


//// [entry.d.ts]
export declare const special: any;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [tests/cases/compiler/declarationEmitUnsafeImportSymbolName.ts] ////

=== r/node_modules/foo/node_modules/nested/index.d.ts ===
export interface MySpecialType {
>MySpecialType : Symbol(MySpecialType, Decl(index.d.ts, 0, 0))

val: string;
>val : Symbol(MySpecialType.val, Decl(index.d.ts, 0, 32))
}
=== r/node_modules/foo/index.d.ts ===
import { MySpecialType } from "nested";
>MySpecialType : Symbol(MySpecialType, Decl(index.d.ts, 0, 8))

export function getSpecial(): MySpecialType;
>getSpecial : Symbol(getSpecial, Decl(index.d.ts, 0, 39))
>MySpecialType : Symbol(MySpecialType, Decl(index.d.ts, 0, 8))

=== r/entry.ts ===
import { getSpecial } from "foo";
>getSpecial : Symbol(getSpecial, Decl(entry.ts, 0, 8))

export const special = getSpecial();
>special : Symbol(special, Decl(entry.ts, 1, 12))
>getSpecial : Symbol(getSpecial, Decl(entry.ts, 0, 8))

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [tests/cases/compiler/declarationEmitUnsafeImportSymbolName.ts] ////

=== r/node_modules/foo/node_modules/nested/index.d.ts ===
export interface MySpecialType {
val: string;
>val : string
}
=== r/node_modules/foo/index.d.ts ===
import { MySpecialType } from "nested";
>MySpecialType : any

export function getSpecial(): MySpecialType;
>getSpecial : () => MySpecialType

=== r/entry.ts ===
import { getSpecial } from "foo";
>getSpecial : () => import("foo/node_modules/nested").MySpecialType

export const special = getSpecial();
>special : import("foo/node_modules/nested").MySpecialType
>getSpecial() : import("foo/node_modules/nested").MySpecialType
>getSpecial : () => import("foo/node_modules/nested").MySpecialType

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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.
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.


==== r/node_modules/foo/node_modules/nested/index.d.ts (0 errors) ====
Expand All @@ -23,6 +23,6 @@ r/entry.ts(3,14): error TS2742: The inferred type of 'x' cannot be named without
import { bar } from "root";
export const x = foo();
~
!!! 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.
!!! 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.
export const y = bar();

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--- old.declarationEmitCommonJsModuleReferencedType.errors.txt
+++ new.declarationEmitCommonJsModuleReferencedType.errors.txt
@@= skipped -0, +0 lines =@@
-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.
+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.


==== r/node_modules/foo/node_modules/nested/index.d.ts (0 errors) ====
@@= skipped -22, +22 lines =@@
import { bar } from "root";
export const x = foo();
~
-!!! 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.
+!!! 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.
export const y = bar();

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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.
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.


==== node_modules/styled-components/node_modules/hoist-non-react-statics/index.d.ts (0 errors) ====
Expand Down Expand Up @@ -43,5 +43,5 @@ index.ts(7,1): error TS2742: The inferred type of 'default' cannot be named with
~~~~~
});
~~~
!!! 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.
!!! 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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--- old.declarationEmitObjectAssignedDefaultExport.errors.txt
+++ new.declarationEmitObjectAssignedDefaultExport.errors.txt
@@= skipped -0, +0 lines =@@
-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.
+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.


==== node_modules/styled-components/node_modules/hoist-non-react-statics/index.d.ts (0 errors) ====
@@= skipped -42, +42 lines =@@
~~~~~
});
~~~
-!!! 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.
+!!! 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.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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.
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.


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

export const ADMIN = MetadataAccessor.create<boolean>('1');
~~~~~
!!! 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.
!!! 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.
==== monorepo/pkg1/dist/index.d.ts (0 errors) ====
export * from './types';
==== monorepo/pkg1/dist/types.d.ts (0 errors) ====
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--- old.declarationEmitReexportedSymlinkReference3.errors.txt
+++ new.declarationEmitReexportedSymlinkReference3.errors.txt
@@= skipped -0, +0 lines =@@
-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.
+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.


==== monorepo/pkg3/tsconfig.json (0 errors) ====
@@= skipped -20, +20 lines =@@

export const ADMIN = MetadataAccessor.create<boolean>('1');
~~~~~
-!!! 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.
+!!! 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.
==== monorepo/pkg1/dist/index.d.ts (0 errors) ====
export * from './types';
==== monorepo/pkg1/dist/types.d.ts (0 errors) ====
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
src/index.ts(3,14): error TS2742: The inferred type of 'foo' cannot be named without a reference to '../node_modules/some-dep/dist/inner'. This is likely not portable. A type annotation is necessary.
src/index.ts(7,14): error TS2742: The inferred type of 'bar' cannot be named without a reference to '../node_modules/some-dep/dist/inner'. This is likely not portable. A type annotation is necessary.
src/index.ts(3,14): error TS2883: The inferred type of 'foo' cannot be named without a reference to 'SomeType' from '../node_modules/some-dep/dist/inner'. This is likely not portable. A type annotation is necessary.
src/index.ts(7,14): error TS2883: The inferred type of 'bar' cannot be named without a reference to 'Other' from '../node_modules/some-dep/dist/inner'. This is likely not portable. A type annotation is necessary.
src/index.ts(7,14): error TS2883: The inferred type of 'bar' cannot be named without a reference to 'SomeType' from '../node_modules/some-dep/dist/inner'. This is likely not portable. A type annotation is necessary.


==== node_modules/some-dep/dist/inner.d.ts (0 errors) ====
Expand All @@ -18,17 +19,19 @@ src/index.ts(7,14): error TS2742: The inferred type of 'bar' cannot be named wit
}
}

==== src/index.ts (2 errors) ====
==== src/index.ts (3 errors) ====
import { SomeType } from "some-dep";

export const foo = (thing: SomeType) => {
~~~
!!! error TS2742: The inferred type of 'foo' cannot be named without a reference to '../node_modules/some-dep/dist/inner'. This is likely not portable. A type annotation is necessary.
!!! error TS2883: The inferred type of 'foo' cannot be named without a reference to 'SomeType' from '../node_modules/some-dep/dist/inner'. This is likely not portable. A type annotation is necessary.
return thing;
};

export const bar = (thing: SomeType) => {
~~~
!!! error TS2742: The inferred type of 'bar' cannot be named without a reference to '../node_modules/some-dep/dist/inner'. This is likely not portable. A type annotation is necessary.
!!! error TS2883: The inferred type of 'bar' cannot be named without a reference to 'Other' from '../node_modules/some-dep/dist/inner'. This is likely not portable. A type annotation is necessary.
~~~
!!! error TS2883: The inferred type of 'bar' cannot be named without a reference to 'SomeType' from '../node_modules/some-dep/dist/inner'. This is likely not portable. A type annotation is necessary.
return thing.arg;
};
Loading