diff --git a/internal/fourslash/tests/documentHighlightImportPath_test.go b/internal/fourslash/tests/documentHighlightImportPath_test.go new file mode 100644 index 0000000000..229d8b5a13 --- /dev/null +++ b/internal/fourslash/tests/documentHighlightImportPath_test.go @@ -0,0 +1,21 @@ +package fourslash_test + +import ( + "testing" + + "github.com/microsoft/typescript-go/internal/fourslash" + "github.com/microsoft/typescript-go/internal/testutil" +) + +func TestDocumentHighlightImportPath(t *testing.T) { + t.Parallel() + + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `// @Filename: /a.ts +export const x = 0; + +// @Filename: /b.ts +import { x } from "[|./a|]";` + f := fourslash.NewFourslash(t, nil /*capabilities*/, content) + f.VerifyBaselineDocumentHighlights(t, nil /*preferences*/, f.Ranges()[0]) +} diff --git a/internal/ls/findallreferences.go b/internal/ls/findallreferences.go index d30a4035cf..bb234673cc 100644 --- a/internal/ls/findallreferences.go +++ b/internal/ls/findallreferences.go @@ -673,7 +673,7 @@ func (l *LanguageService) getReferencedSymbolsForNode(ctx context.Context, posit } moduleReferences := l.getReferencedSymbolsForModuleIfDeclaredBySourceFile(ctx, symbol, program, sourceFiles, checker, options, sourceFilesSet) // !!! cancellationToken - if moduleReferences != nil && symbol.Flags&ast.SymbolFlagsTransient != 0 { + if moduleReferences != nil && symbol.Flags&ast.SymbolFlagsTransient == 0 { return moduleReferences } @@ -976,8 +976,44 @@ func getMergedAliasedSymbolOfNamespaceExportDeclaration(node *ast.Node, symbol * } func getReferencedSymbolsForModule(program *compiler.Program, symbol *ast.Symbol, excludeImportTypeOfExportEquals bool, sourceFiles []*ast.SourceFile, sourceFilesSet *collections.Set[string]) []*SymbolAndEntries { - // !!! not implemented - return nil + // Minimal implementation to prevent crashes when highlighting import paths. + // This returns module declarations as references, which allows the early return + // in getReferencedSymbolsForNode to work properly and avoid the panic in + // skipPastExportOrImportSpecifierOrUnion. + + var references []*referenceEntry + + // Add the module declarations themselves as references + if symbol.Declarations != nil { + for _, decl := range symbol.Declarations { + switch decl.Kind { + case ast.KindSourceFile: + // Don't include the source file itself + case ast.KindModuleDeclaration: + sourceFile := ast.GetSourceFileOfNode(decl) + if sourceFilesSet.Has(sourceFile.FileName()) { + references = append(references, &referenceEntry{ + kind: entryKindNode, + node: decl.AsModuleDeclaration().Name(), + fileName: sourceFile.FileName(), + }) + } + default: + // This may be merged with something + // TypeScript: Debug.assert(!!(symbol.flags & SymbolFlags.Transient)) + } + } + } + + // Return as SymbolAndEntries even if there are no references + // This ensures the condition check in getReferencedSymbolsForNode works properly + return []*SymbolAndEntries{{ + definition: &Definition{ + Kind: definitionKindSymbol, + symbol: symbol, + }, + references: references, + }} } func getReferenceAtPosition(sourceFile *ast.SourceFile, position int, program *compiler.Program) *refInfo { diff --git a/testdata/baselines/reference/fourslash/documentHighlights/documentHighlightImportPath.baseline.jsonc b/testdata/baselines/reference/fourslash/documentHighlights/documentHighlightImportPath.baseline.jsonc new file mode 100644 index 0000000000..e4448cdf1a --- /dev/null +++ b/testdata/baselines/reference/fourslash/documentHighlights/documentHighlightImportPath.baseline.jsonc @@ -0,0 +1,3 @@ +// === documentHighlights === +// === /b.ts === +// import { x } from "/*HIGHLIGHTS*/./a"; \ No newline at end of file