Skip to content

Commit 96cc9b9

Browse files
authored
Make object literal properties new identifier locations when not contextually typed (microsoft#42612)
* Make object literal properties new identifier locations when not contextually typed * Fix completions after comma in object literal * Update other test
1 parent 71de94a commit 96cc9b9

6 files changed

+24
-14
lines changed

src/services/completions.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ namespace ts.Completions {
13871387

13881388
// Get all entities in the current scope.
13891389
completionKind = CompletionKind.Global;
1390-
isNewIdentifierLocation = isNewIdentifierDefinitionLocation(contextToken);
1390+
isNewIdentifierLocation = isNewIdentifierDefinitionLocation();
13911391

13921392
if (previousToken !== contextToken) {
13931393
Debug.assert(!!previousToken, "Expected 'contextToken' to be defined when different from 'previousToken'.");
@@ -1849,18 +1849,19 @@ namespace ts.Completions {
18491849
return false;
18501850
}
18511851

1852-
function isNewIdentifierDefinitionLocation(previousToken: Node | undefined): boolean {
1853-
if (previousToken) {
1854-
const containingNodeKind = previousToken.parent.kind;
1852+
function isNewIdentifierDefinitionLocation(): boolean {
1853+
if (contextToken) {
1854+
const containingNodeKind = contextToken.parent.kind;
18551855
// Previous token may have been a keyword that was converted to an identifier.
1856-
switch (keywordForNode(previousToken)) {
1856+
switch (keywordForNode(contextToken)) {
18571857
case SyntaxKind.CommaToken:
18581858
return containingNodeKind === SyntaxKind.CallExpression // func( a, |
18591859
|| containingNodeKind === SyntaxKind.Constructor // constructor( a, | /* public, protected, private keywords are allowed here, so show completion */
18601860
|| containingNodeKind === SyntaxKind.NewExpression // new C(a, |
18611861
|| containingNodeKind === SyntaxKind.ArrayLiteralExpression // [a, |
18621862
|| containingNodeKind === SyntaxKind.BinaryExpression // const x = (a, |
1863-
|| containingNodeKind === SyntaxKind.FunctionType; // var x: (s: string, list|
1863+
|| containingNodeKind === SyntaxKind.FunctionType // var x: (s: string, list|
1864+
|| containingNodeKind === SyntaxKind.ObjectLiteralExpression; // const obj = { x, |
18641865

18651866
case SyntaxKind.OpenParenToken:
18661867
return containingNodeKind === SyntaxKind.CallExpression // func( |
@@ -1882,7 +1883,8 @@ namespace ts.Completions {
18821883
return containingNodeKind === SyntaxKind.ModuleDeclaration; // module A.|
18831884

18841885
case SyntaxKind.OpenBraceToken:
1885-
return containingNodeKind === SyntaxKind.ClassDeclaration; // class A{ |
1886+
return containingNodeKind === SyntaxKind.ClassDeclaration // class A { |
1887+
|| containingNodeKind === SyntaxKind.ObjectLiteralExpression; // const obj = { |
18861888

18871889
case SyntaxKind.EqualsToken:
18881890
return containingNodeKind === SyntaxKind.VariableDeclaration // const x = a|

tests/cases/fourslash/completionListInObjectLiteral5.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
verify.completions({ marker: ["1"], excludes: ['obj'], includes: ['o'] });
2424
verify.completions({ marker: ["2"], excludes: ['obj1'], includes: ['o', 'obj'] });
2525
verify.completions({ marker: ["3"], includes: ['o', 'obj', 'obj1'] });
26-
verify.completions({ marker: ["4"], includes: ['o'], excludes: ['obj'] });
26+
verify.completions({ marker: ["4"], includes: ['o'], excludes: ['obj'], isNewIdentifierLocation: true });

tests/cases/fourslash/completionPropertyShorthandForObjectLiteral.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
//// f15({f/*15*/});
4545
//// f16({f/*16*/});
4646

47+
//// f1({ f1, /*17*/ });
48+
4749
const locals = [
4850
...(() => {
4951
const symbols = [];
@@ -56,11 +58,13 @@ const locals = [
5658
];
5759
verify.completions(
5860
// Non-contextual, any, unknown, object, Record<string, ..>, [key: string]: .., Type parameter, etc..
59-
{ marker: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"], exact: completion.globalsPlus(locals)},
61+
{ marker: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"], exact: completion.globalsPlus(locals), isNewIdentifierLocation: true },
6062
// Has named property
61-
{ marker: ["12", "13"], exact: "typed"},
63+
{ marker: ["12", "13"], exact: "typed", isNewIdentifierLocation: false },
6264
// Has both StringIndexType and named property
63-
{ marker: ["14"], exact: "prop", isNewIdentifierLocation: true},
65+
{ marker: ["14"], exact: "prop", isNewIdentifierLocation: true },
6466
// NumberIndexType
65-
{ marker: ["15", "16"], exact: [], isNewIdentifierLocation: true},
67+
{ marker: ["15", "16"], exact: [], isNewIdentifierLocation: true },
68+
// After comma
69+
{ marker: ["17"], exact: completion.globalsPlus(locals), isNewIdentifierLocation: true },
6670
);

tests/cases/fourslash/completionPropertyShorthandForObjectLiteral5.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
verify.completions({
1111
marker: "",
1212
exact: completion.globalsPlus(["foo"]),
13+
isNewIdentifierLocation: true,
1314
preferences: { includeCompletionsForModuleExports: true },
1415
});

tests/cases/fourslash/completionsGenericUnconstrained.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
verify.completions({
1212
marker: "",
13+
isNewIdentifierLocation: true,
1314
includes: [{
1415
name: "Object",
1516
sortText: completion.SortText.GlobalsOrKeywords

tests/cases/fourslash/completionsSelfDeclaring2.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99

1010
verify.completions({
1111
marker: "1",
12-
exact: completion.globalsPlus(["f1", "f2"])
12+
exact: completion.globalsPlus(["f1", "f2"]),
13+
isNewIdentifierLocation: true
1314
});
1415

1516
verify.completions({
1617
marker: "2",
17-
exact: ["xyz"]
18+
exact: ["xyz"],
19+
isNewIdentifierLocation: false
1820
});

0 commit comments

Comments
 (0)