Span length is not optional (#18558)

* Span length is not optional

* Fix calculation of length
This commit is contained in:
Andy 2017-09-25 09:47:53 -07:00 committed by GitHub
parent 024b1fd913
commit a4cf79baa5
2 changed files with 7 additions and 4 deletions

View File

@ -43,4 +43,8 @@ namespace ts {
return refactor && refactor.getEditsForAction(context, actionName);
}
}
export function getRefactorContextLength(context: RefactorContext): number {
return context.endPosition === undefined ? 0 : context.endPosition - context.startPosition;
}
}

View File

@ -14,7 +14,7 @@ namespace ts.refactor.extractMethod {
/** Compute the associated code actions */
function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined {
const rangeToExtract = getRangeToExtract(context.file, { start: context.startPosition, length: context.endPosition - context.startPosition });
const rangeToExtract = getRangeToExtract(context.file, { start: context.startPosition, length: getRefactorContextLength(context) });
const targetRange: TargetRange = rangeToExtract.targetRange;
if (targetRange === undefined) {
@ -66,8 +66,7 @@ namespace ts.refactor.extractMethod {
}
function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
const length = context.endPosition === undefined ? 0 : context.endPosition - context.startPosition;
const rangeToExtract = getRangeToExtract(context.file, { start: context.startPosition, length });
const rangeToExtract = getRangeToExtract(context.file, { start: context.startPosition, length: getRefactorContextLength(context) });
const targetRange: TargetRange = rangeToExtract.targetRange;
const parsedIndexMatch = /^scope_(\d+)$/.exec(actionName);
@ -148,7 +147,7 @@ namespace ts.refactor.extractMethod {
*/
// exported only for tests
export function getRangeToExtract(sourceFile: SourceFile, span: TextSpan): RangeToExtract {
const length = span.length || 0;
const { length } = span;
if (length === 0) {
return { errors: [createFileDiagnostic(sourceFile, span.start, length, Messages.StatementOrExpressionExpected)] };