Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/TypeScript/tsc/internal/fourslash"
"github.com/microsoft/TypeScript/tsc/internal/testutil"
)

// "pkg" resolves through the active "node" condition to dist/node.d.ts, so a symbol that only
// exists in the shadowed "default" target (dist/index.d.ts) can't be imported from "pkg".
func TestAutoImportPackageJsonExportsShadowedCondition1(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @module: node18
// @Filename: /node_modules/pkg/package.json
{
"name": "pkg",
"version": "1.0.0",
"exports": {
".": {
"node": "./dist/node.js",
"default": "./dist/index.js"
}
}
}
// @Filename: /node_modules/pkg/dist/node.d.ts
export declare const fromNode: number;
// @Filename: /node_modules/pkg/dist/index.d.ts
export declare const fromDefault: number;
// @Filename: /package.json
{
"dependencies": {
"pkg": "*"
}
}
// @Filename: /a.ts
fromNode/*1*/;
// @Filename: /b.ts
fromDefault/*2*/;`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyImportFixModuleSpecifiers(t, "1", []string{"pkg"}, nil /*preferences*/)
f.VerifyImportFixModuleSpecifiers(t, "2", []string{}, nil /*preferences*/)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/TypeScript/tsc/internal/fourslash"
"github.com/microsoft/TypeScript/tsc/internal/testutil"
)

// "types" is not a runtime condition, so the runtime conditions listed before it must not shadow
// it: the declaration file it names is exactly what TypeScript resolves "pkg" to.
func TestAutoImportPackageJsonExportsShadowedCondition2(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @module: node18
// @Filename: /node_modules/pkg/package.json
{
"name": "pkg",
"version": "1.0.0",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs",
"types": "./dist/index.d.ts"
}
}
}
// @Filename: /node_modules/pkg/dist/index.d.ts
export declare const dep: number;
// @Filename: /package.json
{
"dependencies": {
"pkg": "*"
}
}
// @Filename: /index.ts
dep/**/`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyImportFixModuleSpecifiers(t, "", []string{"pkg"}, nil /*preferences*/)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/TypeScript/tsc/internal/fourslash"
"github.com/microsoft/TypeScript/tsc/internal/testutil"
)

// Same package shape as ShadowedCondition2, imported from an ES module. The "import" target yields no
// declaration file, so resolution falls through to "types"; the "import" condition listed earlier must
// not be treated as shadowing it.
func TestAutoImportPackageJsonExportsShadowedCondition3(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @module: node18
// @Filename: /node_modules/pkg/package.json
{
"name": "pkg",
"version": "1.0.0",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs",
"types": "./dist/index.d.ts"
}
}
}
// @Filename: /node_modules/pkg/dist/index.d.ts
export declare const dep: number;
// @Filename: /package.json
{
"type": "module",
"dependencies": {
"pkg": "*"
}
}
// @Filename: /index.ts
dep/**/`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyImportFixModuleSpecifiers(t, "", []string{"pkg"}, nil /*preferences*/)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/TypeScript/tsc/internal/fourslash"
"github.com/microsoft/TypeScript/tsc/internal/ls/lsutil"
"github.com/microsoft/TypeScript/tsc/internal/testutil"
)

// The "node" condition is active and is listed first, so at runtime "#utils/summarize/summarize"
// resolves to ./src/utils/summarize/summarize/index.ts, which doesn't exist. The "default" target
// that would have matched is never consulted, so the specifier must not be offered.
func TestAutoImportPackageJsonImportsShadowedCondition1(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @module: nodenext
// @Filename: /package.json
{
"type": "module",
"imports": {
"#*": {
"node": "./src/*/index.ts",
"default": "./src/*.ts"
}
}
}
// @Filename: /src/utils/summarize/index.ts
export {};
// @Filename: /src/utils/summarize/summarize.ts
export function summarize(name: string): any;
// @Filename: /src/index.ts
summarize/**/`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyImportFixModuleSpecifiers(t, "", []string{"./utils/summarize/summarize.js"}, &lsutil.UserPreferences{ImportModuleSpecifierPreference: "non-relative"})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/TypeScript/tsc/internal/fourslash"
"github.com/microsoft/TypeScript/tsc/internal/ls/lsutil"
"github.com/microsoft/TypeScript/tsc/internal/testutil"
)

// Same mapping as ShadowedCondition1, but the target is the file the active "node" condition
// actually selects, so the "#" specifier is valid and must still be offered.
func TestAutoImportPackageJsonImportsShadowedCondition2(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @module: nodenext
// @Filename: /package.json
{
"type": "module",
"imports": {
"#*": {
"node": "./src/*/index.ts",
"default": "./src/*.ts"
}
}
}
// @Filename: /src/utils/summarize/index.ts
export function summarize(name: string): any;
// @Filename: /src/index.ts
summarize/**/`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyImportFixModuleSpecifiers(t, "", []string{"#utils/summarize"}, &lsutil.UserPreferences{ImportModuleSpecifierPreference: "non-relative"})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/TypeScript/tsc/internal/fourslash"
"github.com/microsoft/TypeScript/tsc/internal/ls/lsutil"
"github.com/microsoft/TypeScript/tsc/internal/testutil"
)

// A custom condition shadows "default" in exactly the same way the built-in "node" condition does.
func TestAutoImportPackageJsonImportsShadowedCondition3(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @module: esnext
// @moduleResolution: bundler
// @customConditions: custom
// @Filename: /package.json
{
"imports": {
"#*": {
"custom": "./src/*/index.ts",
"default": "./src/*.ts"
}
}
}
// @Filename: /src/utils/summarize/index.ts
export {};
// @Filename: /src/utils/summarize/summarize.ts
export function summarize(name: string): any;
// @Filename: /src/index.ts
summarize/**/`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyImportFixModuleSpecifiers(t, "", []string{"./utils/summarize/summarize"}, &lsutil.UserPreferences{ImportModuleSpecifierPreference: "non-relative"})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/TypeScript/tsc/internal/fourslash"
"github.com/microsoft/TypeScript/tsc/internal/ls/lsutil"
"github.com/microsoft/TypeScript/tsc/internal/testutil"
)

// A null target for the active condition blocks the specifier outright at runtime, so the
// "default" target after it can't rescue the "#" specifier.
func TestAutoImportPackageJsonImportsShadowedCondition4(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @module: nodenext
// @Filename: /package.json
{
"type": "module",
"imports": {
"#*": {
"node": null,
"default": "./src/*.ts"
}
}
}
// @Filename: /src/utils/summarize.ts
export function summarize(name: string): any;
// @Filename: /src/index.ts
summarize/**/`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyImportFixModuleSpecifiers(t, "", []string{"./utils/summarize.js"}, &lsutil.UserPreferences{ImportModuleSpecifierPreference: "non-relative"})
}
23 changes: 16 additions & 7 deletions tsc/internal/module/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2310,25 +2310,34 @@ func (r *resolutionState) loadEntrypointsFromExportMap(
continue
}

conditionAlwaysMatches := condition == "default" || condition == "types" || IsApplicableVersionedTypesKey(condition)
isTypesCondition := condition == "types" || IsApplicableVersionedTypesKey(condition)
conditionAlwaysMatches := condition == "default" || isTypesCondition
newIncludeConditions := includeConditions
if !conditionAlwaysMatches {
newIncludeConditions = includeConditions.Clone()
excludeConditions = excludeConditions.Clone()
if newIncludeConditions == nil {
newIncludeConditions = &collections.Set[string]{}
}
newIncludeConditions.Add(condition)
}
// A resolver commits to the first of the preceding conditions it has, so this target is only
// reachable by a resolver that has none of them. That applies to "default" as much as to any
// named condition. The "types" conditions don't exist at runtime, so a resolver that has an
// earlier condition still falls through to a "types" target when the earlier one yields no
// declaration file.
newExcludeConditions := excludeConditions
if !isTypesCondition && len(prevConditions) > 0 {
newExcludeConditions = excludeConditions.Clone()
if newExcludeConditions == nil {
newExcludeConditions = &collections.Set[string]{}
}
for _, prevCondition := range prevConditions {
if excludeConditions == nil {
excludeConditions = &collections.Set[string]{}
}
excludeConditions.Add(prevCondition)
newExcludeConditions.Add(prevCondition)
Comment on lines 2334 to +2335
}
}

prevConditions = append(prevConditions, condition)
loadEntrypointsFromTargetExports(subpath, newIncludeConditions, excludeConditions, export)
loadEntrypointsFromTargetExports(subpath, newIncludeConditions, newExcludeConditions, export)
if conditionAlwaysMatches {
break
}
Expand Down
Loading