Skip to content

Feat/exclude completions of variable initializers #42087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1bcbd8f
feat: exclude declared variable when Object literal completions
chenjigeng Dec 20, 2020
505b1b6
feat: check undeclareVariable when completion
chenjigeng Dec 20, 2020
c8079a4
feat: add completion test case
chenjigeng Dec 22, 2020
7f2434d
feat: code optimization
chenjigeng Dec 23, 2020
cade279
Merge branch 'master' of https://github.com/microsoft/TypeScript into…
chenjigeng Dec 23, 2020
fe0f923
feat: support shorthand property assignment
chenjigeng Dec 23, 2020
71c0db9
feat: add shorthand property assignment test case
chenjigeng Dec 23, 2020
43a8234
feat: update completionPropertyShorthandForObjectLiteral test cases
chenjigeng Dec 23, 2020
c4cd16e
feat: exclude completions of variable initializers
chenjigeng Dec 23, 2020
6f0f204
feat: update test cases
chenjigeng Dec 23, 2020
a3d4282
feat: add completionListWithoutVariableinitializer test case
chenjigeng Dec 23, 2020
ab3be82
feat: perfect the completionListWithoutVariableinitializer test case
chenjigeng Dec 23, 2020
b4dc72c
feat: remove isIdentifier limit
chenjigeng Dec 24, 2020
019bbfb
feat: update test cases
chenjigeng Dec 24, 2020
5695b34
feat: code optimization and filter out some binding cases
chenjigeng Dec 24, 2020
f6542e5
feat: update test case
chenjigeng Dec 24, 2020
cc6212f
feat: handle arrow function expressions without braces
chenjigeng Dec 29, 2020
5c2e314
feat: add arrow function expressions without braces test case
chenjigeng Dec 29, 2020
7b7cc24
feat: check node.parent exist first
chenjigeng Dec 29, 2020
0630cfe
feat: optimization name
chenjigeng Dec 30, 2020
578a1ce
feat: optimize test cases
chenjigeng Dec 30, 2020
52ee16a
chore: code formatting
chenjigeng Jan 13, 2021
86c30d0
feat: perfect type
chenjigeng Jan 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1504,13 +1504,21 @@ namespace ts.Completions {
: KeywordCompletionFilters.TypeKeywords;
}

const variableDeclaration = getVariableDeclaration(location);

filterMutate(symbols, symbol => {
if (!isSourceFile(location)) {
// export = /**/ here we want to get all meanings, so any symbol is ok
if (isExportAssignment(location.parent)) {
return true;
}

// Filter out variables from their own initializers
// `const a = /* no 'a' here */`
if (variableDeclaration && symbol.valueDeclaration === variableDeclaration) {
return false;
}

symbol = skipAlias(symbol, typeChecker);

// import m = /**/ <-- It can only access namespace (if typing import = x. this would get member symbols and not namespace)
Expand All @@ -1529,6 +1537,19 @@ namespace ts.Completions {
});
}

function getVariableDeclaration(property: Node): VariableDeclaration | undefined {
const variableDeclaration = findAncestor(property, node =>
isFunctionBlock(node) || isArrowFunctionBody(node) || isBindingPattern(node)
? "quit"
: isVariableDeclaration(node));

return variableDeclaration as VariableDeclaration | undefined;
}

function isArrowFunctionBody(node: Node) {
return node.parent && isArrowFunction(node.parent) && node.parent.body === node;
};

function isTypeOnlyCompletion(): boolean {
return insideJsDocTagTypeExpression
|| !isContextTokenValueLocation(contextToken) &&
Expand Down
3 changes: 2 additions & 1 deletion tests/cases/fourslash/commentsClassMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ verify.completions(
],
},
{ marker: ["29", "33", "38", "109"], includes: locals },
{ marker: ["35", "40", "87"], includes: locals, isNewIdentifierLocation: true },
{ marker: ["35", "40"], includes: locals, isNewIdentifierLocation: true },
{ marker: ["87"], includes: locals.filter(local => local === 'i1_s_p') , isNewIdentifierLocation: true },
{ marker: "31", includes: { name: "b", text: "(parameter) b: number", documentation: "number to add" } },
{ marker: "42", includes: { name: "value", text: "(parameter) value: number", documentation: "this is value" }, isNewIdentifierLocation: true },
{ marker: ["45", "52", "59"], includes: { name: "b", text: "(parameter) b: number" } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@

////var y = 10; y=/*var12*/

// first declaration
verify.completions({
marker: test.markers(),
marker: ["var1"],
exact: completion.globalsPlus(["y", "C"]),
isNewIdentifierLocation: true
});

verify.completions({
marker: ["var2", "var3", "var4", "var5", "var6", "var7", "var8", "var9", "var10", "var11", "var12"],
exact: completion.globalsPlus(["x", "y", "C"]),
isNewIdentifierLocation: true
});
26 changes: 26 additions & 0 deletions tests/cases/fourslash/completionListInObjectLiteral5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// <reference path="fourslash.ts" />

////const o = 'something'
////const obj = {
//// prop: o/*1*/,
//// pro() {
//// const obj1 = {
//// p:{
//// s: {
//// h: {
//// hh: o/*2*/
//// },
//// someFun() {
//// o/*3*/
//// }
//// }
//// }
//// }
//// },
//// o/*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'] });
9 changes: 5 additions & 4 deletions tests/cases/fourslash/completionListIsGlobalCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ verify.completions(
{ marker: ["1", "3", "6", "8", "12", "14"], exact: undefined, isGlobalCompletion: false },
{ marker: "2", exact: ["a.ts", "file.ts"], isGlobalCompletion: false, isNewIdentifierLocation: true },
{ marker: ["4", "19"], exact: [], isGlobalCompletion: false },
{ marker: ["5", "11", "18"], exact: globals, isGlobalCompletion: true },
{ marker: ["5", "11"], exact: globals, isGlobalCompletion: true },
{ marker: ["18"], exact: globals.filter(name => name !== 'user'), isGlobalCompletion: true },
{ marker: "7", exact: completion.globalsInsideFunction(x), isGlobalCompletion: true },
{ marker: "9", exact: ["x", "y"], isGlobalCompletion: false },
{ marker: "10", exact: completion.classElementKeywords, isGlobalCompletion: false, isNewIdentifierLocation: true },
{ marker: "13", exact: globals, isGlobalCompletion: false },
{ marker: "15", exact: globals, isGlobalCompletion: true, isNewIdentifierLocation: true },
{ marker: "16", exact: [...x, completion.globalThisEntry, ...completion.globalsVars, completion.undefinedVarEntry], isGlobalCompletion: false },
{ marker: "13", exact: globals.filter(name => name !== 'z'), isGlobalCompletion: false },
{ marker: "15", exact: globals.filter(name => name !== 'x'), isGlobalCompletion: true, isNewIdentifierLocation: true },
{ marker: "16", exact: [...x, completion.globalThisEntry, ...completion.globalsVars, completion.undefinedVarEntry].filter(name => name !== 'user'), isGlobalCompletion: false },
{ marker: "17", exact: completion.globalKeywords, isGlobalCompletion: false },
);
14 changes: 12 additions & 2 deletions tests/cases/fourslash/completionListWithMeanings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ const types: ReadonlyArray<FourSlashInterface.ExpectedCompletionEntry> = [
...completion.typeKeywords,
];

const filterValuesByName = (name: string) => {
return values.filter(entry => {
if (typeof entry === 'string') {
return entry !== name;
}

return entry.name !== name;
})
}

verify.completions(
{ marker: "valueExpr", exact: values, isNewIdentifierLocation: true },
{ marker: "valueExpr", exact: filterValuesByName('tt'), isNewIdentifierLocation: true },
{ marker: "typeExpr", exact: types, },
{ marker: "valueExprInObjectLiteral", exact: values },
{ marker: "valueExprInObjectLiteral", exact: filterValuesByName('yy') },
{ marker: "membertypeExpr", exact: [{ name: "point3", text: "interface m3.point3" }] },
{ marker: "membervalueExpr", exact: [{ name: "zz2", text: "var m3.zz2: number" }] },
);
62 changes: 62 additions & 0 deletions tests/cases/fourslash/completionListWithoutVariableinitializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/// <reference path='fourslash.ts' />

//// const a = a/*1*/;
//// const b = a && b/*2*/;
//// const c = [{ prop: [c/*3*/] }];
//// const d = () => { d/*4*/ };
//// const e = () => expression/*5*/
//// const f = { prop() { e/*6*/ } };
//// const fn = (p = /*7*/) => {}
//// const { g, h = /*8*/ } = { ... }
//// const [ g1, h1 = /*9*/ ] = [ ... ]

verify.completions({
marker: ["1"],
excludes: ["a"],
isNewIdentifierLocation: true,
});

verify.completions({
marker: ["2"],
excludes: ["b"],
includes: ["a"],
});

verify.completions({
marker: ["3"],
excludes: ["c"],
includes: ["a", "b"],
isNewIdentifierLocation: true,
});

verify.completions({
marker: ["4"],
includes: ["a", "b", "c", "d"],
});

verify.completions({
marker: ["5"],
includes: ["a", "b", "c", "d", "e"],
});

verify.completions({
marker: ["6"],
includes: ["a", "b", "c", "d", "e"],
});

verify.completions({
marker: ["7"],
includes: ["a", "b", "c", "d", "e"],
excludes: ["fn"],
});

verify.completions({
marker: ["8"],
includes: ["a", "b", "c", "d", "e", "fn"],
});

verify.completions({
marker: ["9"],
includes: ["a", "b", "c", "d", "e", "fn"],
});

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
//// };

verify.completions({
marker: test.markers(),
exact: completion.globalsPlus(["foo", "bar", "obj1", "obj2"]),
marker: ["1"],
exact: completion.globalsPlus(["foo", "bar", "obj2"]),
});

verify.completions({
marker: ["2"],
exact: completion.globalsPlus(["foo", "bar", "obj1"]),
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

verify.completions({
marker: ["1"],
exact: completion.globalsPlus(["foo", "bar", "obj"])
exact: completion.globalsPlus(["foo", "bar"]),
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

verify.completions({
marker: ["1"],
exact: completion.globalsPlus(["foo", "bar", "obj"])
exact: completion.globalsPlus(["foo", "bar"]),
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
//// export const exportedConstant = 0;

// @Filename: /b.ts
//// const foo = 'foo'
//// const obj = { exp/**/

verify.completions({
marker: "",
exact: completion.globalsPlus(["obj"]),
preferences: { includeCompletionsForModuleExports: true }
});
marker: "",
exact: completion.globalsPlus(["foo"]),
preferences: { includeCompletionsForModuleExports: true },
});
3 changes: 2 additions & 1 deletion tests/cases/fourslash/completionTypeAssertion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path="fourslash.ts" />

//// var x = this as/*1*/
//// var x = 'something'
//// var y = this as/*1*/

verify.completions({marker: "1", exact: completion.globalsPlus(["x"]) })
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
// 5, 6: Literal member completion after member name with empty member expression.
const exact = ["p1", "p2", "p3", "p4", ...completion.globalsPlus(["ObjectLiterals"])];
verify.completions(
{ marker: ["1",], exact, isNewIdentifierLocation: true },
{ marker: ["2", "3", "4", "5", "6"], exact }
{ marker: ["1",], exact: exact.filter(name => name !== 'p1'), isNewIdentifierLocation: true },
{ marker: ["2", "3"], exact: exact.filter(name => name !== 'p2') },
{ marker: ["4"], exact: exact.filter(name => name !== 'p3' ) },
{ marker: ["5", "6"], exact: exact.filter(name => name !== 'p4') },
);
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference path='fourslash.ts' />

//@Filename: file.tsx
//// var x = </**/;
//// var x = 'something'
//// var y = </**/;

verify.completions({ marker: "", exact: [completion.globalThisEntry, ...completion.globalsVars, "x", completion.undefinedVarEntry] });