microsoft / microsoft/TypeScript

internal/ls: getContextNodeForNodeEntry returns nil for module-specifier literals (stock returns the enclosing import statement)

未关闭
#63,872 0 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@gabritto 已经在做这个了。

开始于 2026年8月4日。

Needs Investigation
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

Stock's getContextNodeForNodeEntry (src/services/findAllReferences.ts:338) returns the enclosing import statement for a module-specifier string literal; tsgo returns nil. One visible effect: Go To Implementation on an import path returns a LocationLink whose target range is just the string literal instead of the whole import statement.

Minimal repro

Run go test ./internal/ls/ -run TestModuleSpecifierContextNode:

package ls

import (
	"testing"

	"github.com/microsoft/typescript-go/internal/ast"
	"github.com/microsoft/typescript-go/internal/core"
	"github.com/microsoft/typescript-go/internal/parser"
)

func TestModuleSpecifierContextNode(t *testing.T) {
	t.Parallel()
	text := "import { x } from \"./b\";\nx;\n"
	sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
		FileName: "/a.ts",
		Path:     "/a.ts",
	}, text, core.ScriptKindTS)

	var literal *ast.Node
	for _, stmt := range sourceFile.Statements.Nodes {
		if ast.IsImportDeclaration(stmt) {
			literal = stmt.AsImportDeclaration().ModuleSpecifier
		}
	}
	if literal == nil || literal.Kind != ast.KindStringLiteral {
		t.Fatalf("module specifier literal not found: %v", literal)
	}
	if ctx := getContextNodeForNodeEntry(literal); ctx == nil {
		t.Fatal("getContextNodeForNodeEntry(module specifier) = nil; want the enclosing import statement")
	}
}

Fails with getContextNodeForNodeEntry(module specifier) = nil on a pristine checkout (v7.0.2, 2bd066d8).

The string-literal special case diverges from stock's version (findAllReferences.ts:371): the case keys on node.Parent.Kind instead of the node itself, the FindAncestor predicate closes over the outer node instead of testing its parameter, and the outer gate's IsDeclaration(ImportDeclaration) is true in tsgo but false in stock. Same result for the other forms TryGetImportFromModuleSpecifier handles (export … from, import()/require(), …).

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。