Skip to content

Commit 0a97663

Browse files
author
Andy
authored
Add 'prefixText' and 'suffixText' when renaming shorthand properties (#27356)
* Add 'prefixText' and 'suffixText' when renaming shorthand properties * Make prefixText and suffixText missing instead of undefined * Fix test
1 parent 0b12e9d commit 0a97663

38 files changed

+326
-226
lines changed

src/harness/client.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,8 @@ namespace ts.server {
390390
const locations: RenameLocation[] = [];
391391
for (const entry of body.locs) {
392392
const fileName = entry.file;
393-
for (const loc of entry.locs) {
394-
locations.push({ textSpan: this.decodeSpan(loc, fileName), fileName });
393+
for (const { start, end, ...prefixSuffixText } of entry.locs) {
394+
locations.push({ textSpan: this.decodeSpan({ start, end }, fileName), fileName, ...prefixSuffixText });
395395
}
396396
}
397397

src/harness/fourslash.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,7 @@ Actual: ${stringify(fullActual)}`);
13951395
}
13961396
}
13971397

1398-
public verifyRenameLocations(startRanges: ArrayOrSingle<Range>, options: ReadonlyArray<Range> | { findInStrings?: boolean, findInComments?: boolean, ranges: ReadonlyArray<Range> }) {
1398+
public verifyRenameLocations(startRanges: ArrayOrSingle<Range>, options: FourSlashInterface.RenameLocationsOptions) {
13991399
const { findInStrings = false, findInComments = false, ranges = this.getRanges() } = ts.isArray(options) ? { findInStrings: false, findInComments: false, ranges: options } : options;
14001400

14011401
for (const startRange of toArray(startRanges)) {
@@ -1412,7 +1412,10 @@ Actual: ${stringify(fullActual)}`);
14121412

14131413
const sort = (locations: ReadonlyArray<ts.RenameLocation> | undefined) =>
14141414
locations && ts.sort(locations, (r1, r2) => ts.compareStringsCaseSensitive(r1.fileName, r2.fileName) || r1.textSpan.start - r2.textSpan.start);
1415-
assert.deepEqual(sort(references), sort(ranges.map((r): ts.RenameLocation => ({ fileName: r.fileName, textSpan: ts.createTextSpanFromRange(r) }))));
1415+
assert.deepEqual(sort(references), sort(ranges.map((rangeOrOptions): ts.RenameLocation => {
1416+
const { range, ...prefixSuffixText } = "range" in rangeOrOptions ? rangeOrOptions : { range: rangeOrOptions };
1417+
return { fileName: range.fileName, textSpan: ts.createTextSpanFromRange(range), ...prefixSuffixText };
1418+
})));
14161419
}
14171420
}
14181421

@@ -4484,7 +4487,7 @@ namespace FourSlashInterface {
44844487
this.state.verifyRenameInfoFailed(message);
44854488
}
44864489

4487-
public renameLocations(startRanges: ArrayOrSingle<FourSlash.Range>, options: FourSlash.Range[] | { findInStrings?: boolean, findInComments?: boolean, ranges: FourSlash.Range[] }) {
4490+
public renameLocations(startRanges: ArrayOrSingle<FourSlash.Range>, options: RenameLocationsOptions) {
44884491
this.state.verifyRenameLocations(startRanges, options);
44894492
}
44904493

@@ -4959,4 +4962,11 @@ namespace FourSlashInterface {
49594962
readonly newFileContents: { readonly [fileName: string]: string };
49604963
readonly preferences?: ts.UserPreferences;
49614964
}
4965+
4966+
export type RenameLocationsOptions = ReadonlyArray<RenameLocationOptions> | {
4967+
readonly findInStrings?: boolean;
4968+
readonly findInComments?: boolean;
4969+
readonly ranges: ReadonlyArray<RenameLocationOptions>;
4970+
};
4971+
export type RenameLocationOptions = FourSlash.Range | { readonly range: FourSlash.Range, readonly prefixText?: string, readonly suffixText?: string };
49624972
}

src/server/protocol.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,12 @@ namespace ts.server.protocol {
11351135
/** The file to which the spans apply */
11361136
file: string;
11371137
/** The text spans in this group */
1138-
locs: TextSpan[];
1138+
locs: RenameTextSpan[];
1139+
}
1140+
1141+
export interface RenameTextSpan extends TextSpan {
1142+
readonly prefixText?: string;
1143+
readonly suffixText?: string;
11391144
}
11401145

11411146
export interface RenameResponseBody {

src/server/session.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1190,10 +1190,11 @@ namespace ts.server {
11901190

11911191
private toSpanGroups(locations: ReadonlyArray<RenameLocation>): ReadonlyArray<protocol.SpanGroup> {
11921192
const map = createMap<protocol.SpanGroup>();
1193-
for (const { fileName, textSpan } of locations) {
1193+
for (const { fileName, textSpan, originalTextSpan: _, originalFileName: _1, ...prefixSuffixText } of locations) {
11941194
let group = map.get(fileName);
11951195
if (!group) map.set(fileName, group = { file: fileName, locs: [] });
1196-
group.locs.push(this.toLocationTextSpan(textSpan, Debug.assertDefined(this.projectService.getScriptInfo(fileName))));
1196+
const scriptInfo = Debug.assertDefined(this.projectService.getScriptInfo(fileName));
1197+
group.locs.push({ ...this.toLocationTextSpan(textSpan, scriptInfo), ...prefixSuffixText });
11971198
}
11981199
return arrayFrom(map.values());
11991200
}

src/services/codefixes/inferFromUsage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ namespace ts.codefix {
196196
function getReferences(token: PropertyName | Token<SyntaxKind.ConstructorKeyword>, program: Program, cancellationToken: CancellationToken): ReadonlyArray<Identifier> {
197197
// Position shouldn't matter since token is not a SourceFile.
198198
return mapDefined(FindAllReferences.getReferenceEntriesForNode(-1, token, program, program.getSourceFiles(), cancellationToken), entry =>
199-
entry.type === "node" ? tryCast(entry.node, isIdentifier) : undefined);
199+
entry.kind !== FindAllReferences.EntryKind.Span ? tryCast(entry.node, isIdentifier) : undefined);
200200
}
201201

202202
function inferTypeForVariableFromUsage(token: Identifier, program: Program, cancellationToken: CancellationToken): Type | undefined {

0 commit comments

Comments
 (0)