Skip to content

Commit

Permalink
Add names for refactoring functions. (microsoft#47375)
Browse files Browse the repository at this point in the history
* Add names for refactoring functions.

* Consistency in function names.
  • Loading branch information
DanielRosenwasser authored Jan 11, 2022
1 parent faee7b3 commit ed014db
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 44 deletions.
9 changes: 5 additions & 4 deletions src/services/refactors/addOrRemoveBracesToArrowFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
};
registerRefactor(refactorName, {
kinds: [removeBracesAction.kind],
getEditsForAction,
getAvailableActions });
getEditsForAction: getRefactorEditsToRemoveFunctionBraces,
getAvailableActions: getRefactorActionsToRemoveFunctionBraces
});

interface FunctionBracesInfo {
func: ArrowFunction;
Expand All @@ -25,7 +26,7 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
addBraces: boolean;
}

function getAvailableActions(context: RefactorContext): readonly ApplicableRefactorInfo[] {
function getRefactorActionsToRemoveFunctionBraces(context: RefactorContext): readonly ApplicableRefactorInfo[] {
const { file, startPosition, triggerReason } = context;
const info = getConvertibleArrowFunctionAtPosition(file, startPosition, triggerReason === "invoked");
if (!info) return emptyArray;
Expand Down Expand Up @@ -54,7 +55,7 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
return emptyArray;
}

function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
function getRefactorEditsToRemoveFunctionBraces(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
const { file, startPosition } = context;
const info = getConvertibleArrowFunctionAtPosition(file, startPosition);
Debug.assert(info && !isRefactorErrorInfo(info), "Expected applicable refactor info");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression {
toNamedFunctionAction.kind,
toArrowFunctionAction.kind
],
getEditsForAction,
getAvailableActions
getEditsForAction: getRefactorEditsToConvertFunctionExpressions,
getAvailableActions: getRefactorActionsToConvertFunctionExpressions
});

interface FunctionInfo {
Expand All @@ -40,7 +40,7 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression {
readonly name: Identifier;
}

function getAvailableActions(context: RefactorContext): readonly ApplicableRefactorInfo[] {
function getRefactorActionsToConvertFunctionExpressions(context: RefactorContext): readonly ApplicableRefactorInfo[] {
const { file, startPosition, program, kind } = context;
const info = getFunctionInfo(file, startPosition, program);

Expand Down Expand Up @@ -88,7 +88,7 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression {
}];
}

function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
function getRefactorEditsToConvertFunctionExpressions(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
const { file, startPosition, program } = context;
const info = getFunctionInfo(file, startPosition, program);

Expand Down
4 changes: 2 additions & 2 deletions src/services/refactors/convertExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace ts.refactor {
defaultToNamedAction.kind,
namedToDefaultAction.kind
],
getAvailableActions(context): readonly ApplicableRefactorInfo[] {
getAvailableActions: function getRefactorActionsToConvertBetweenNamedAndDefaultExports(context): readonly ApplicableRefactorInfo[] {
const info = getInfo(context, context.triggerReason === "invoked");
if (!info) return emptyArray;

Expand All @@ -38,7 +38,7 @@ namespace ts.refactor {

return emptyArray;
},
getEditsForAction(context, actionName): RefactorEditInfo {
getEditsForAction: function getRefactorEditsToConvertBetweenNamedAndDefaultExports(context, actionName): RefactorEditInfo {
Debug.assert(actionName === defaultToNamedAction.name || actionName === namedToDefaultAction.name, "Unexpected action name");
const info = getInfo(context);
Debug.assert(info && !isRefactorErrorInfo(info), "Expected applicable refactor info");
Expand Down
4 changes: 2 additions & 2 deletions src/services/refactors/convertImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace ts.refactor {
namespaceToNamedAction.kind,
namedToNamespaceAction.kind
],
getAvailableActions(context): readonly ApplicableRefactorInfo[] {
getAvailableActions: function getRefactorActionsToConvertBetweenNamedAndNamespacedImports(context): readonly ApplicableRefactorInfo[] {
const info = getImportToConvert(context, context.triggerReason === "invoked");
if (!info) return emptyArray;

Expand All @@ -39,7 +39,7 @@ namespace ts.refactor {

return emptyArray;
},
getEditsForAction(context, actionName): RefactorEditInfo {
getEditsForAction: function getRefactorEditsToConvertBetweenNamedAndNamespacedImports(context, actionName): RefactorEditInfo {
Debug.assert(actionName === namespaceToNamedAction.name || actionName === namedToNamespaceAction.name, "Unexpected action name");
const info = getImportToConvert(context);
Debug.assert(info && !isRefactorErrorInfo(info), "Expected applicable refactor info");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
};
registerRefactor(refactorName, {
kinds: [functionOverloadAction.kind],
getEditsForAction,
getAvailableActions
getEditsForAction: getRefactorEditsToConvertOverloadsToOneSignature,
getAvailableActions: getRefactorActionsToConvertOverloadsToOneSignature
});

function getAvailableActions(context: RefactorContext): readonly ApplicableRefactorInfo[] {
function getRefactorActionsToConvertOverloadsToOneSignature(context: RefactorContext): readonly ApplicableRefactorInfo[] {
const { file, startPosition, program } = context;
const info = getConvertableOverloadListAtPosition(file, startPosition, program);
if (!info) return emptyArray;
Expand All @@ -26,7 +26,7 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
}];
}

function getEditsForAction(context: RefactorContext): RefactorEditInfo | undefined {
function getRefactorEditsToConvertOverloadsToOneSignature(context: RefactorContext): RefactorEditInfo | undefined {
const { file, startPosition, program } = context;
const signatureDecls = getConvertableOverloadListAtPosition(file, startPosition, program);
if (!signatureDecls) return undefined;
Expand Down
8 changes: 4 additions & 4 deletions src/services/refactors/convertParamsToDestructuredObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ namespace ts.refactor.convertParamsToDestructuredObject {
};
registerRefactor(refactorName, {
kinds: [toDestructuredAction.kind],
getEditsForAction,
getAvailableActions
getEditsForAction: getRefactorEditsToConvertParametersToDestructuredObject,
getAvailableActions: getRefactorActionsToConvertParametersToDestructuredObject
});

function getAvailableActions(context: RefactorContext): readonly ApplicableRefactorInfo[] {
function getRefactorActionsToConvertParametersToDestructuredObject(context: RefactorContext): readonly ApplicableRefactorInfo[] {
const { file, startPosition } = context;
const isJSFile = isSourceFileJS(file);
if (isJSFile) return emptyArray; // TODO: GH#30113
Expand All @@ -29,7 +29,7 @@ namespace ts.refactor.convertParamsToDestructuredObject {
}];
}

function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
function getRefactorEditsToConvertParametersToDestructuredObject(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
Debug.assert(actionName === refactorName, "Unexpected action name");
const { file, startPosition, program, cancellationToken, host } = context;
const functionDeclaration = getFunctionDeclarationAtPosition(file, startPosition, program.getTypeChecker());
Expand Down
8 changes: 4 additions & 4 deletions src/services/refactors/convertStringOrTemplateLiteral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
};
registerRefactor(refactorName, {
kinds: [convertStringAction.kind],
getEditsForAction,
getAvailableActions
getEditsForAction: getRefactorEditsToConvertToTemplateString,
getAvailableActions: getRefactorActionsToConvertToTemplateString
});

function getAvailableActions(context: RefactorContext): readonly ApplicableRefactorInfo[] {
function getRefactorActionsToConvertToTemplateString(context: RefactorContext): readonly ApplicableRefactorInfo[] {
const { file, startPosition } = context;
const node = getNodeOrParentOfParentheses(file, startPosition);
const maybeBinary = getParentBinaryExpression(node);
Expand Down Expand Up @@ -48,7 +48,7 @@ namespace ts.refactor.convertStringOrTemplateLiteral {
return node;
}

function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
function getRefactorEditsToConvertToTemplateString(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
const { file, startPosition } = context;
const node = getNodeOrParentOfParentheses(file, startPosition);

Expand Down
8 changes: 4 additions & 4 deletions src/services/refactors/convertToOptionalChainExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ namespace ts.refactor.convertToOptionalChainExpression {
};
registerRefactor(refactorName, {
kinds: [toOptionalChainAction.kind],
getAvailableActions,
getEditsForAction
getEditsForAction: getRefactorEditsToConvertToOptionalChain,
getAvailableActions: getRefactorActionsToConvertToOptionalChain,
});

function getAvailableActions(context: RefactorContext): readonly ApplicableRefactorInfo[] {
function getRefactorActionsToConvertToOptionalChain(context: RefactorContext): readonly ApplicableRefactorInfo[] {
const info = getInfo(context, context.triggerReason === "invoked");
if (!info) return emptyArray;

Expand All @@ -36,7 +36,7 @@ namespace ts.refactor.convertToOptionalChainExpression {
return emptyArray;
}

function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
function getRefactorEditsToConvertToOptionalChain(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
const info = getInfo(context);
Debug.assert(info && !isRefactorErrorInfo(info), "Expected applicable refactor info");
const edits = textChanges.ChangeTracker.with(context, t =>
Expand Down
8 changes: 4 additions & 4 deletions src/services/refactors/extractSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ namespace ts.refactor.extractSymbol {
extractConstantAction.kind,
extractFunctionAction.kind
],
getAvailableActions,
getEditsForAction
getEditsForAction: getRefactorEditsToExtractSymbol,
getAvailableActions: getRefactorActionsToExtractSymbol,
});

/**
* Compute the associated code actions
* Exported for tests.
*/
export function getAvailableActions(context: RefactorContext): readonly ApplicableRefactorInfo[] {
export function getRefactorActionsToExtractSymbol(context: RefactorContext): readonly ApplicableRefactorInfo[] {
const requestedRefactor = context.kind;
const rangeToExtract = getRangeToExtract(context.file, getRefactorContextSpan(context), context.triggerReason === "invoked");
const targetRange = rangeToExtract.targetRange;
Expand Down Expand Up @@ -168,7 +168,7 @@ namespace ts.refactor.extractSymbol {
}

/* Exported for tests */
export function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
export function getRefactorEditsToExtractSymbol(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
const rangeToExtract = getRangeToExtract(context.file, getRefactorContextSpan(context));
const targetRange = rangeToExtract.targetRange!; // TODO:GH#18217

Expand Down
4 changes: 2 additions & 2 deletions src/services/refactors/extractType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace ts.refactor {
extractToInterfaceAction.kind,
extractToTypeDefAction.kind
],
getAvailableActions(context): readonly ApplicableRefactorInfo[] {
getAvailableActions: function getRefactorActionsToExtractType(context): readonly ApplicableRefactorInfo[] {
const info = getRangeToExtract(context, context.triggerReason === "invoked");
if (!info) return emptyArray;

Expand All @@ -51,7 +51,7 @@ namespace ts.refactor {

return emptyArray;
},
getEditsForAction(context, actionName): RefactorEditInfo {
getEditsForAction: function getRefactorEditsToExtractType(context, actionName): RefactorEditInfo {
const { file, } = context;
const info = getRangeToExtract(context);
Debug.assert(info && !isRefactorErrorInfo(info), "Expected to find a range to extract");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
};
registerRefactor(actionName, {
kinds: [generateGetSetAction.kind],
getEditsForAction(context, actionName) {
getEditsForAction: function getRefactorActionsToGenerateGetAndSetAccessors(context, actionName) {
if (!context.endPosition) return undefined;
const info = codefix.getAccessorConvertiblePropertyAtPosition(context.file, context.program, context.startPosition, context.endPosition);
Debug.assert(info && !isRefactorErrorInfo(info), "Expected applicable refactor info");
Expand Down
8 changes: 4 additions & 4 deletions src/services/refactors/inferFunctionReturnType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ namespace ts.refactor.inferFunctionReturnType {
};
registerRefactor(refactorName, {
kinds: [inferReturnTypeAction.kind],
getEditsForAction,
getAvailableActions
getEditsForAction: getRefactorEditsToInferReturnType,
getAvailableActions: getRefactorActionsToInferReturnType
});

function getEditsForAction(context: RefactorContext): RefactorEditInfo | undefined {
function getRefactorEditsToInferReturnType(context: RefactorContext): RefactorEditInfo | undefined {
const info = getInfo(context);
if (info && !isRefactorErrorInfo(info)) {
const edits = textChanges.ChangeTracker.with(context, t => doChange(context.file, t, info.declaration, info.returnTypeNode));
Expand All @@ -23,7 +23,7 @@ namespace ts.refactor.inferFunctionReturnType {
return undefined;
}

function getAvailableActions(context: RefactorContext): readonly ApplicableRefactorInfo[] {
function getRefactorActionsToInferReturnType(context: RefactorContext): readonly ApplicableRefactorInfo[] {
const info = getInfo(context);
if (!info) return emptyArray;
if (!isRefactorErrorInfo(info)) {
Expand Down
4 changes: 2 additions & 2 deletions src/services/refactors/moveToNewFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ts.refactor {
};
registerRefactor(refactorName, {
kinds: [moveToNewFileAction.kind],
getAvailableActions(context): readonly ApplicableRefactorInfo[] {
getAvailableActions: function getRefactorActionsToMoveToNewFile(context): readonly ApplicableRefactorInfo[] {
const statements = getStatementsToMove(context);
if (context.preferences.allowTextChangesInNewFiles && statements) {
return [{ name: refactorName, description, actions: [moveToNewFileAction] }];
Expand All @@ -22,7 +22,7 @@ namespace ts.refactor {
}
return emptyArray;
},
getEditsForAction(context, actionName): RefactorEditInfo {
getEditsForAction: function getRefactorEditsToMoveToNewFile(context, actionName): RefactorEditInfo {
Debug.assert(actionName === refactorName, "Wrong refactor invoked");
const statements = Debug.checkDefined(getStatementsToMove(context));
const edits = textChanges.ChangeTracker.with(context, t => doChange(context.file, context.program, statements, t, context.host, context.preferences));
Expand Down
6 changes: 3 additions & 3 deletions src/testRunner/unittests/services/extract/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ namespace ts {
};
const rangeToExtract = refactor.extractSymbol.getRangeToExtract(sourceFile, createTextSpanFromRange(selectionRange));
assert.equal(rangeToExtract.errors, undefined, rangeToExtract.errors && "Range error: " + rangeToExtract.errors[0].messageText);
const infos = refactor.extractSymbol.getAvailableActions(context);
const infos = refactor.extractSymbol.getRefactorActionsToExtractSymbol(context);
const actions = find(infos, info => info.description === description.message)!.actions;

const data: string[] = [];
data.push(`// ==ORIGINAL==`);
data.push(text.replace("[#|", "/*[#|*/").replace("|]", "/*|]*/"));
for (const action of actions) {
const { renameLocation, edits } = refactor.extractSymbol.getEditsForAction(context, action.name)!;
const { renameLocation, edits } = refactor.extractSymbol.getRefactorEditsToExtractSymbol(context, action.name)!;
assert.lengthOf(edits, 1);
data.push(`// ==SCOPE::${action.description}==`);
const newText = textChanges.applyChanges(sourceFile.text, edits[0].textChanges);
Expand Down Expand Up @@ -170,7 +170,7 @@ namespace ts {
};
const rangeToExtract = refactor.extractSymbol.getRangeToExtract(sourceFile, createTextSpanFromRange(selectionRange));
assert.isUndefined(rangeToExtract.errors, rangeToExtract.errors && "Range error: " + rangeToExtract.errors[0].messageText);
const infos = refactor.extractSymbol.getAvailableActions(context);
const infos = refactor.extractSymbol.getRefactorActionsToExtractSymbol(context);
assert.isUndefined(find(infos, info => info.description === description.message));
});
}
Expand Down

0 comments on commit ed014db

Please sign in to comment.