From 581ad93ba23b7c6cf439c42e25fb530cde9b54f1 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 2 Feb 2021 10:26:21 -0800 Subject: [PATCH 1/3] Make object literal properties new identifier locations when not contextually typed --- src/services/completions.ts | 3 ++- .../completionPropertyShorthandForObjectLiteral.ts | 8 ++++---- .../completionPropertyShorthandForObjectLiteral5.ts | 1 + tests/cases/fourslash/completionsGenericUnconstrained.ts | 1 + tests/cases/fourslash/completionsSelfDeclaring2.ts | 6 ++++-- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/services/completions.ts b/src/services/completions.ts index e7b1a8e86bdd4..4d4d64182f0df 100644 --- a/src/services/completions.ts +++ b/src/services/completions.ts @@ -1882,7 +1882,8 @@ namespace ts.Completions { return containingNodeKind === SyntaxKind.ModuleDeclaration; // module A.| case SyntaxKind.OpenBraceToken: - return containingNodeKind === SyntaxKind.ClassDeclaration; // class A{ | + return containingNodeKind === SyntaxKind.ClassDeclaration // class A { | + || containingNodeKind === SyntaxKind.ObjectLiteralExpression; // const obj = { | case SyntaxKind.EqualsToken: return containingNodeKind === SyntaxKind.VariableDeclaration // const x = a| diff --git a/tests/cases/fourslash/completionPropertyShorthandForObjectLiteral.ts b/tests/cases/fourslash/completionPropertyShorthandForObjectLiteral.ts index 0f8f65e70ebd1..aca3740bd29ec 100644 --- a/tests/cases/fourslash/completionPropertyShorthandForObjectLiteral.ts +++ b/tests/cases/fourslash/completionPropertyShorthandForObjectLiteral.ts @@ -56,11 +56,11 @@ const locals = [ ]; verify.completions( // Non-contextual, any, unknown, object, Record, [key: string]: .., Type parameter, etc.. - { marker: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"], exact: completion.globalsPlus(locals)}, + { marker: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"], exact: completion.globalsPlus(locals), isNewIdentifierLocation: true }, // Has named property - { marker: ["12", "13"], exact: "typed"}, + { marker: ["12", "13"], exact: "typed", isNewIdentifierLocation: false }, // Has both StringIndexType and named property - { marker: ["14"], exact: "prop", isNewIdentifierLocation: true}, + { marker: ["14"], exact: "prop", isNewIdentifierLocation: true }, // NumberIndexType - { marker: ["15", "16"], exact: [], isNewIdentifierLocation: true}, + { marker: ["15", "16"], exact: [], isNewIdentifierLocation: true }, ); diff --git a/tests/cases/fourslash/completionPropertyShorthandForObjectLiteral5.ts b/tests/cases/fourslash/completionPropertyShorthandForObjectLiteral5.ts index 979b0b2afe767..d3002f56c4619 100644 --- a/tests/cases/fourslash/completionPropertyShorthandForObjectLiteral5.ts +++ b/tests/cases/fourslash/completionPropertyShorthandForObjectLiteral5.ts @@ -10,5 +10,6 @@ verify.completions({ marker: "", exact: completion.globalsPlus(["foo"]), + isNewIdentifierLocation: true, preferences: { includeCompletionsForModuleExports: true }, }); diff --git a/tests/cases/fourslash/completionsGenericUnconstrained.ts b/tests/cases/fourslash/completionsGenericUnconstrained.ts index d18991830e7dd..0d91b99f062f1 100644 --- a/tests/cases/fourslash/completionsGenericUnconstrained.ts +++ b/tests/cases/fourslash/completionsGenericUnconstrained.ts @@ -10,6 +10,7 @@ verify.completions({ marker: "", + isNewIdentifierLocation: true, includes: [{ name: "Object", sortText: completion.SortText.GlobalsOrKeywords diff --git a/tests/cases/fourslash/completionsSelfDeclaring2.ts b/tests/cases/fourslash/completionsSelfDeclaring2.ts index 7a46020a2c210..a068837c00701 100644 --- a/tests/cases/fourslash/completionsSelfDeclaring2.ts +++ b/tests/cases/fourslash/completionsSelfDeclaring2.ts @@ -9,10 +9,12 @@ verify.completions({ marker: "1", - exact: completion.globalsPlus(["f1", "f2"]) + exact: completion.globalsPlus(["f1", "f2"]), + isNewIdentifierLocation: true }); verify.completions({ marker: "2", - exact: ["xyz"] + exact: ["xyz"], + isNewIdentifierLocation: false }); From a7cc908e45613a8775757896ae5c55427ba50acb Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 2 Feb 2021 14:10:06 -0800 Subject: [PATCH 2/3] Fix completions after comma in object literal --- src/services/completions.ts | 13 +++++++------ .../completionPropertyShorthandForObjectLiteral.ts | 4 ++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/services/completions.ts b/src/services/completions.ts index 4d4d64182f0df..a59f637a96eca 100644 --- a/src/services/completions.ts +++ b/src/services/completions.ts @@ -1387,7 +1387,7 @@ namespace ts.Completions { // Get all entities in the current scope. completionKind = CompletionKind.Global; - isNewIdentifierLocation = isNewIdentifierDefinitionLocation(contextToken); + isNewIdentifierLocation = isNewIdentifierDefinitionLocation(); if (previousToken !== contextToken) { Debug.assert(!!previousToken, "Expected 'contextToken' to be defined when different from 'previousToken'."); @@ -1849,18 +1849,19 @@ namespace ts.Completions { return false; } - function isNewIdentifierDefinitionLocation(previousToken: Node | undefined): boolean { - if (previousToken) { - const containingNodeKind = previousToken.parent.kind; + function isNewIdentifierDefinitionLocation(): boolean { + if (contextToken) { + const containingNodeKind = contextToken.parent.kind; // Previous token may have been a keyword that was converted to an identifier. - switch (keywordForNode(previousToken)) { + switch (keywordForNode(contextToken)) { case SyntaxKind.CommaToken: return containingNodeKind === SyntaxKind.CallExpression // func( a, | || containingNodeKind === SyntaxKind.Constructor // constructor( a, | /* public, protected, private keywords are allowed here, so show completion */ || containingNodeKind === SyntaxKind.NewExpression // new C(a, | || containingNodeKind === SyntaxKind.ArrayLiteralExpression // [a, | || containingNodeKind === SyntaxKind.BinaryExpression // const x = (a, | - || containingNodeKind === SyntaxKind.FunctionType; // var x: (s: string, list| + || containingNodeKind === SyntaxKind.FunctionType // var x: (s: string, list| + || containingNodeKind === SyntaxKind.ObjectLiteralExpression; // const obj = { x, | case SyntaxKind.OpenParenToken: return containingNodeKind === SyntaxKind.CallExpression // func( | diff --git a/tests/cases/fourslash/completionPropertyShorthandForObjectLiteral.ts b/tests/cases/fourslash/completionPropertyShorthandForObjectLiteral.ts index aca3740bd29ec..8eae3df67a698 100644 --- a/tests/cases/fourslash/completionPropertyShorthandForObjectLiteral.ts +++ b/tests/cases/fourslash/completionPropertyShorthandForObjectLiteral.ts @@ -44,6 +44,8 @@ //// f15({f/*15*/}); //// f16({f/*16*/}); +//// f1({ f1, /*17*/ }); + const locals = [ ...(() => { const symbols = []; @@ -63,4 +65,6 @@ verify.completions( { marker: ["14"], exact: "prop", isNewIdentifierLocation: true }, // NumberIndexType { marker: ["15", "16"], exact: [], isNewIdentifierLocation: true }, + // After comma + { marker: ["17"], exact: completion.globalsPlus(locals), isNewIdentifierLocation: true }, ); From d158c25b1ed131487d28ce22b345ff188aff9adb Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 2 Feb 2021 15:49:34 -0800 Subject: [PATCH 3/3] Update other test --- tests/cases/fourslash/completionListInObjectLiteral5.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cases/fourslash/completionListInObjectLiteral5.ts b/tests/cases/fourslash/completionListInObjectLiteral5.ts index eb245d748bf41..2cbb1699cc79f 100644 --- a/tests/cases/fourslash/completionListInObjectLiteral5.ts +++ b/tests/cases/fourslash/completionListInObjectLiteral5.ts @@ -23,4 +23,4 @@ verify.completions({ marker: ["1"], excludes: ['obj'], includes: ['o'] }); verify.completions({ marker: ["2"], excludes: ['obj1'], includes: ['o', 'obj'] }); verify.completions({ marker: ["3"], includes: ['o', 'obj', 'obj1'] }); -verify.completions({ marker: ["4"], includes: ['o'], excludes: ['obj'] }); \ No newline at end of file +verify.completions({ marker: ["4"], includes: ['o'], excludes: ['obj'], isNewIdentifierLocation: true }); \ No newline at end of file