Skip to content

Fix find-all-references on undefined #39591

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
merged 4 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 37 additions & 0 deletions src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,43 @@ namespace FourSlash {
}
}

public verifyBaselineFindAllReferences(markerName: string) {
const marker = this.getMarkerByName(markerName);
const references = this.languageService.findReferences(marker.fileName, marker.position);
const refsByFile = references
? ts.group(ts.sort(ts.flatMap(references, r => r.references), (a, b) => a.textSpan.start - b.textSpan.start), ref => ref.fileName)
: ts.emptyArray;

// Write input files
let baselineContent = "";
for (const group of refsByFile) {
baselineContent += getBaselineContentForFile(group[0].fileName, this.getFileContent(group[0].fileName));
baselineContent += "\n\n";
}

// Write response JSON
baselineContent += JSON.stringify(references, undefined, 2);
Harness.Baseline.runBaseline(this.getBaselineFileNameForContainingTestFile(".baseline.jsonc"), baselineContent);

function getBaselineContentForFile(fileName: string, content: string) {
let newContent = `=== ${fileName} ===\n`;
let pos = 0;
for (const { textSpan } of refsByFile.find(refs => refs[0].fileName === fileName) ?? ts.emptyArray) {
if (fileName === marker.fileName && ts.textSpanContainsPosition(textSpan, marker.position)) {
newContent += "/*FIND ALL REFS*/";
}
const end = textSpan.start + textSpan.length;
newContent += content.slice(pos, textSpan.start);
newContent += "[|";
newContent += content.slice(textSpan.start, end);
newContent += "|]";
pos = end;
}
newContent += content.slice(pos);
return newContent.split(/\r?\n/).map(l => "// " + l).join("\n");
}
}

public verifyNoReferences(markerNameOrRange?: string | Range) {
if (markerNameOrRange !== undefined) this.goToMarkerOrRange(markerNameOrRange);
const refs = this.getReferencesAtCaret();
Expand Down
4 changes: 4 additions & 0 deletions src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ namespace FourSlashInterface {
this.state.verifyTypeOfSymbolAtLocation(range, symbol, expected);
}

public baselineFindAllReferences(markerName: string) {
this.state.verifyBaselineFindAllReferences(markerName);
}

public referenceGroups(starts: ArrayOrSingle<string> | ArrayOrSingle<FourSlash.Range>, parts: ReferenceGroup[]) {
this.state.verifyReferenceGroups(starts, parts);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/findAllReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ namespace ts.FindAllReferences {
const { symbol } = def;
const { displayParts, kind } = getDefinitionKindAndDisplayParts(symbol, checker, originalNode);
const name = displayParts.map(p => p.text).join("");
const declaration = symbol.declarations ? first(symbol.declarations) : undefined;
const declaration = symbol.declarations && firstOrUndefined(symbol.declarations);
return {
node: declaration ?
getNameOfDeclaration(declaration) || declaration :
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// === /a.ts ===
// /*FIND ALL REFS*/[|undefined|];
//
// void [|undefined|];

// === /b.ts ===
// [|undefined|];

[
{
"definition": {
"containerKind": "",
"containerName": "",
"fileName": "/a.ts",
"kind": "var",
"name": "var undefined",
"textSpan": {
"start": 0,
"length": 9
},
"displayParts": [
{
"text": "var",
"kind": "keyword"
},
{
"text": " ",
"kind": "space"
},
{
"text": "undefined",
"kind": "propertyName"
}
]
},
"references": [
{
"textSpan": {
"start": 0,
"length": 9
},
"fileName": "/a.ts",
"isWriteAccess": false,
"isDefinition": false
},
{
"textSpan": {
"start": 17,
"length": 9
},
"fileName": "/a.ts",
"isWriteAccess": false,
"isDefinition": false
},
{
"textSpan": {
"start": 0,
"length": 9
},
"fileName": "/b.ts",
"isWriteAccess": false,
"isDefinition": false
}
]
}
]
11 changes: 11 additions & 0 deletions tests/cases/fourslash/findAllReferencesUndefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path="fourslash.ts" />

// @Filename: /a.ts
//// /**/undefined;
////
//// void undefined;

// @Filename: /b.ts
//// undefined;

verify.baselineFindAllReferences("");
1 change: 1 addition & 0 deletions tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ declare namespace FourSlashInterface {
goToType(startMarkerNames: ArrayOrSingle<string>, endMarkerNames: ArrayOrSingle<string>): void;
verifyGetEmitOutputForCurrentFile(expected: string): void;
verifyGetEmitOutputContentsForCurrentFile(expected: ts.OutputFile[]): void;
baselineFindAllReferences(markerName: string): void;
noReferences(markerNameOrRange?: string | Range): void;
symbolAtLocation(startRange: Range, ...declarationRanges: Range[]): void;
typeOfSymbolAtLocation(range: Range, symbol: any, expected: string): void;
Expand Down