Ensure getApplicableRefactors doesn't crash when given a position (#28361)

This commit is contained in:
Andy
2018-11-16 09:30:37 -08:00
committed by GitHub
parent a6ef176dbb
commit 1089424035
3 changed files with 15 additions and 8 deletions
+4 -7
View File
@@ -1811,7 +1811,7 @@ namespace ts.server {
return (<protocol.FileLocationRequestArgs>locationOrSpan).line !== undefined;
}
private extractPositionAndRange(args: protocol.FileLocationOrRangeRequestArgs, scriptInfo: ScriptInfo): { position: number, textRange: TextRange } {
private extractPositionOrRange(args: protocol.FileLocationOrRangeRequestArgs, scriptInfo: ScriptInfo): number | TextRange {
let position: number | undefined;
let textRange: TextRange | undefined;
if (this.isLocation(args)) {
@@ -1821,7 +1821,7 @@ namespace ts.server {
const { startPosition, endPosition } = this.getStartAndEndPosition(args, scriptInfo);
textRange = { pos: startPosition, end: endPosition };
}
return { position: position!, textRange: textRange! }; // TODO: GH#18217
return Debug.assertDefined(position === undefined ? textRange : position);
function getPosition(loc: protocol.FileLocationRequestArgs) {
return loc.position !== undefined ? loc.position : scriptInfo.lineOffsetToPosition(loc.line, loc.offset);
@@ -1831,19 +1831,16 @@ namespace ts.server {
private getApplicableRefactors(args: protocol.GetApplicableRefactorsRequestArgs): protocol.ApplicableRefactorInfo[] {
const { file, project } = this.getFileAndProject(args);
const scriptInfo = project.getScriptInfoForNormalizedPath(file)!;
const { position, textRange } = this.extractPositionAndRange(args, scriptInfo);
return project.getLanguageService().getApplicableRefactors(file, position || textRange, this.getPreferences(file));
return project.getLanguageService().getApplicableRefactors(file, this.extractPositionOrRange(args, scriptInfo), this.getPreferences(file));
}
private getEditsForRefactor(args: protocol.GetEditsForRefactorRequestArgs, simplifiedResult: boolean): RefactorEditInfo | protocol.RefactorEditInfo {
const { file, project } = this.getFileAndProject(args);
const scriptInfo = project.getScriptInfoForNormalizedPath(file)!;
const { position, textRange } = this.extractPositionAndRange(args, scriptInfo);
const result = project.getLanguageService().getEditsForRefactor(
file,
this.getFormatOptions(file),
position || textRange,
this.extractPositionOrRange(args, scriptInfo),
args.refactor,
args.action,
this.getPreferences(file),
@@ -3433,7 +3433,17 @@ var x = 10;`
command: protocol.CommandTypes.GetApplicableRefactors,
arguments: { file: file2.path, startLine: 1, startOffset, endLine: 1, endOffset: startOffset + 1 }
});
});
describe("getApplicableRefactors", () => {
it("works when taking position", () => {
const aTs: File = { path: "/a.ts", content: "" };
const session = createSession(createServerHost([aTs]));
openFilesForSession([aTs], session);
const response = executeSessionRequest<protocol.GetApplicableRefactorsRequest, protocol.GetApplicableRefactorsResponse>(
session, protocol.CommandTypes.GetApplicableRefactors, { file: aTs.path, line: 1, offset: 1 });
assert.deepEqual<ReadonlyArray<protocol.ApplicableRefactorInfo> | undefined>(response, []);
});
});
describe("includes deferred files in the project context", () => {
+1 -1
View File
@@ -8895,7 +8895,7 @@ declare namespace ts.server {
private getFullNavigateToItems;
private getSupportedCodeFixes;
private isLocation;
private extractPositionAndRange;
private extractPositionOrRange;
private getApplicableRefactors;
private getEditsForRefactor;
private organizeImports;