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,131 @@
package fourslash_test

import (
"slices"
"strings"
"testing"

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

func TestAutoImportNonRelativeModuleAugmentation(t *testing.T) {
t.Parallel()

for _, specifier := range []string{"lib", "./lib"} {
t.Run(specifier, func(t *testing.T) {
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @Filename: /tsconfig.json
{ "compilerOptions": { "module": "preserve", "paths": { "lib": ["./lib.ts"] } } }

// @Filename: /lib.ts
export interface Options {
a: string;
}

// @Filename: /augment.ts
export {};
declare module "TARGET" {
interface Options {
b: string;
}
}

// @Filename: /other.ts
export interface Options {
c: number;
}

// @Filename: /ambient.d.ts
declare module "ambient-options" {
export interface Options {
d: boolean;
}
}
` + autoImportOptionsConsumers
verifyAugmentedAutoImportSources(t, strings.Replace(content, "TARGET", specifier, 1), []string{"./other:Named", "ambient-options:Named", "lib:Named"})
})
}
}

func TestAutoImportNonRelativeModuleAugmentationOfPackage(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @Filename: /tsconfig.json
{ "compilerOptions": { "module": "preserve" } }

// @Filename: /package.json
{ "name": "project", "dependencies": { "some-pkg": "1.0.0" } }

// @Filename: /node_modules/some-pkg/package.json
{ "name": "some-pkg", "version": "1.0.0", "types": "index.d.ts" }

// @Filename: /node_modules/some-pkg/index.d.ts
export interface Options {
a: string;
}

// @Filename: /augment.ts
export {};
declare module "some-pkg" {
interface Options {
b: string;
}
}
` + autoImportOptionsConsumers
verifyAugmentedAutoImportSources(t, content, []string{"some-pkg:Named"})
}

func TestAutoImportKeepsDistinctNewImportsFromSameModule(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @Filename: /tsconfig.json
{ "compilerOptions": { "module": "preserve", "paths": { "lib": ["./lib.ts"] } } }

// @Filename: /lib.ts
export { Options as default } from "./source";
export interface Options {
a: string;
}

// @Filename: /source.ts
export interface Options {
b: string;
}

` + autoImportOptionsConsumers
verifyAugmentedAutoImportSources(t, content, []string{"lib:Default", "lib:Named"})
}

const autoImportOptionsConsumers = `
// @Filename: /main.ts
let options: Option/*typeUse*/

// @Filename: /importer.ts
import Option/*importStatement*/
`

func verifyAugmentedAutoImportSources(t *testing.T, content string, want []string) {
t.Helper()
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()

for _, marker := range []string{"typeUse", "importStatement"} {
f.GoToMarker(t, marker)
completions := f.GetCompletions(t, nil /*userPreferences*/)
if completions == nil {
t.Fatalf("%s: no completions returned", marker)
}
var sources []string
for _, item := range completions.Items {
if item.Label == "Options" && item.Data != nil && item.Data.AutoImport != nil {
fix := item.Data.AutoImport
sources = append(sources, fix.ModuleSpecifier+":"+fix.ImportKind.String())
}
}
slices.Sort(sources)
if !slices.Equal(sources, want) {
t.Errorf("%s: auto-imports for Options = %v, want %v", marker, sources, want)
}
}
}
20 changes: 20 additions & 0 deletions tsc/internal/ls/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2040,6 +2040,13 @@ func (l *LanguageService) getCompletionEntriesFromSymbols(
sortedEntries = append(sortedEntries, &CompletionItem{CompletionItem: entry, Symbol: sym})
}

type newImportCompletionKey struct {
fix lsproto.AutoImportFix
insertText string
kind lsproto.CompletionItemKind
deprecated bool
}
var seenNewImports collections.Set[newImportCompletionKey]
for _, autoImport := range data.autoImports {
// !!! check for type-only in JS
// !!! deprecation
Expand Down Expand Up @@ -2094,6 +2101,19 @@ func (l *LanguageService) getCompletionEntriesFromSymbols(
}
}

// A module augmentation and the file it augments can be indexed
// separately but resolve to the same new import. Keep distinct edits
// and presentation details, not duplicate suggestions.
if autoImport.Fix.Kind == lsproto.AutoImportFixKindAddNew &&
!seenNewImports.AddIfAbsent(newImportCompletionKey{
fix: *autoImport.Fix.AutoImportFix,
insertText: insertText,
kind: getCompletionsSymbolKind(autoImport.Export.ScriptElementKind),
deprecated: autoImport.Export.ScriptElementKindModifiers&lsutil.ScriptElementKindModifierDeprecated != 0,
}) {
continue
}

entry := l.createLSPCompletionItem(
ctx,
autoImport.Fix.Name,
Expand Down