fix up some bools

This commit is contained in:
Jesse Trinity 2020-05-26 21:07:33 -07:00
parent d37f4c33cc
commit e565931392
4 changed files with 14 additions and 17 deletions

View File

@ -17,8 +17,7 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
function getAvailableActions(context: RefactorContext): readonly ApplicableRefactorInfo[] {
const { file, startPosition, triggerReason } = context;
const forImplicitRequest = triggerReason ? triggerReason === "implicit" : true;
const info = getConvertibleArrowFunctionAtPosition(file, startPosition, forImplicitRequest);
const info = getConvertibleArrowFunctionAtPosition(file, startPosition, triggerReason === "invoked");
if (!info) return emptyArray;
return [{
@ -71,12 +70,12 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
return { renameFilename: undefined, renameLocation: undefined, edits };
}
function getConvertibleArrowFunctionAtPosition(file: SourceFile, startPosition: number, forImplicitRequest = false): Info | undefined {
function getConvertibleArrowFunctionAtPosition(file: SourceFile, startPosition: number, userRequested = true): Info | undefined {
const node = getTokenAtPosition(file, startPosition);
const func = getContainingFunction(node);
// Only offer a refactor in the function body on explicit refactor requests.
if (!func || !isArrowFunction(func) || (!rangeContainsRange(func, node)
|| (rangeContainsRange(func.body, node) && forImplicitRequest))) return undefined;
|| (rangeContainsRange(func.body, node) && !userRequested))) return undefined;
if (isExpression(func.body)) {
return {

View File

@ -8,8 +8,7 @@ namespace ts.refactor.extractSymbol {
* Exported for tests.
*/
export function getAvailableActions(context: RefactorContext): readonly ApplicableRefactorInfo[] {
const forImplicitRequest = context.triggerReason ? context.triggerReason === "implicit" : true;
const rangeToExtract = getRangeToExtract(context.file, getRefactorContextSpan(context), forImplicitRequest);
const rangeToExtract = getRangeToExtract(context.file, getRefactorContextSpan(context), context.triggerReason === "invoked");
const targetRange = rangeToExtract.targetRange;
if (targetRange === undefined) {
@ -187,20 +186,20 @@ namespace ts.refactor.extractSymbol {
* not shown to the user, but can be used by us diagnostically)
*/
// exported only for tests
export function getRangeToExtract(sourceFile: SourceFile, span: TextSpan, forImplicitRequest = false): RangeToExtract {
export function getRangeToExtract(sourceFile: SourceFile, span: TextSpan, userRequested = true): RangeToExtract {
const { length } = span;
if (length === 0 && forImplicitRequest) {
if (length === 0 && !userRequested) {
return { errors: [createFileDiagnostic(sourceFile, span.start, length, Messages.cannotExtractEmpty)] };
}
const explicitCursorRequest = length === 0 && !forImplicitRequest;
const cursorRequest = length === 0 && userRequested;
// Walk up starting from the the start position until we find a non-SourceFile node that subsumes the selected span.
// This may fail (e.g. you select two statements in the root of a source file)
const startToken = getTokenAtPosition(sourceFile, span.start);
const start = explicitCursorRequest ? getExtractableParent(startToken): getParentNodeInSpan(startToken, sourceFile, span);
const start = cursorRequest ? getExtractableParent(startToken): getParentNodeInSpan(startToken, sourceFile, span);
// Do the same for the ending position
const endToken = findTokenOnLeftOfPosition(sourceFile, textSpanEnd(span));
const end = explicitCursorRequest ? start : getParentNodeInSpan(endToken, sourceFile, span);
const end = cursorRequest ? start : getParentNodeInSpan(endToken, sourceFile, span);
const declarations: Symbol[] = [];

View File

@ -6,8 +6,7 @@ namespace ts.refactor {
const extractToTypeDef = "Extract to typedef";
registerRefactor(refactorName, {
getAvailableActions(context): readonly ApplicableRefactorInfo[] {
const forImplicitRequest = context.triggerReason ? context.triggerReason === "implicit" : true;
const info = getRangeToExtract(context, forImplicitRequest);
const info = getRangeToExtract(context, context.triggerReason === "invoked");
if (!info) return emptyArray;
return [{
@ -59,15 +58,15 @@ namespace ts.refactor {
type Info = TypeAliasInfo | InterfaceInfo;
function getRangeToExtract(context: RefactorContext, forImplicitRequest = false): Info | undefined {
function getRangeToExtract(context: RefactorContext, userRequested = true): Info | undefined {
const { file, startPosition } = context;
const isJS = isSourceFileJS(file);
const current = getTokenAtPosition(file, startPosition);
const range = createTextRangeFromSpan(getRefactorContextSpan(context));
const explicitCursorRequest = range.pos === range.end && !forImplicitRequest;
const cursorRequest = range.pos === range.end && userRequested;
const selection = findAncestor(current, (node => node.parent && isTypeNode(node) && !rangeContainsSkipTrivia(range, node.parent, file) &&
(explicitCursorRequest || nodeOverlapsWithStartEnd(current, file, range.pos, range.end))));
(cursorRequest || nodeOverlapsWithStartEnd(current, file, range.pos, range.end))));
if (!selection || !isTypeNode(selection)) return undefined;
const checker = context.program.getTypeChecker();

View File

@ -7,7 +7,7 @@ namespace ts {
if (!selectionRange) {
throw new Error(`Test ${s} does not specify selection range`);
}
const result = refactor.extractSymbol.getRangeToExtract(file, createTextSpanFromRange(selectionRange), /*forImplicitRequest*/ true);
const result = refactor.extractSymbol.getRangeToExtract(file, createTextSpanFromRange(selectionRange), /*userRequested*/ false);
assert(result.targetRange === undefined, "failure expected");
const sortedErrors = result.errors!.map(e => <string>e.messageText).sort();
assert.deepEqual(sortedErrors, expectedErrors.sort(), "unexpected errors");