Skip to content

Commit 14e5de3

Browse files
authored
Merge pull request #11647 from Microsoft/AddIsGlobalCompletionRestrictions
Add restrictions to IsGlobalCompletion
2 parents 12e7fab + dba0337 commit 14e5de3

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

src/services/completions.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// <reference path='../compiler/utilities.ts' />
2+
13
/* @internal */
24
namespace ts.Completions {
35
export function getCompletionsAtPosition(host: LanguageServiceHost, typeChecker: TypeChecker, log: (message: string) => void, compilerOptions: CompilerOptions, sourceFile: SourceFile, position: number): CompletionInfo {
@@ -951,7 +953,6 @@ namespace ts.Completions {
951953
if (!tryGetGlobalSymbols()) {
952954
return undefined;
953955
}
954-
isGlobalCompletion = true;
955956
}
956957

957958
log("getCompletionData: Semantic work: " + (timestamp() - semanticStart));
@@ -1030,15 +1031,13 @@ namespace ts.Completions {
10301031
if ((jsxContainer.kind === SyntaxKind.JsxSelfClosingElement) || (jsxContainer.kind === SyntaxKind.JsxOpeningElement)) {
10311032
// Cursor is inside a JSX self-closing element or opening element
10321033
attrsType = typeChecker.getJsxElementAttributesType(<JsxOpeningLikeElement>jsxContainer);
1033-
isGlobalCompletion = false;
10341034

10351035
if (attrsType) {
10361036
symbols = filterJsxAttributes(typeChecker.getPropertiesOfType(attrsType), (<JsxOpeningLikeElement>jsxContainer).attributes);
10371037
isMemberCompletion = true;
10381038
isNewIdentifierLocation = false;
10391039
return true;
10401040
}
1041-
10421041
}
10431042
}
10441043

@@ -1079,6 +1078,13 @@ namespace ts.Completions {
10791078
position;
10801079

10811080
const scopeNode = getScopeNode(contextToken, adjustedPosition, sourceFile) || sourceFile;
1081+
if (scopeNode) {
1082+
isGlobalCompletion =
1083+
scopeNode.kind === SyntaxKind.SourceFile ||
1084+
scopeNode.kind === SyntaxKind.TemplateExpression ||
1085+
scopeNode.kind === SyntaxKind.JsxExpression ||
1086+
isStatement(scopeNode);
1087+
}
10821088

10831089
/// TODO filter meaning based on the current context
10841090
const symbolMeanings = SymbolFlags.Type | SymbolFlags.Value | SymbolFlags.Namespace | SymbolFlags.Alias;
Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,74 @@
11
/// <reference path='fourslash.ts'/>
22

3+
// @Filename: file.ts
4+
////export var x = 10;
5+
////export var y = 10;
6+
////export default class C {
7+
////}
8+
9+
// @Filename: a.ts
10+
////import { /*1*/ } from "./file.ts"; // no globals in imports - export found
11+
312
//@Filename: file.tsx
4-
/////// <reference path="/*1*/..\services\services.ts" /> // no globals in reference paths
5-
////import { /*2*/ } from "./file.ts"; // no globals in imports
6-
////var test = "/*3*/"; // no globals in strings
7-
/////*4*/class A { // insert globals
13+
/////// <reference path="/*2*/..\services\services.ts" /> // no globals in reference paths
14+
////import { /*3*/ } from "./file1.ts"; // no globals in imports - export not found
15+
////var test = "/*4*/"; // no globals in strings
16+
/////*5*/class A { // insert globals
817
//// foo(): string { return ''; }
918
////}
1019
////
11-
////class /*5*/B extends A { // no globals after class keyword
20+
////class /*6*/B extends A { // no globals after class keyword
1221
//// bar(): string {
13-
//// /*6*/ // insert globals
22+
//// /*7*/ // insert globals
1423
//// return '';
1524
//// }
1625
////}
1726
////
18-
////class C</*7*/ U extends A, T extends A> { // no globals at beginning of generics
27+
////class C</*8*/ U extends A, T extends A> { // no globals at beginning of generics
1928
//// x: U;
20-
//// y = this./*8*/x; // no globals inserted for member completions
21-
//// /*9*/ // insert globals
29+
//// y = this./*9*/x; // no globals inserted for member completions
30+
//// /*10*/ // insert globals
2231
////}
23-
/////*10*/ // insert globals
24-
////const y = <div /*11*/ />;
32+
/////*11*/ // insert globals
33+
////const y = <div /*12*/ />; // no globals in jsx attribute found
34+
////const z = <div =/*13*/ />; // no globals in jsx attribute with syntax error
35+
////const x = `/*14*/ ${/*15*/}`; // globals only in template expression
36+
////var user = </*16*/User name=/*17*/{ /*18*/window.isLoggedIn ? window.name : '/*19*/'} />; // globals only in JSX expression (but not in JSX expression strings)
2537
goTo.marker("1");
2638
verify.completionListIsGlobal(false);
2739
goTo.marker("2");
2840
verify.completionListIsGlobal(false);
2941
goTo.marker("3");
3042
verify.completionListIsGlobal(false);
3143
goTo.marker("4");
32-
verify.completionListIsGlobal(true);
33-
goTo.marker("5");
3444
verify.completionListIsGlobal(false);
35-
goTo.marker("6");
45+
goTo.marker("5");
3646
verify.completionListIsGlobal(true);
37-
goTo.marker("7");
47+
goTo.marker("6");
3848
verify.completionListIsGlobal(false);
49+
goTo.marker("7");
50+
verify.completionListIsGlobal(true);
3951
goTo.marker("8");
4052
verify.completionListIsGlobal(false);
4153
goTo.marker("9");
42-
verify.completionListIsGlobal(true);
54+
verify.completionListIsGlobal(false);
4355
goTo.marker("10");
4456
verify.completionListIsGlobal(true);
4557
goTo.marker("11");
58+
verify.completionListIsGlobal(true);
59+
goTo.marker("12");
60+
verify.completionListIsGlobal(false);
61+
goTo.marker("13");
4662
verify.completionListIsGlobal(false);
63+
goTo.marker("14");
64+
verify.completionListIsGlobal(false);
65+
goTo.marker("15");
66+
verify.completionListIsGlobal(true);
67+
goTo.marker("16");
68+
verify.completionListIsGlobal(false);
69+
goTo.marker("17");
70+
verify.completionListIsGlobal(false);
71+
goTo.marker("18");
72+
verify.completionListIsGlobal(true);
73+
goTo.marker("19");
74+
verify.completionListIsGlobal(false);

0 commit comments

Comments
 (0)