From 21cd68dd04ff62238d05a9ca2832d61bfc83201a Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 27 Mar 2018 13:05:41 -0700 Subject: [PATCH] Test for isNewIdentifierLocation, and make true for type with index signature (#22910) --- src/harness/fourslash.ts | 4 ++-- src/services/completions.ts | 2 ++ tests/cases/fourslash/completionForStringLiteral7.ts | 8 +++++--- ...mpletionListInClosedObjectTypeLiteralInSignature04.ts | 8 +------- tests/cases/fourslash/completionListInImportClause05.ts | 2 +- tests/cases/fourslash/completionListInImportClause06.ts | 2 +- ...letionListInUnclosedObjectTypeLiteralInSignature04.ts | 2 +- .../cases/fourslash/completionListInvalidMemberNames.ts | 3 ++- .../completionListWithModulesInsideModuleScope.ts | 4 ++-- tests/cases/fourslash/completionsInterfaceElement.ts | 2 +- tests/cases/fourslash/completionsJsPropertyAssignment.ts | 3 ++- tests/cases/fourslash/completionsJsdocTypeTagCast.ts | 2 +- tests/cases/fourslash/completionsPaths.ts | 9 +++++---- tests/cases/fourslash/completionsPaths_pathMapping.ts | 4 ++-- .../completionsPaths_pathMapping_parentDirectory.ts | 2 +- .../completionsPaths_pathMapping_relativePath.ts | 2 +- .../fourslash/completionsPaths_pathMapping_topLevel.ts | 2 +- .../completionsStringLiteral_fromTypeConstraint.ts | 3 ++- tests/cases/fourslash/completionsUnion.ts | 3 ++- 19 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index f209cafb8a1..7f36e326f57 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -840,8 +840,8 @@ namespace FourSlash { this.raiseError(`No completions at position '${this.currentCaretPosition}'.`); } - if (options && options.isNewIdentifierLocation !== undefined && actualCompletions.isNewIdentifierLocation !== options.isNewIdentifierLocation) { - this.raiseError(`Expected 'isNewIdentifierLocation' to be ${options.isNewIdentifierLocation}, got ${actualCompletions.isNewIdentifierLocation}`); + if (actualCompletions.isNewIdentifierLocation !== (options && options.isNewIdentifierLocation || false)) { + this.raiseError(`Expected 'isNewIdentifierLocation' to be ${options && options.isNewIdentifierLocation}, got ${actualCompletions.isNewIdentifierLocation}`); } const actual = actualCompletions.entries; diff --git a/src/services/completions.ts b/src/services/completions.ts index d7eb2e2d277..c30a302dc09 100644 --- a/src/services/completions.ts +++ b/src/services/completions.ts @@ -1031,6 +1031,8 @@ namespace ts.Completions { } function addTypeProperties(type: Type): void { + isNewIdentifierLocation = !!type.getStringIndexType() || !!type.getNumberIndexType(); + if (isSourceFileJavaScript(sourceFile)) { // In javascript files, for union types, we don't just get the members that // the individual types have in common, we also include all the members that diff --git a/tests/cases/fourslash/completionForStringLiteral7.ts b/tests/cases/fourslash/completionForStringLiteral7.ts index b2d250ac3fd..6b7bead2a66 100644 --- a/tests/cases/fourslash/completionForStringLiteral7.ts +++ b/tests/cases/fourslash/completionForStringLiteral7.ts @@ -5,6 +5,8 @@ ////function f(x: T, ...args: U[]) { }; ////f("/*1*/", "/*2*/", "/*3*/"); -verify.completionsAt("1", ["foo", "bar"]); -verify.completionsAt("2", ["oof", "rab"]); -verify.completionsAt("3", ["oof", "rab"]); +// TODO: GH#22907 +const options = { isNewIdentifierLocation: true }; +verify.completionsAt("1", ["foo", "bar"], options); +verify.completionsAt("2", ["oof", "rab"], options); +verify.completionsAt("3", ["oof", "rab"], options); diff --git a/tests/cases/fourslash/completionListInClosedObjectTypeLiteralInSignature04.ts b/tests/cases/fourslash/completionListInClosedObjectTypeLiteralInSignature04.ts index 14ae0e155d8..8674124f05f 100644 --- a/tests/cases/fourslash/completionListInClosedObjectTypeLiteralInSignature04.ts +++ b/tests/cases/fourslash/completionListInClosedObjectTypeLiteralInSignature04.ts @@ -7,10 +7,4 @@ //// ////declare function foo(obj: I): { /*1*/ } -goTo.marker("1"); - -verify.not.completionListContains("I"); -verify.not.completionListContains("TString"); -verify.not.completionListContains("TNumber"); -verify.not.completionListContains("foo"); -verify.not.completionListContains("obj"); +verify.completionsAt("1", ["readonly"], { isNewIdentifierLocation: true }); diff --git a/tests/cases/fourslash/completionListInImportClause05.ts b/tests/cases/fourslash/completionListInImportClause05.ts index f7b5244d5a4..2b5d66621dd 100644 --- a/tests/cases/fourslash/completionListInImportClause05.ts +++ b/tests/cases/fourslash/completionListInImportClause05.ts @@ -17,4 +17,4 @@ verify.completionsAt("1", [ { name: "@a/b", replacementSpan }, { name: "@c/d", replacementSpan }, { name: "@e/f", replacementSpan }, -]); +], { isNewIdentifierLocation: true }); diff --git a/tests/cases/fourslash/completionListInImportClause06.ts b/tests/cases/fourslash/completionListInImportClause06.ts index f6cfefa437f..9336803f7f9 100644 --- a/tests/cases/fourslash/completionListInImportClause06.ts +++ b/tests/cases/fourslash/completionListInImportClause06.ts @@ -14,4 +14,4 @@ // Confirm that entries are de-dup'd. verify.completionsAt("1", [ { name: "@a/b", replacementSpan: test.ranges()[0] }, -]); +], { isNewIdentifierLocation: true }); diff --git a/tests/cases/fourslash/completionListInUnclosedObjectTypeLiteralInSignature04.ts b/tests/cases/fourslash/completionListInUnclosedObjectTypeLiteralInSignature04.ts index d6746803ab8..5a9731ca0e3 100644 --- a/tests/cases/fourslash/completionListInUnclosedObjectTypeLiteralInSignature04.ts +++ b/tests/cases/fourslash/completionListInUnclosedObjectTypeLiteralInSignature04.ts @@ -7,4 +7,4 @@ //// ////declare function foo(obj: I): { /*1*/ -verify.completionsAt("1", ["readonly"]); +verify.completionsAt("1", ["readonly"], { isNewIdentifierLocation: true }); diff --git a/tests/cases/fourslash/completionListInvalidMemberNames.ts b/tests/cases/fourslash/completionListInvalidMemberNames.ts index 3dc54274e83..291ea945b29 100644 --- a/tests/cases/fourslash/completionListInvalidMemberNames.ts +++ b/tests/cases/fourslash/completionListInvalidMemberNames.ts @@ -14,7 +14,8 @@ ////x[|./*a*/|]; ////x["/*b*/"]; -verify.completionsAt("b", ["foo ", "bar", "break", "any", "#", "$", "b", "1b"]); +// TODO: GH#22907 +verify.completionsAt("b", ["foo ", "bar", "break", "any", "#", "$", "b", "1b"], { isNewIdentifierLocation: true }); const replacementSpan = test.ranges()[0]; verify.completionsAt("a", [ diff --git a/tests/cases/fourslash/completionListWithModulesInsideModuleScope.ts b/tests/cases/fourslash/completionListWithModulesInsideModuleScope.ts index d13141c38cb..682ef9b453e 100644 --- a/tests/cases/fourslash/completionListWithModulesInsideModuleScope.ts +++ b/tests/cases/fourslash/completionListWithModulesInsideModuleScope.ts @@ -311,7 +311,7 @@ goToMarkAndGeneralVerify('class', { isClassScope: true }); //verify.not.completionListContains('ceVar'); // from interface in mod1 -verify.completionsAt("interface", ["readonly"]); +verify.completionsAt("interface", ["readonly"], { isNewIdentifierLocation: true }); // from namespace in mod1 verifyNamespaceInMod1('namespace'); @@ -348,7 +348,7 @@ verify.not.completionListContains('ceFunc'); verify.not.completionListContains('ceVar'); // from exported interface in mod1 -verify.completionsAt("exportedInterface", ["readonly"]); +verify.completionsAt("exportedInterface", ["readonly"], { isNewIdentifierLocation: true }); // from exported namespace in mod1 verifyExportedNamespace('exportedNamespace'); diff --git a/tests/cases/fourslash/completionsInterfaceElement.ts b/tests/cases/fourslash/completionsInterfaceElement.ts index fe7d55db794..cbf15581dc0 100644 --- a/tests/cases/fourslash/completionsInterfaceElement.ts +++ b/tests/cases/fourslash/completionsInterfaceElement.ts @@ -14,5 +14,5 @@ ////interface EndOfFile { f; /*e*/ for (const marker of test.markerNames()) { - verify.completionsAt(marker, ["readonly"]); + verify.completionsAt(marker, ["readonly"], { isNewIdentifierLocation: true }); } diff --git a/tests/cases/fourslash/completionsJsPropertyAssignment.ts b/tests/cases/fourslash/completionsJsPropertyAssignment.ts index c7e8a9ad268..fa758188ea9 100644 --- a/tests/cases/fourslash/completionsJsPropertyAssignment.ts +++ b/tests/cases/fourslash/completionsJsPropertyAssignment.ts @@ -7,4 +7,5 @@ ////const x = { p: "x" }; ////x.p = "/**/"; -verify.completionsAt("", ["x", "y"]); +// TODO: GH#22907 +verify.completionsAt("", ["x", "y"], { isNewIdentifierLocation: true }); diff --git a/tests/cases/fourslash/completionsJsdocTypeTagCast.ts b/tests/cases/fourslash/completionsJsdocTypeTagCast.ts index 822069feb53..8cd78103e34 100644 --- a/tests/cases/fourslash/completionsJsdocTypeTagCast.ts +++ b/tests/cases/fourslash/completionsJsdocTypeTagCast.ts @@ -4,4 +4,4 @@ // @Filename: /a.js ////const x = /** @type {{ s: string }} */ ({ /**/ }); -verify.completionsAt("", ["s", "x"]); +verify.completionsAt("", ["s", "x"], { isNewIdentifierLocation: true }); diff --git a/tests/cases/fourslash/completionsPaths.ts b/tests/cases/fourslash/completionsPaths.ts index 6bb2fb38706..9b485d60ac3 100644 --- a/tests/cases/fourslash/completionsPaths.ts +++ b/tests/cases/fourslash/completionsPaths.ts @@ -24,7 +24,8 @@ ////const foo = require(`x/[|/*4*/|]`); const [r0, r1, r2, r3] = test.ranges(); -verify.completionsAt("1", [{ name: "y", replacementSpan: r0 }, { name: "x", replacementSpan: r0 }]); -verify.completionsAt("2", [{ name: "bar", replacementSpan: r1 }, { name: "foo", replacementSpan: r1 }]); -verify.completionsAt("3", [{ name: "bar", replacementSpan: r2 }, { name: "foo", replacementSpan: r2 }]); -verify.completionsAt("4", [{ name: "bar", replacementSpan: r3 }, { name: "foo", replacementSpan: r3 }]); +const options = { isNewIdentifierLocation: true }; +verify.completionsAt("1", [{ name: "y", replacementSpan: r0 }, { name: "x", replacementSpan: r0 }], options); +verify.completionsAt("2", [{ name: "bar", replacementSpan: r1 }, { name: "foo", replacementSpan: r1 }], options); +verify.completionsAt("3", [{ name: "bar", replacementSpan: r2 }, { name: "foo", replacementSpan: r2 }], options); +verify.completionsAt("4", [{ name: "bar", replacementSpan: r3 }, { name: "foo", replacementSpan: r3 }], options); diff --git a/tests/cases/fourslash/completionsPaths_pathMapping.ts b/tests/cases/fourslash/completionsPaths_pathMapping.ts index 1c222545ae6..200c6d25569 100644 --- a/tests/cases/fourslash/completionsPaths_pathMapping.ts +++ b/tests/cases/fourslash/completionsPaths_pathMapping.ts @@ -21,5 +21,5 @@ ////} const [r0, r1] = test.ranges(); -verify.completionsAt("0", ["a", "b", "dir"].map(name => ({ name, replacementSpan: r0 }))); -verify.completionsAt("1", ["x"].map(name => ({ name, replacementSpan: r1 }))); +verify.completionsAt("0", ["a", "b", "dir"].map(name => ({ name, replacementSpan: r0 })), { isNewIdentifierLocation: true }); +verify.completionsAt("1", ["x"].map(name => ({ name, replacementSpan: r1 })), { isNewIdentifierLocation: true }); diff --git a/tests/cases/fourslash/completionsPaths_pathMapping_parentDirectory.ts b/tests/cases/fourslash/completionsPaths_pathMapping_parentDirectory.ts index 7dcd6e3adb1..6736256cc0f 100644 --- a/tests/cases/fourslash/completionsPaths_pathMapping_parentDirectory.ts +++ b/tests/cases/fourslash/completionsPaths_pathMapping_parentDirectory.ts @@ -17,4 +17,4 @@ ////} const [replacementSpan] = test.ranges(); -verify.completionsAt("", [{ name: "x", replacementSpan }]); +verify.completionsAt("", [{ name: "x", replacementSpan }], { isNewIdentifierLocation: true }); diff --git a/tests/cases/fourslash/completionsPaths_pathMapping_relativePath.ts b/tests/cases/fourslash/completionsPaths_pathMapping_relativePath.ts index c24d3690298..659d3352755 100644 --- a/tests/cases/fourslash/completionsPaths_pathMapping_relativePath.ts +++ b/tests/cases/fourslash/completionsPaths_pathMapping_relativePath.ts @@ -20,4 +20,4 @@ ////} const [replacementSpan] = test.ranges(); -verify.completionsAt("", ["a", "b"].map(name => ({ name, replacementSpan }))); +verify.completionsAt("", ["a", "b"].map(name => ({ name, replacementSpan })), { isNewIdentifierLocation: true }); diff --git a/tests/cases/fourslash/completionsPaths_pathMapping_topLevel.ts b/tests/cases/fourslash/completionsPaths_pathMapping_topLevel.ts index 963851f5bc6..b99d6be4ca9 100644 --- a/tests/cases/fourslash/completionsPaths_pathMapping_topLevel.ts +++ b/tests/cases/fourslash/completionsPaths_pathMapping_topLevel.ts @@ -14,4 +14,4 @@ ////} const [replacementSpan] = test.ranges(); -verify.completionsAt("", ["src", "foo/"].map(name => ({ name, replacementSpan }))); +verify.completionsAt("", ["src", "foo/"].map(name => ({ name, replacementSpan })), { isNewIdentifierLocation: true }); diff --git a/tests/cases/fourslash/completionsStringLiteral_fromTypeConstraint.ts b/tests/cases/fourslash/completionsStringLiteral_fromTypeConstraint.ts index cb3f1a31fe2..18941eb35ea 100644 --- a/tests/cases/fourslash/completionsStringLiteral_fromTypeConstraint.ts +++ b/tests/cases/fourslash/completionsStringLiteral_fromTypeConstraint.ts @@ -3,4 +3,5 @@ ////interface Foo { foo: string; bar: string; } ////type T = Pick; -verify.completionsAt("", ["foo", "bar"]); +// TODO: GH#22907 +verify.completionsAt("", ["foo", "bar"], { isNewIdentifierLocation: true }); diff --git a/tests/cases/fourslash/completionsUnion.ts b/tests/cases/fourslash/completionsUnion.ts index d9d1d3c792f..4ee701dddbe 100644 --- a/tests/cases/fourslash/completionsUnion.ts +++ b/tests/cases/fourslash/completionsUnion.ts @@ -7,4 +7,5 @@ // We specifically filter out any array-like types. // Private members will be excluded by `createUnionOrIntersectionProperty`. -verify.completionsAt("", ["x"]); +// TODO: GH#22907 +verify.completionsAt("", ["x"], { isNewIdentifierLocation: true });