Add names for refactoring functions. (#47375)

* Add names for refactoring functions.

* Consistency in function names.
This commit is contained in:
Daniel Rosenwasser 2022-01-11 12:20:17 -08:00 committed by GitHub
parent faee7b3621
commit ed014db864
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 45 additions and 44 deletions

View File

@ -15,8 +15,9 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
};
registerRefactor(refactorName, {
kinds: [removeBracesAction.kind],
getEditsForAction,
getAvailableActions });
getEditsForAction: getRefactorEditsToRemoveFunctionBraces,
getAvailableActions: getRefactorActionsToRemoveFunctionBraces
});
interface FunctionBracesInfo {
func: ArrowFunction;
@ -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;
@ -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");

View File

@ -24,8 +24,8 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression {
toNamedFunctionAction.kind,
toArrowFunctionAction.kind
],
getEditsForAction,
getAvailableActions
getEditsForAction: getRefactorEditsToConvertFunctionExpressions,
getAvailableActions: getRefactorActionsToConvertFunctionExpressions
});
interface FunctionInfo {
@ -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);
@ -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);

View File

@ -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;
@ -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");

View File

@ -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;
@ -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");

View File

@ -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;
@ -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;

View File

@ -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
@ -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());

View File

@ -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);
@ -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);

View File

@ -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;
@ -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 =>

View File

@ -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;
@ -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

View File

@ -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;
@ -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");

View File

@ -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");

View File

@ -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));
@ -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)) {

View File

@ -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] }];
@ -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));

View File

@ -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);
@ -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));
});
}