Skip to content

Commit 21b260b

Browse files
Copilotjakebailey
andauthored
Exclude top-level imports from document symbols (#64160)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent 1a78786 commit 21b260b

6 files changed

Lines changed: 51 additions & 30 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/TypeScript/tsc/internal/fourslash"
7+
"github.com/microsoft/TypeScript/tsc/internal/testutil"
8+
)
9+
10+
func TestDocumentSymbolTopLevelImports(t *testing.T) {
11+
t.Parallel()
12+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
13+
const content = `/** @typedef {number} ImportedType */
14+
import DefaultComponent from "./component";
15+
/** @callback ImportedCallback
16+
* @param {string} value
17+
* @returns {number}
18+
*/
19+
import * as utils from "./utils";
20+
import { value, original as renamed } from "./values";
21+
import type { Options } from "./types";
22+
23+
const local = 1;`
24+
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
25+
defer done()
26+
f.VerifyBaselineDocumentSymbol(t)
27+
}

‎tsc/internal/ls/symbols.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ func (l *LanguageService) getDocumentSymbolsForChildren(ctx context.Context, nod
161161
}
162162
}
163163
}
164+
if node.Parent.Kind == ast.KindSourceFile && ast.IsImportOrImportEqualsDeclaration(node) {
165+
return false
166+
}
164167
switch node.Kind {
165168
case ast.KindClassDeclaration, ast.KindClassExpression, ast.KindInterfaceDeclaration, ast.KindEnumDeclaration:
166169
if ast.IsClassLike(node) && ast.GetDeclarationName(node) != "" {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// === Document Symbols ===
2+
// === /documentSymbolTopLevelImports.ts ===
3+
// /** <|@typedef {number} [|{| name: ImportedType, kind: Class |}ImportedType|]|> */
4+
// import DefaultComponent from "./component";
5+
// /** <|@callback [|{| name: ImportedCallback, kind: Class |}ImportedCallback|]
6+
// * @param {string} value
7+
// * @returns {number}
8+
// |>*/
9+
// import * as utils from "./utils";
10+
// import { value, original as renamed } from "./values";
11+
// import type { Options } from "./types";
12+
//
13+
// const <|[|{| name: local, kind: Variable |}local|] = 1|>;
14+
15+
// === Details ===
16+
(Class) ImportedType
17+
(Class) ImportedCallback
18+
(Variable) local
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
// === Document Symbols ===
2-
// === /navigationBarImports.ts ===
3-
// import <|[|{| name: a, kind: Variable |}a|]|>, {<|[|{| name: b, kind: Variable |}b|]|>} from "m";
4-
// <|import [|{| name: c, kind: Variable |}c|] = require("m");|>
5-
// import <|* as [|{| name: d, kind: Variable |}d|]|> from "m";
2+
63

74
// === Details ===
8-
(Variable) a
9-
(Variable) b
10-
(Variable) c
11-
(Variable) d

‎tsc/testdata/baselines/reference/fourslash/documentSymbols/navigationBarItemsExports.baseline‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
//
55
// export { <|b as [|{| name: B, kind: Variable |}B|]|> } from "a"
66
//
7-
// <|export import [|{| name: e, kind: Variable |}e|] = require("a");|>
7+
// export import e = require("a");
88
//
99
// export * from "a"; // no bindings here
1010

1111
// === Details ===
1212
(Variable) a
1313
(Variable) B
14-
(Variable) e
Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,4 @@
11
// === Document Symbols ===
2-
// === /navigationBarItemsImports.ts ===
3-
// import <|[|{| name: d1, kind: Variable |}d1|]|> from "a";
4-
//
5-
// import { <|[|{| name: a, kind: Variable |}a|]|> } from "a";
6-
//
7-
// import { <|b as [|{| name: B, kind: Variable |}B|]|> } from "a"
8-
//
9-
// import <|[|{| name: d2, kind: Variable |}d2|]|>, { <|[|{| name: c, kind: Variable |}c|]|>, <|d as [|{| name: D, kind: Variable |}D|]|> } from "a"
10-
//
11-
// <|import [|{| name: e, kind: Variable |}e|] = require("a");|>
12-
//
13-
// import <|* as [|{| name: ns, kind: Variable |}ns|]|> from "a";
2+
143

154
// === Details ===
16-
(Variable) d1
17-
(Variable) a
18-
(Variable) B
19-
(Variable) d2
20-
(Variable) c
21-
(Variable) D
22-
(Variable) e
23-
(Variable) ns

0 commit comments

Comments
 (0)