Skip to content

Commit da2aa97

Browse files
committed
Revert to using spread instead of mutating value later
1 parent a84ed93 commit da2aa97

File tree

8 files changed

+115
-127
lines changed

8 files changed

+115
-127
lines changed

src/harness/client.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,14 @@ namespace ts.server {
396396
for (const entry of body.locs) {
397397
const fileName = entry.file;
398398
for (const { start, end, declarationStart, declarationEnd, ...prefixSuffixText } of entry.locs) {
399-
const renameLocation: RenameLocation = { textSpan: this.decodeSpan({ start, end }, fileName), fileName, ...prefixSuffixText };
400-
if (declarationStart !== undefined) {
401-
renameLocation.declarationSpan = this.decodeSpan({ start: declarationStart, end: declarationEnd! }, fileName);
402-
}
403-
locations.push(renameLocation);
399+
locations.push({
400+
textSpan: this.decodeSpan({ start, end }, fileName),
401+
fileName,
402+
...(declarationStart !== undefined ?
403+
{ declarationSpan: this.decodeSpan({ start: declarationStart, end: declarationEnd! }, fileName) } :
404+
undefined),
405+
...prefixSuffixText
406+
});
404407
}
405408
}
406409

src/harness/fourslash.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -957,17 +957,16 @@ namespace FourSlash {
957957
definition: typeof definition === "string" ? definition : { ...definition, range: ts.createTextSpanFromRange(definition.range) },
958958
references: ranges.map<ts.ReferenceEntry>(r => {
959959
const { isWriteAccess = false, isDefinition = false, isInString, declarationRangeIndex } = (r.marker && r.marker.data || {}) as { isWriteAccess?: boolean, isDefinition?: boolean, isInString?: true, declarationRangeIndex?: number };
960-
const result: ts.ReferenceEntry = {
960+
return {
961961
fileName: r.fileName,
962962
textSpan: ts.createTextSpanFromRange(r),
963963
isWriteAccess,
964964
isDefinition,
965+
...(declarationRangeIndex !== undefined ?
966+
{ declarationSpan: ts.createTextSpanFromRange(this.getRanges()[declarationRangeIndex]) } :
967+
undefined),
965968
...(isInString ? { isInString: true } : undefined),
966969
};
967-
if (declarationRangeIndex !== undefined) {
968-
result.declarationSpan = ts.createTextSpanFromRange(this.getRanges()[declarationRangeIndex]);
969-
}
970-
return result;
971970
}),
972971
}));
973972

@@ -1192,14 +1191,17 @@ Actual: ${stringify(fullActual)}`);
11921191

11931192
const sort = (locations: ReadonlyArray<ts.RenameLocation> | undefined) =>
11941193
locations && ts.sort(locations, (r1, r2) => ts.compareStringsCaseSensitive(r1.fileName, r2.fileName) || r1.textSpan.start - r2.textSpan.start);
1195-
assert.deepEqual(sort(references), sort(ranges.map(rangeOrOptions => {
1194+
assert.deepEqual(sort(references), sort(ranges.map((rangeOrOptions): ts.RenameLocation => {
11961195
const { range, ...prefixSuffixText } = "range" in rangeOrOptions ? rangeOrOptions : { range: rangeOrOptions };
1197-
const result: ts.RenameLocation = { fileName: range.fileName, textSpan: ts.createTextSpanFromRange(range), ...prefixSuffixText };
11981196
const { declarationRangeIndex } = (range.marker && range.marker.data || {}) as { declarationRangeIndex?: number; };
1199-
if (declarationRangeIndex !== undefined) {
1200-
result.declarationSpan = ts.createTextSpanFromRange(this.getRanges()[declarationRangeIndex]);
1201-
}
1202-
return result;
1197+
return {
1198+
fileName: range.fileName,
1199+
textSpan: ts.createTextSpanFromRange(range),
1200+
...(declarationRangeIndex !== undefined ?
1201+
{ declarationSpan: ts.createTextSpanFromRange(this.getRanges()[declarationRangeIndex]) } :
1202+
undefined),
1203+
...prefixSuffixText
1204+
};
12031205
})));
12041206
}
12051207
}

src/server/session.ts

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,14 +1066,11 @@ namespace ts.server {
10661066
}
10671067

10681068
private toDeclarationFileSpan(fileName: string, textSpan: TextSpan, declarationSpan: TextSpan | undefined, project: Project): protocol.DeclarationFileSpan {
1069-
const result = this.toFileSpan(fileName, textSpan, project) as protocol.DeclarationFileSpan;
1070-
if (declarationSpan) {
1071-
const declaration = this.toFileSpan(fileName, declarationSpan, project);
1072-
result.declarationStart = declaration.start;
1073-
result.declarationEnd = declaration.end;
1074-
}
1075-
return result;
1076-
1069+
const fileSpan = this.toFileSpan(fileName, textSpan, project);
1070+
const declaration = declarationSpan && this.toFileSpan(fileName, declarationSpan, project);
1071+
return declaration ?
1072+
{ ...fileSpan, declarationStart: declaration.start, declarationEnd: declaration.end } :
1073+
fileSpan;
10771074
}
10781075

10791076
private getTypeDefinition(args: protocol.FileLocationRequestArgs): ReadonlyArray<protocol.DeclarationFileSpan> {
@@ -1099,36 +1096,27 @@ namespace ts.server {
10991096
const { file, project } = this.getFileAndProject(args);
11001097
const position = this.getPositionInFile(args, file);
11011098
const implementations = this.mapImplementationLocations(project.getLanguageService().getImplementationAtPosition(file, position) || emptyArray, project);
1102-
if (simplifiedResult) {
1103-
return implementations.map(({ fileName, textSpan, declarationSpan }) => this.toDeclarationFileSpan(fileName, textSpan, declarationSpan, project));
1104-
}
1105-
1106-
return implementations.map(Session.mapToOriginalLocation);
1099+
return simplifiedResult ?
1100+
implementations.map(({ fileName, textSpan, declarationSpan }) => this.toDeclarationFileSpan(fileName, textSpan, declarationSpan, project)) :
1101+
implementations.map(Session.mapToOriginalLocation);
11071102
}
11081103

11091104
private getOccurrences(args: protocol.FileLocationRequestArgs): ReadonlyArray<protocol.OccurrencesResponseItem> {
11101105
const { file, project } = this.getFileAndProject(args);
1111-
11121106
const position = this.getPositionInFile(args, file);
1113-
11141107
const occurrences = project.getLanguageService().getOccurrencesAtPosition(file, position);
1115-
1116-
if (!occurrences) {
1117-
return emptyArray;
1118-
}
1119-
1120-
return occurrences.map(occurrence => {
1121-
const { fileName, isWriteAccess, textSpan, isInString, declarationSpan } = occurrence;
1122-
const scriptInfo = project.getScriptInfo(fileName)!;
1123-
const result = toProtocolDeclarationTextSpan(textSpan, declarationSpan, scriptInfo) as protocol.OccurrencesResponseItem;
1124-
result.file = fileName;
1125-
result.isWriteAccess = isWriteAccess;
1126-
// no need to serialize the property if it is not true
1127-
if (isInString) {
1128-
result.isInString = isInString;
1129-
}
1130-
return result;
1131-
});
1108+
return occurrences ?
1109+
occurrences.map<protocol.OccurrencesResponseItem>(occurrence => {
1110+
const { fileName, isWriteAccess, textSpan, isInString, declarationSpan } = occurrence;
1111+
const scriptInfo = project.getScriptInfo(fileName)!;
1112+
return {
1113+
...toProtocolDeclarationTextSpan(textSpan, declarationSpan, scriptInfo),
1114+
file: fileName,
1115+
isWriteAccess,
1116+
...(isInString ? { isInString } : undefined)
1117+
};
1118+
}) :
1119+
emptyArray;
11321120
}
11331121

11341122
private getSyntacticDiagnosticsSync(args: protocol.SyntacticDiagnosticsSyncRequestArgs): ReadonlyArray<protocol.Diagnostic> | ReadonlyArray<protocol.DiagnosticWithLinePosition> {
@@ -1178,11 +1166,10 @@ namespace ts.server {
11781166
const scriptInfo = project.getScriptInfo(fileName)!;
11791167
return {
11801168
file: fileName,
1181-
highlightSpans: highlightSpans.map(({ textSpan, kind, declarationSpan }) => {
1182-
const result = toProtocolDeclarationTextSpan(textSpan, declarationSpan, scriptInfo) as protocol.HighlightSpan;
1183-
result.kind = kind;
1184-
return result;
1185-
})
1169+
highlightSpans: highlightSpans.map(({ textSpan, kind, declarationSpan }) => ({
1170+
...toProtocolDeclarationTextSpan(textSpan, declarationSpan, scriptInfo),
1171+
kind
1172+
}))
11861173
};
11871174
});
11881175
}
@@ -1328,8 +1315,7 @@ namespace ts.server {
13281315
isDefinition
13291316
};
13301317
}));
1331-
const result: protocol.ReferencesResponseBody = { refs, symbolName, symbolStartOffset, symbolDisplayString };
1332-
return result;
1318+
return { refs, symbolName, symbolStartOffset, symbolDisplayString };
13331319
}
13341320
/**
13351321
* @param fileName is the name of the file to be opened
@@ -2580,11 +2566,11 @@ namespace ts.server {
25802566
}
25812567

25822568
function toProtocolDeclarationTextSpan(span: TextSpan, declarationSpan: TextSpan | undefined, scriptInfo: ScriptInfo): protocol.DeclarationTextSpan {
2583-
const result = toProcolTextSpan(span, scriptInfo) as protocol.DeclarationTextSpan;
2584-
if (!declarationSpan) return result;
2585-
result.declarationStart = scriptInfo.positionToLineOffset(declarationSpan.start);
2586-
result.declarationEnd = scriptInfo.positionToLineOffset(textSpanEnd(declarationSpan));
2587-
return result;
2569+
const textSpan = toProcolTextSpan(span, scriptInfo);
2570+
const declarationTextSpan = declarationSpan && toProcolTextSpan(declarationSpan, scriptInfo);
2571+
return declarationTextSpan ?
2572+
{ ...textSpan, declarationStart: declarationTextSpan.start, declarationEnd: declarationTextSpan.end } :
2573+
textSpan;
25882574
}
25892575

25902576
function convertTextChangeToCodeEdit(change: TextChange, scriptInfo: ScriptInfoOrConfig): protocol.CodeEdit {

src/services/findAllReferences.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,14 @@ namespace ts.FindAllReferences {
161161
}
162162
}
163163

164-
export function setDeclarationSpan(span: DocumentSpan, sourceFile: SourceFile, declaration?: DeclarationNode) {
165-
if (declaration) {
166-
const declarationSpan = isDeclarationNodeWithStartAndEnd(declaration) ?
167-
getTextSpan(declaration.start, sourceFile, declaration.end) :
168-
getTextSpan(declaration, sourceFile);
169-
if (declarationSpan.start !== span.textSpan.start || declarationSpan.length !== span.textSpan.length) {
170-
span.declarationSpan = declarationSpan;
171-
}
172-
}
164+
export function toDeclarationSpan(textSpan: TextSpan, sourceFile: SourceFile, declaration?: DeclarationNode): { declarationSpan: TextSpan } | undefined {
165+
if (!declaration) return undefined;
166+
const declarationSpan = isDeclarationNodeWithStartAndEnd(declaration) ?
167+
getTextSpan(declaration.start, sourceFile, declaration.end) :
168+
getTextSpan(declaration, sourceFile);
169+
return declarationSpan.start !== textSpan.start || declarationSpan.length !== textSpan.length ?
170+
{ declarationSpan } :
171+
undefined;
173172
}
174173

175174
export interface Options {
@@ -304,17 +303,17 @@ namespace ts.FindAllReferences {
304303

305304
const { node, name, kind, displayParts, declaration } = info;
306305
const sourceFile = node.getSourceFile();
307-
const result: ReferencedSymbolDefinitionInfo = {
306+
const textSpan = getTextSpan(isComputedPropertyName(node) ? node.expression : node, sourceFile);
307+
return {
308308
containerKind: ScriptElementKind.unknown,
309309
containerName: "",
310310
fileName: sourceFile.fileName,
311311
kind,
312312
name,
313-
textSpan: getTextSpan(isComputedPropertyName(node) ? node.expression : node, sourceFile),
314-
displayParts
313+
textSpan,
314+
displayParts,
315+
...toDeclarationSpan(textSpan, sourceFile, declaration)
315316
};
316-
setDeclarationSpan(result, sourceFile, declaration);
317-
return result;
318317
}
319318

320319
function getDefinitionKindAndDisplayParts(symbol: Symbol, checker: TypeChecker, node: Node): { displayParts: SymbolDisplayPart[], kind: ScriptElementKind } {
@@ -351,9 +350,12 @@ namespace ts.FindAllReferences {
351350
}
352351
else {
353352
const sourceFile = entry.node.getSourceFile();
354-
const result: DocumentSpan = { textSpan: getTextSpan(entry.node, sourceFile), fileName: sourceFile.fileName };
355-
setDeclarationSpan(result, sourceFile, entry.declaration);
356-
return result;
353+
const textSpan = getTextSpan(entry.node, sourceFile);
354+
return {
355+
textSpan,
356+
fileName: sourceFile.fileName,
357+
...toDeclarationSpan(textSpan, sourceFile, entry.declaration)
358+
};
357359
}
358360
}
359361

@@ -438,10 +440,8 @@ namespace ts.FindAllReferences {
438440
textSpan: documentSpan.textSpan,
439441
kind: writeAccess ? HighlightSpanKind.writtenReference : HighlightSpanKind.reference,
440442
isInString: entry.kind === EntryKind.StringLiteral ? true : undefined,
443+
...documentSpan.declarationSpan && { declarationSpan: documentSpan.declarationSpan }
441444
};
442-
if (documentSpan.declarationSpan) {
443-
span.declarationSpan = documentSpan.declarationSpan;
444-
}
445445
return { fileName: documentSpan.fileName, span };
446446
}
447447

src/services/goToDefinition.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,20 +273,20 @@ namespace ts.GoToDefinition {
273273
function createDefinitionInfoFromName(declaration: Declaration, symbolKind: ScriptElementKind, symbolName: string, containerName: string): DefinitionInfo {
274274
const name = getNameOfDeclaration(declaration) || declaration;
275275
const sourceFile = name.getSourceFile();
276-
const result: DefinitionInfo = {
276+
const textSpan = createTextSpanFromNode(name, sourceFile);
277+
return {
277278
fileName: sourceFile.fileName,
278-
textSpan: createTextSpanFromNode(name, sourceFile),
279+
textSpan,
279280
kind: symbolKind,
280281
name: symbolName,
281282
containerKind: undefined!, // TODO: GH#18217
282283
containerName,
284+
...FindAllReferences.toDeclarationSpan(
285+
textSpan,
286+
sourceFile,
287+
FindAllReferences.getDeclarationForDeclarationSpan(declaration)
288+
)
283289
};
284-
FindAllReferences.setDeclarationSpan(
285-
result,
286-
sourceFile,
287-
FindAllReferences.getDeclarationForDeclarationSpan(declaration)
288-
);
289-
return result;
290290
}
291291

292292
function createDefinitionFromSignatureDeclaration(typeChecker: TypeChecker, decl: SignatureDeclaration): DefinitionInfo {

src/services/services.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,19 +1546,14 @@ namespace ts {
15461546
function getOccurrencesAtPosition(fileName: string, position: number): ReadonlyArray<ReferenceEntry> | undefined {
15471547
return flatMap(
15481548
getDocumentHighlights(fileName, position, [fileName]),
1549-
entry => entry.highlightSpans.map(highlightSpan => {
1550-
const result: ReferenceEntry = {
1551-
fileName: entry.fileName,
1552-
textSpan: highlightSpan.textSpan,
1553-
isWriteAccess: highlightSpan.kind === HighlightSpanKind.writtenReference,
1554-
isDefinition: false,
1555-
isInString: highlightSpan.isInString,
1556-
};
1557-
if (highlightSpan.declarationSpan) {
1558-
result.declarationSpan = highlightSpan.declarationSpan;
1559-
}
1560-
return result;
1561-
})
1549+
entry => entry.highlightSpans.map<ReferenceEntry>(highlightSpan => ({
1550+
fileName: entry.fileName,
1551+
textSpan: highlightSpan.textSpan,
1552+
isWriteAccess: highlightSpan.kind === HighlightSpanKind.writtenReference,
1553+
isDefinition: false,
1554+
...highlightSpan.isInString && { isInString: true },
1555+
...highlightSpan.declarationSpan && { declarationSpan: highlightSpan.declarationSpan }
1556+
}))
15621557
);
15631558
}
15641559

@@ -1577,13 +1572,13 @@ namespace ts {
15771572
const node = getTouchingPropertyName(sourceFile, position);
15781573
if (isIdentifier(node) && (isJsxOpeningElement(node.parent) || isJsxClosingElement(node.parent)) && isIntrinsicJsxName(node.escapedText)) {
15791574
const { openingElement, closingElement } = node.parent.parent;
1580-
return [openingElement, closingElement].map(node => {
1581-
const result: RenameLocation = {
1575+
return [openingElement, closingElement].map((node): RenameLocation => {
1576+
const textSpan = createTextSpanFromNode(node.tagName, sourceFile);
1577+
return {
15821578
fileName: sourceFile.fileName,
1583-
textSpan: createTextSpanFromNode(node.tagName, sourceFile)
1579+
textSpan,
1580+
...FindAllReferences.toDeclarationSpan(textSpan, sourceFile, node.parent)
15841581
};
1585-
FindAllReferences.setDeclarationSpan(result, sourceFile, node.parent);
1586-
return result;
15871582
});
15881583
}
15891584
else {

src/testRunner/unittests/tsserver/declarationFileMaps.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ namespace ts.projectSystem {
77
declarationOptions?: SpanFromSubstringOptions;
88
}
99
function documentSpanFromSubstring({ file, text, declarationText, options, declarationOptions }: DocumentSpanFromSubstring): DocumentSpan {
10-
const result: DocumentSpan = { fileName: file.path, textSpan: textSpanFromSubstring(file.content, text, options) };
11-
if (declarationText) {
12-
const declarationSpan = documentSpanFromSubstring({ file, text: declarationText, options: declarationOptions });
13-
result.declarationSpan = declarationSpan.textSpan;
14-
}
15-
return result;
10+
const declarationSpan = declarationText !== undefined ? documentSpanFromSubstring({ file, text: declarationText, options: declarationOptions }) : undefined;
11+
return {
12+
fileName: file.path,
13+
textSpan: textSpanFromSubstring(file.content, text, options),
14+
...declarationSpan && { declarationSpan: declarationSpan.textSpan }
15+
};
1616
}
1717

1818
function renameLocation(input: DocumentSpanFromSubstring): RenameLocation {

0 commit comments

Comments
 (0)