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
21 changes: 21 additions & 0 deletions internal/fourslash/tests/documentHighlightImportPath_test.go
Original file line number Diff line number Diff line change
@@ -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])
}
42 changes: 39 additions & 3 deletions internal/ls/findallreferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// === documentHighlights ===
// === /b.ts ===
// import { x } from "/*HIGHLIGHTS*/./a";