Skip to content

Commit 2be521c

Browse files
committed
Port special localness checks to determine if we need to load project tree
1 parent 23f8f3f commit 2be521c

File tree

4 files changed

+104
-10
lines changed

4 files changed

+104
-10
lines changed

internal/ls/findallreferences.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ func (l *LanguageService) GetNonLocalDefinition(ctx context.Context, entry *Symb
469469
defer done()
470470
emitResolver := checker.GetEmitResolver()
471471
for _, d := range entry.definition.symbol.Declarations {
472-
if emitResolver.IsDeclarationVisible(d) {
472+
if isDefinitionVisible(emitResolver, d) {
473473
file, startPos := getFileAndStartPosFromDeclaration(d)
474474
fileName := file.FileName()
475475
return &NonLocalDefinition{
@@ -503,6 +503,48 @@ func (l *LanguageService) GetNonLocalDefinition(ctx context.Context, entry *Symb
503503
return nil
504504
}
505505

506+
// This is special handling to determine if we should load up more projects and find location in other projects
507+
// By default arrows (and such other ast kinds) are not visible as declaration emitter doesnt need them
508+
// But we want to handle them specially so that they are visible if their parent is visible
509+
func isDefinitionVisible(emitResolver *checker.EmitResolver, declaration *ast.Node) bool {
510+
if emitResolver.IsDeclarationVisible(declaration) {
511+
return true
512+
}
513+
if declaration.Parent == nil {
514+
return false
515+
}
516+
517+
// Variable initializers are visible if variable is visible
518+
if ast.HasInitializer(declaration.Parent) && declaration.Parent.Initializer() == declaration {
519+
return isDefinitionVisible(emitResolver, declaration.Parent)
520+
}
521+
522+
// Handle some exceptions here like arrow function, members of class and object literal expression which are technically not visible but we want the definition to be determined by its parent
523+
switch declaration.Kind {
524+
case ast.KindPropertyDeclaration,
525+
ast.KindGetAccessor,
526+
ast.KindSetAccessor,
527+
ast.KindMethodDeclaration:
528+
// Private/protected properties/methods are not visible
529+
if ast.HasModifier(declaration, ast.ModifierFlagsPrivate) || ast.IsPrivateIdentifier(declaration.Name()) {
530+
return false
531+
}
532+
// Public properties/methods are visible if its parents are visible, so:
533+
// falls through
534+
fallthrough
535+
case ast.KindConstructor,
536+
ast.KindPropertyAssignment,
537+
ast.KindShorthandPropertyAssignment,
538+
ast.KindObjectLiteralExpression,
539+
ast.KindClassExpression,
540+
ast.KindArrowFunction,
541+
ast.KindFunctionExpression:
542+
return isDefinitionVisible(emitResolver, declaration.Parent)
543+
default:
544+
return false
545+
}
546+
}
547+
506548
func (l *LanguageService) ForEachOriginalDefinitionLocation(
507549
ctx context.Context,
508550
entry *SymbolAndEntries,

testdata/baselines/reference/lspservertests/findAllRefs/special-handling-of-localness-when-using-arrow-function-as-object-literal-property-types.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,39 @@ Projects::
9898
[/user/username/projects/solution/api/tsconfig.json]
9999
/user/username/projects/solution/shared/src/index.ts
100100
/user/username/projects/solution/api/src/server.ts
101+
[/user/username/projects/solution/app/tsconfig.json] *new*
102+
/user/username/projects/solution/shared/src/index.ts
103+
/user/username/projects/solution/app/src/app.ts
101104
[/user/username/projects/solution/shared/tsconfig.json] *new*
102105
/user/username/projects/solution/shared/src/index.ts
103-
[/user/username/projects/solution/tsconfig.json]
106+
[/user/username/projects/solution/tsconfig.json] *modified*
104107
Config::
105-
[/user/username/projects/solution/api/tsconfig.json]
106-
RetainingProjects:
108+
[/user/username/projects/solution/api/tsconfig.json] *modified*
109+
RetainingProjects: *modified*
107110
/user/username/projects/solution/api/tsconfig.json
111+
/user/username/projects/solution/tsconfig.json *new*
108112
RetainingOpenFiles:
109113
/user/username/projects/solution/api/src/server.ts
114+
[/user/username/projects/solution/app/tsconfig.json] *new*
115+
RetainingProjects:
116+
/user/username/projects/solution/app/tsconfig.json
117+
/user/username/projects/solution/tsconfig.json
110118
[/user/username/projects/solution/shared/tsconfig.json] *modified*
111119
RetainingProjects: *modified*
112120
/user/username/projects/solution/api/tsconfig.json
121+
/user/username/projects/solution/app/tsconfig.json *new*
113122
/user/username/projects/solution/shared/tsconfig.json *new*
123+
/user/username/projects/solution/tsconfig.json *new*
124+
[/user/username/projects/solution/tsconfig.json] *new*
125+
RetainingProjects:
126+
/user/username/projects/solution/tsconfig.json
114127
// === /user/username/projects/solution/api/src/server.ts ===
115128
// import * as shared from "../../shared/dist"
116129
// shared.foo./*FIND ALL REFS*/[|bar|]();
117130

131+
// === /user/username/projects/solution/app/src/app.ts ===
132+
// import * as shared from "../../shared/dist"
133+
// shared.foo.[|bar|]();
134+
118135
// === /user/username/projects/solution/shared/src/index.ts ===
119136
// export const foo = { [|bar|]: () => { } };

testdata/baselines/reference/lspservertests/findAllRefs/special-handling-of-localness-when-using-method-of-class-expression.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,41 @@ Projects::
100100
[/user/username/projects/solution/api/tsconfig.json]
101101
/user/username/projects/solution/shared/src/index.ts
102102
/user/username/projects/solution/api/src/server.ts
103+
[/user/username/projects/solution/app/tsconfig.json] *new*
104+
/user/username/projects/solution/shared/src/index.ts
105+
/user/username/projects/solution/app/src/app.ts
103106
[/user/username/projects/solution/shared/tsconfig.json] *new*
104107
/user/username/projects/solution/shared/src/index.ts
105-
[/user/username/projects/solution/tsconfig.json]
108+
[/user/username/projects/solution/tsconfig.json] *modified*
106109
Config::
107-
[/user/username/projects/solution/api/tsconfig.json]
108-
RetainingProjects:
110+
[/user/username/projects/solution/api/tsconfig.json] *modified*
111+
RetainingProjects: *modified*
109112
/user/username/projects/solution/api/tsconfig.json
113+
/user/username/projects/solution/tsconfig.json *new*
110114
RetainingOpenFiles:
111115
/user/username/projects/solution/api/src/server.ts
116+
[/user/username/projects/solution/app/tsconfig.json] *new*
117+
RetainingProjects:
118+
/user/username/projects/solution/app/tsconfig.json
119+
/user/username/projects/solution/tsconfig.json
112120
[/user/username/projects/solution/shared/tsconfig.json] *modified*
113121
RetainingProjects: *modified*
114122
/user/username/projects/solution/api/tsconfig.json
123+
/user/username/projects/solution/app/tsconfig.json *new*
115124
/user/username/projects/solution/shared/tsconfig.json *new*
125+
/user/username/projects/solution/tsconfig.json *new*
126+
[/user/username/projects/solution/tsconfig.json] *new*
127+
RetainingProjects:
128+
/user/username/projects/solution/tsconfig.json
116129
// === /user/username/projects/solution/api/src/server.ts ===
117130
// import * as shared from "../../shared/dist"
118131
// const instance = new shared.foo();
119132
// instance./*FIND ALL REFS*/[|fly|]();
120133

134+
// === /user/username/projects/solution/app/src/app.ts ===
135+
// import * as shared from "../../shared/dist"
136+
// const instance = new shared.foo();
137+
// instance.[|fly|]();
138+
121139
// === /user/username/projects/solution/shared/src/index.ts ===
122140
// export const foo = class { [|fly|]() {} };

testdata/baselines/reference/lspservertests/findAllRefs/special-handling-of-localness-when-using-object-literal-property.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,39 @@ Projects::
9898
[/user/username/projects/solution/api/tsconfig.json]
9999
/user/username/projects/solution/shared/src/index.ts
100100
/user/username/projects/solution/api/src/server.ts
101+
[/user/username/projects/solution/app/tsconfig.json] *new*
102+
/user/username/projects/solution/shared/src/index.ts
103+
/user/username/projects/solution/app/src/app.ts
101104
[/user/username/projects/solution/shared/tsconfig.json] *new*
102105
/user/username/projects/solution/shared/src/index.ts
103-
[/user/username/projects/solution/tsconfig.json]
106+
[/user/username/projects/solution/tsconfig.json] *modified*
104107
Config::
105-
[/user/username/projects/solution/api/tsconfig.json]
106-
RetainingProjects:
108+
[/user/username/projects/solution/api/tsconfig.json] *modified*
109+
RetainingProjects: *modified*
107110
/user/username/projects/solution/api/tsconfig.json
111+
/user/username/projects/solution/tsconfig.json *new*
108112
RetainingOpenFiles:
109113
/user/username/projects/solution/api/src/server.ts
114+
[/user/username/projects/solution/app/tsconfig.json] *new*
115+
RetainingProjects:
116+
/user/username/projects/solution/app/tsconfig.json
117+
/user/username/projects/solution/tsconfig.json
110118
[/user/username/projects/solution/shared/tsconfig.json] *modified*
111119
RetainingProjects: *modified*
112120
/user/username/projects/solution/api/tsconfig.json
121+
/user/username/projects/solution/app/tsconfig.json *new*
113122
/user/username/projects/solution/shared/tsconfig.json *new*
123+
/user/username/projects/solution/tsconfig.json *new*
124+
[/user/username/projects/solution/tsconfig.json] *new*
125+
RetainingProjects:
126+
/user/username/projects/solution/tsconfig.json
114127
// === /user/username/projects/solution/api/src/server.ts ===
115128
// import * as shared from "../../shared/dist"
116129
// shared.foo./*FIND ALL REFS*/[|baz|];
117130

131+
// === /user/username/projects/solution/app/src/app.ts ===
132+
// import * as shared from "../../shared/dist"
133+
// shared.foo.[|baz|];
134+
118135
// === /user/username/projects/solution/shared/src/index.ts ===
119136
// export const foo = { [|baz|]: "BAZ" };

0 commit comments

Comments
 (0)